blob: 2db74ebc324060683aa39fc51a94b49c60b5daab [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>
Robin Murphyf9a05f02016-04-13 18:13:01 +010037#include <linux/io-64-nonatomic-hi-lo.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010038#include <linux/iommu.h>
Mitchel Humpherys859a7322014-10-29 21:13:40 +000039#include <linux/iopoll.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010040#include <linux/module.h>
41#include <linux/of.h>
Robin Murphybae2c2d2015-07-29 19:46:05 +010042#include <linux/of_address.h>
Will Deacona9a1b0b2014-05-01 18:05:08 +010043#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010044#include <linux/platform_device.h>
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47
48#include <linux/amba/bus.h>
49
Will Deacon518f7132014-11-14 17:17:54 +000050#include "io-pgtable.h"
Will Deacon45ae7cf2013-06-24 18:31:25 +010051
52/* Maximum number of stream IDs assigned to a single device */
Joerg Roedelcb6c27b2016-04-04 17:49:22 +020053#define MAX_MASTER_STREAMIDS 128
Will Deacon45ae7cf2013-06-24 18:31:25 +010054
55/* Maximum number of context banks per SMMU */
56#define ARM_SMMU_MAX_CBS 128
57
58/* Maximum number of mapping groups per SMMU */
59#define ARM_SMMU_MAX_SMRS 128
60
Will Deacon45ae7cf2013-06-24 18:31:25 +010061/* SMMU global address space */
62#define ARM_SMMU_GR0(smmu) ((smmu)->base)
Will Deaconc757e852014-07-30 11:33:25 +010063#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +010064
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +000065/*
66 * SMMU global address space with conditional offset to access secure
67 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
68 * nsGFSYNR0: 0x450)
69 */
70#define ARM_SMMU_GR0_NS(smmu) \
71 ((smmu)->base + \
72 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
73 ? 0x400 : 0))
74
Robin Murphyf9a05f02016-04-13 18:13:01 +010075/*
76 * Some 64-bit registers only make sense to write atomically, but in such
77 * cases all the data relevant to AArch32 formats lies within the lower word,
78 * therefore this actually makes more sense than it might first appear.
79 */
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +010080#ifdef CONFIG_64BIT
Robin Murphyf9a05f02016-04-13 18:13:01 +010081#define smmu_write_atomic_lq writeq_relaxed
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +010082#else
Robin Murphyf9a05f02016-04-13 18:13:01 +010083#define smmu_write_atomic_lq writel_relaxed
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +010084#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)
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -080097#define sCR0_VMID16EN (1 << 31)
Will Deacon45ae7cf2013-06-24 18:31:25 +010098#define sCR0_BSU_SHIFT 14
99#define sCR0_BSU_MASK 0x3
100
Peng Fan3ca37122016-05-03 21:50:30 +0800101/* Auxiliary Configuration register */
102#define ARM_SMMU_GR0_sACR 0x10
103
Will Deacon45ae7cf2013-06-24 18:31:25 +0100104/* Identification registers */
105#define ARM_SMMU_GR0_ID0 0x20
106#define ARM_SMMU_GR0_ID1 0x24
107#define ARM_SMMU_GR0_ID2 0x28
108#define ARM_SMMU_GR0_ID3 0x2c
109#define ARM_SMMU_GR0_ID4 0x30
110#define ARM_SMMU_GR0_ID5 0x34
111#define ARM_SMMU_GR0_ID6 0x38
112#define ARM_SMMU_GR0_ID7 0x3c
113#define ARM_SMMU_GR0_sGFSR 0x48
114#define ARM_SMMU_GR0_sGFSYNR0 0x50
115#define ARM_SMMU_GR0_sGFSYNR1 0x54
116#define ARM_SMMU_GR0_sGFSYNR2 0x58
Will Deacon45ae7cf2013-06-24 18:31:25 +0100117
118#define ID0_S1TS (1 << 30)
119#define ID0_S2TS (1 << 29)
120#define ID0_NTS (1 << 28)
121#define ID0_SMS (1 << 27)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000122#define ID0_ATOSNS (1 << 26)
Robin Murphy7602b872016-04-28 17:12:09 +0100123#define ID0_PTFS_NO_AARCH32 (1 << 25)
124#define ID0_PTFS_NO_AARCH32S (1 << 24)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100125#define ID0_CTTW (1 << 14)
126#define ID0_NUMIRPT_SHIFT 16
127#define ID0_NUMIRPT_MASK 0xff
Olav Haugan3c8766d2014-08-22 17:12:32 -0700128#define ID0_NUMSIDB_SHIFT 9
129#define ID0_NUMSIDB_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100130#define ID0_NUMSMRG_SHIFT 0
131#define ID0_NUMSMRG_MASK 0xff
132
133#define ID1_PAGESIZE (1 << 31)
134#define ID1_NUMPAGENDXB_SHIFT 28
135#define ID1_NUMPAGENDXB_MASK 7
136#define ID1_NUMS2CB_SHIFT 16
137#define ID1_NUMS2CB_MASK 0xff
138#define ID1_NUMCB_SHIFT 0
139#define ID1_NUMCB_MASK 0xff
140
141#define ID2_OAS_SHIFT 4
142#define ID2_OAS_MASK 0xf
143#define ID2_IAS_SHIFT 0
144#define ID2_IAS_MASK 0xf
145#define ID2_UBS_SHIFT 8
146#define ID2_UBS_MASK 0xf
147#define ID2_PTFS_4K (1 << 12)
148#define ID2_PTFS_16K (1 << 13)
149#define ID2_PTFS_64K (1 << 14)
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800150#define ID2_VMID16 (1 << 15)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100151
Peng Fan3ca37122016-05-03 21:50:30 +0800152#define ID7_MAJOR_SHIFT 4
153#define ID7_MAJOR_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100154
Will Deacon45ae7cf2013-06-24 18:31:25 +0100155/* Global TLB invalidation */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100156#define ARM_SMMU_GR0_TLBIVMID 0x64
157#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
158#define ARM_SMMU_GR0_TLBIALLH 0x6c
159#define ARM_SMMU_GR0_sTLBGSYNC 0x70
160#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
161#define sTLBGSTATUS_GSACTIVE (1 << 0)
162#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
163
164/* Stream mapping registers */
165#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
166#define SMR_VALID (1 << 31)
167#define SMR_MASK_SHIFT 16
168#define SMR_MASK_MASK 0x7fff
169#define SMR_ID_SHIFT 0
170#define SMR_ID_MASK 0x7fff
171
172#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
173#define S2CR_CBNDX_SHIFT 0
174#define S2CR_CBNDX_MASK 0xff
175#define S2CR_TYPE_SHIFT 16
176#define S2CR_TYPE_MASK 0x3
177#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
178#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
179#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
180
Robin Murphyd3461802016-01-26 18:06:34 +0000181#define S2CR_PRIVCFG_SHIFT 24
182#define S2CR_PRIVCFG_UNPRIV (2 << S2CR_PRIVCFG_SHIFT)
183
Will Deacon45ae7cf2013-06-24 18:31:25 +0100184/* Context bank attribute registers */
185#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
186#define CBAR_VMID_SHIFT 0
187#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000188#define CBAR_S1_BPSHCFG_SHIFT 8
189#define CBAR_S1_BPSHCFG_MASK 3
190#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100191#define CBAR_S1_MEMATTR_SHIFT 12
192#define CBAR_S1_MEMATTR_MASK 0xf
193#define CBAR_S1_MEMATTR_WB 0xf
194#define CBAR_TYPE_SHIFT 16
195#define CBAR_TYPE_MASK 0x3
196#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
197#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
198#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
199#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
200#define CBAR_IRPTNDX_SHIFT 24
201#define CBAR_IRPTNDX_MASK 0xff
202
203#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
204#define CBA2R_RW64_32BIT (0 << 0)
205#define CBA2R_RW64_64BIT (1 << 0)
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800206#define CBA2R_VMID_SHIFT 16
207#define CBA2R_VMID_MASK 0xffff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100208
209/* Translation context bank */
210#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
Will Deaconc757e852014-07-30 11:33:25 +0100211#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100212
213#define ARM_SMMU_CB_SCTLR 0x0
Robin Murphyf0cfffc2016-04-13 18:12:59 +0100214#define ARM_SMMU_CB_ACTLR 0x4
Will Deacon45ae7cf2013-06-24 18:31:25 +0100215#define ARM_SMMU_CB_RESUME 0x8
216#define ARM_SMMU_CB_TTBCR2 0x10
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100217#define ARM_SMMU_CB_TTBR0 0x20
218#define ARM_SMMU_CB_TTBR1 0x28
Will Deacon45ae7cf2013-06-24 18:31:25 +0100219#define ARM_SMMU_CB_TTBCR 0x30
220#define ARM_SMMU_CB_S1_MAIR0 0x38
Will Deacon518f7132014-11-14 17:17:54 +0000221#define ARM_SMMU_CB_S1_MAIR1 0x3c
Robin Murphyf9a05f02016-04-13 18:13:01 +0100222#define ARM_SMMU_CB_PAR 0x50
Will Deacon45ae7cf2013-06-24 18:31:25 +0100223#define ARM_SMMU_CB_FSR 0x58
Robin Murphyf9a05f02016-04-13 18:13:01 +0100224#define ARM_SMMU_CB_FAR 0x60
Will Deacon45ae7cf2013-06-24 18:31:25 +0100225#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon518f7132014-11-14 17:17:54 +0000226#define ARM_SMMU_CB_S1_TLBIVA 0x600
Will Deacon1463fe42013-07-31 19:21:27 +0100227#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon518f7132014-11-14 17:17:54 +0000228#define ARM_SMMU_CB_S1_TLBIVAL 0x620
229#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
230#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
Robin Murphy661d9622015-05-27 17:09:34 +0100231#define ARM_SMMU_CB_ATS1PR 0x800
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000232#define ARM_SMMU_CB_ATSR 0x8f0
Will Deacon45ae7cf2013-06-24 18:31:25 +0100233
234#define SCTLR_S1_ASIDPNE (1 << 12)
235#define SCTLR_CFCFG (1 << 7)
236#define SCTLR_CFIE (1 << 6)
237#define SCTLR_CFRE (1 << 5)
238#define SCTLR_E (1 << 4)
239#define SCTLR_AFE (1 << 2)
240#define SCTLR_TRE (1 << 1)
241#define SCTLR_M (1 << 0)
242#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
243
Robin Murphyf0cfffc2016-04-13 18:12:59 +0100244#define ARM_MMU500_ACTLR_CPRE (1 << 1)
245
Peng Fan3ca37122016-05-03 21:50:30 +0800246#define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
247
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000248#define CB_PAR_F (1 << 0)
249
250#define ATSR_ACTIVE (1 << 0)
251
Will Deacon45ae7cf2013-06-24 18:31:25 +0100252#define RESUME_RETRY (0 << 0)
253#define RESUME_TERMINATE (1 << 0)
254
Will Deacon45ae7cf2013-06-24 18:31:25 +0100255#define TTBCR2_SEP_SHIFT 15
Will Deacon5dc56162015-05-08 17:44:22 +0100256#define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100257
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100258#define TTBRn_ASID_SHIFT 48
Will Deacon45ae7cf2013-06-24 18:31:25 +0100259
260#define FSR_MULTI (1 << 31)
261#define FSR_SS (1 << 30)
262#define FSR_UUT (1 << 8)
263#define FSR_ASF (1 << 7)
264#define FSR_TLBLKF (1 << 6)
265#define FSR_TLBMCF (1 << 5)
266#define FSR_EF (1 << 4)
267#define FSR_PF (1 << 3)
268#define FSR_AFF (1 << 2)
269#define FSR_TF (1 << 1)
270
Mitchel Humpherys29073202014-07-08 09:52:18 -0700271#define FSR_IGN (FSR_AFF | FSR_ASF | \
272 FSR_TLBMCF | FSR_TLBLKF)
273#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100274 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100275
276#define FSYNR0_WNR (1 << 4)
277
Will Deacon4cf740b2014-07-14 19:47:39 +0100278static int force_stage;
Robin Murphy25a1c962016-02-10 14:25:33 +0000279module_param(force_stage, int, S_IRUGO);
Will Deacon4cf740b2014-07-14 19:47:39 +0100280MODULE_PARM_DESC(force_stage,
281 "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 +0000282static bool disable_bypass;
283module_param(disable_bypass, bool, S_IRUGO);
284MODULE_PARM_DESC(disable_bypass,
285 "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 +0100286
Robin Murphy09360402014-08-28 17:51:59 +0100287enum arm_smmu_arch_version {
Robin Murphyb7862e32016-04-13 18:13:03 +0100288 ARM_SMMU_V1,
289 ARM_SMMU_V1_64K,
Robin Murphy09360402014-08-28 17:51:59 +0100290 ARM_SMMU_V2,
291};
292
Robin Murphy67b65a32016-04-13 18:12:57 +0100293enum arm_smmu_implementation {
294 GENERIC_SMMU,
Robin Murphyf0cfffc2016-04-13 18:12:59 +0100295 ARM_MMU500,
Robin Murphye086d912016-04-13 18:12:58 +0100296 CAVIUM_SMMUV2,
Robin Murphy67b65a32016-04-13 18:12:57 +0100297};
298
Will Deacon45ae7cf2013-06-24 18:31:25 +0100299struct arm_smmu_smr {
300 u8 idx;
301 u16 mask;
302 u16 id;
303};
304
Will Deacona9a1b0b2014-05-01 18:05:08 +0100305struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100306 int num_streamids;
307 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100308 struct arm_smmu_smr *smrs;
309};
310
Will Deacona9a1b0b2014-05-01 18:05:08 +0100311struct arm_smmu_master {
312 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100313 struct rb_node node;
314 struct arm_smmu_master_cfg cfg;
315};
316
Will Deacon45ae7cf2013-06-24 18:31:25 +0100317struct arm_smmu_device {
318 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100319
320 void __iomem *base;
321 unsigned long size;
Will Deaconc757e852014-07-30 11:33:25 +0100322 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100323
324#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
325#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
326#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
327#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
328#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000329#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800330#define ARM_SMMU_FEAT_VMID16 (1 << 6)
Robin Murphy7602b872016-04-28 17:12:09 +0100331#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
332#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
333#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
334#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
335#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100336 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000337
338#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
339 u32 options;
Robin Murphy09360402014-08-28 17:51:59 +0100340 enum arm_smmu_arch_version version;
Robin Murphy67b65a32016-04-13 18:12:57 +0100341 enum arm_smmu_implementation model;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100342
343 u32 num_context_banks;
344 u32 num_s2_context_banks;
345 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
346 atomic_t irptndx;
347
348 u32 num_mapping_groups;
349 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
350
Will Deacon518f7132014-11-14 17:17:54 +0000351 unsigned long va_size;
352 unsigned long ipa_size;
353 unsigned long pa_size;
Robin Murphyd5466352016-05-09 17:20:09 +0100354 unsigned long pgsize_bitmap;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100355
356 u32 num_global_irqs;
357 u32 num_context_irqs;
358 unsigned int *irqs;
359
Will Deacon45ae7cf2013-06-24 18:31:25 +0100360 struct list_head list;
361 struct rb_root masters;
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800362
363 u32 cavium_id_base; /* Specific to Cavium */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100364};
365
Robin Murphy7602b872016-04-28 17:12:09 +0100366enum arm_smmu_context_fmt {
367 ARM_SMMU_CTX_FMT_NONE,
368 ARM_SMMU_CTX_FMT_AARCH64,
369 ARM_SMMU_CTX_FMT_AARCH32_L,
370 ARM_SMMU_CTX_FMT_AARCH32_S,
Will Deacon45ae7cf2013-06-24 18:31:25 +0100371};
372
373struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100374 u8 cbndx;
375 u8 irptndx;
376 u32 cbar;
Robin Murphy7602b872016-04-28 17:12:09 +0100377 enum arm_smmu_context_fmt fmt;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100378};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100379#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100380
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800381#define ARM_SMMU_CB_ASID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx)
382#define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1)
Will Deaconecfadb62013-07-31 19:21:28 +0100383
Will Deaconc752ce42014-06-25 22:46:31 +0100384enum arm_smmu_domain_stage {
385 ARM_SMMU_DOMAIN_S1 = 0,
386 ARM_SMMU_DOMAIN_S2,
387 ARM_SMMU_DOMAIN_NESTED,
388};
389
Will Deacon45ae7cf2013-06-24 18:31:25 +0100390struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100391 struct arm_smmu_device *smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000392 struct io_pgtable_ops *pgtbl_ops;
393 spinlock_t pgtbl_lock;
Will Deacon44680ee2014-06-25 11:29:12 +0100394 struct arm_smmu_cfg cfg;
Will Deaconc752ce42014-06-25 22:46:31 +0100395 enum arm_smmu_domain_stage stage;
Will Deacon518f7132014-11-14 17:17:54 +0000396 struct mutex init_mutex; /* Protects smmu pointer */
Joerg Roedel1d672632015-03-26 13:43:10 +0100397 struct iommu_domain domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100398};
399
Joerg Roedelcb6c27b2016-04-04 17:49:22 +0200400struct arm_smmu_phandle_args {
401 struct device_node *np;
402 int args_count;
403 uint32_t args[MAX_MASTER_STREAMIDS];
404};
405
Will Deacon45ae7cf2013-06-24 18:31:25 +0100406static DEFINE_SPINLOCK(arm_smmu_devices_lock);
407static LIST_HEAD(arm_smmu_devices);
408
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000409struct arm_smmu_option_prop {
410 u32 opt;
411 const char *prop;
412};
413
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800414static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
415
Mitchel Humpherys29073202014-07-08 09:52:18 -0700416static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000417 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
418 { 0, NULL},
419};
420
Joerg Roedel1d672632015-03-26 13:43:10 +0100421static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
422{
423 return container_of(dom, struct arm_smmu_domain, domain);
424}
425
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000426static void parse_driver_options(struct arm_smmu_device *smmu)
427{
428 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700429
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000430 do {
431 if (of_property_read_bool(smmu->dev->of_node,
432 arm_smmu_options[i].prop)) {
433 smmu->options |= arm_smmu_options[i].opt;
434 dev_notice(smmu->dev, "option %s\n",
435 arm_smmu_options[i].prop);
436 }
437 } while (arm_smmu_options[++i].opt);
438}
439
Will Deacon8f68f8e2014-07-15 11:27:08 +0100440static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100441{
442 if (dev_is_pci(dev)) {
443 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700444
Will Deacona9a1b0b2014-05-01 18:05:08 +0100445 while (!pci_is_root_bus(bus))
446 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100447 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100448 }
449
Will Deacon8f68f8e2014-07-15 11:27:08 +0100450 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100451}
452
Will Deacon45ae7cf2013-06-24 18:31:25 +0100453static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
454 struct device_node *dev_node)
455{
456 struct rb_node *node = smmu->masters.rb_node;
457
458 while (node) {
459 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700460
Will Deacon45ae7cf2013-06-24 18:31:25 +0100461 master = container_of(node, struct arm_smmu_master, node);
462
463 if (dev_node < master->of_node)
464 node = node->rb_left;
465 else if (dev_node > master->of_node)
466 node = node->rb_right;
467 else
468 return master;
469 }
470
471 return NULL;
472}
473
Will Deacona9a1b0b2014-05-01 18:05:08 +0100474static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100475find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100476{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100477 struct arm_smmu_master_cfg *cfg = NULL;
478 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100479
Will Deacon8f68f8e2014-07-15 11:27:08 +0100480 if (group) {
481 cfg = iommu_group_get_iommudata(group);
482 iommu_group_put(group);
483 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100484
Will Deacon8f68f8e2014-07-15 11:27:08 +0100485 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100486}
487
Will Deacon45ae7cf2013-06-24 18:31:25 +0100488static int insert_smmu_master(struct arm_smmu_device *smmu,
489 struct arm_smmu_master *master)
490{
491 struct rb_node **new, *parent;
492
493 new = &smmu->masters.rb_node;
494 parent = NULL;
495 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700496 struct arm_smmu_master *this
497 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100498
499 parent = *new;
500 if (master->of_node < this->of_node)
501 new = &((*new)->rb_left);
502 else if (master->of_node > this->of_node)
503 new = &((*new)->rb_right);
504 else
505 return -EEXIST;
506 }
507
508 rb_link_node(&master->node, parent, new);
509 rb_insert_color(&master->node, &smmu->masters);
510 return 0;
511}
512
513static int register_smmu_master(struct arm_smmu_device *smmu,
514 struct device *dev,
Joerg Roedelcb6c27b2016-04-04 17:49:22 +0200515 struct arm_smmu_phandle_args *masterspec)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100516{
517 int i;
518 struct arm_smmu_master *master;
519
520 master = find_smmu_master(smmu, masterspec->np);
521 if (master) {
522 dev_err(dev,
523 "rejecting multiple registrations for master device %s\n",
524 masterspec->np->name);
525 return -EBUSY;
526 }
527
528 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
529 dev_err(dev,
530 "reached maximum number (%d) of stream IDs for master device %s\n",
531 MAX_MASTER_STREAMIDS, masterspec->np->name);
532 return -ENOSPC;
533 }
534
535 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
536 if (!master)
537 return -ENOMEM;
538
Will Deacona9a1b0b2014-05-01 18:05:08 +0100539 master->of_node = masterspec->np;
540 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100541
Olav Haugan3c8766d2014-08-22 17:12:32 -0700542 for (i = 0; i < master->cfg.num_streamids; ++i) {
543 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100544
Olav Haugan3c8766d2014-08-22 17:12:32 -0700545 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
546 (streamid >= smmu->num_mapping_groups)) {
547 dev_err(dev,
548 "stream ID for master device %s greater than maximum allowed (%d)\n",
549 masterspec->np->name, smmu->num_mapping_groups);
550 return -ERANGE;
551 }
552 master->cfg.streamids[i] = streamid;
553 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100554 return insert_smmu_master(smmu, master);
555}
556
Will Deacon44680ee2014-06-25 11:29:12 +0100557static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100558{
Will Deacon44680ee2014-06-25 11:29:12 +0100559 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100560 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100561 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100562
563 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100564 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100565 master = find_smmu_master(smmu, dev_node);
566 if (master)
567 break;
568 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100569 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100570
Will Deacona9a1b0b2014-05-01 18:05:08 +0100571 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100572}
573
574static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
575{
576 int idx;
577
578 do {
579 idx = find_next_zero_bit(map, end, start);
580 if (idx == end)
581 return -ENOSPC;
582 } while (test_and_set_bit(idx, map));
583
584 return idx;
585}
586
587static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
588{
589 clear_bit(idx, map);
590}
591
592/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000593static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100594{
595 int count = 0;
596 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
597
598 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
599 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
600 & sTLBGSTATUS_GSACTIVE) {
601 cpu_relax();
602 if (++count == TLB_LOOP_TIMEOUT) {
603 dev_err_ratelimited(smmu->dev,
604 "TLB sync timed out -- SMMU may be deadlocked\n");
605 return;
606 }
607 udelay(1);
608 }
609}
610
Will Deacon518f7132014-11-14 17:17:54 +0000611static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100612{
Will Deacon518f7132014-11-14 17:17:54 +0000613 struct arm_smmu_domain *smmu_domain = cookie;
614 __arm_smmu_tlb_sync(smmu_domain->smmu);
615}
616
617static void arm_smmu_tlb_inv_context(void *cookie)
618{
619 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100620 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
621 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100622 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000623 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100624
625 if (stage1) {
626 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800627 writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg),
Will Deaconecfadb62013-07-31 19:21:28 +0100628 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100629 } else {
630 base = ARM_SMMU_GR0(smmu);
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800631 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg),
Will Deaconecfadb62013-07-31 19:21:28 +0100632 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100633 }
634
Will Deacon518f7132014-11-14 17:17:54 +0000635 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100636}
637
Will Deacon518f7132014-11-14 17:17:54 +0000638static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
Robin Murphy06c610e2015-12-07 18:18:53 +0000639 size_t granule, bool leaf, void *cookie)
Will Deacon518f7132014-11-14 17:17:54 +0000640{
641 struct arm_smmu_domain *smmu_domain = cookie;
642 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
643 struct arm_smmu_device *smmu = smmu_domain->smmu;
644 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
645 void __iomem *reg;
646
647 if (stage1) {
648 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
649 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
650
Robin Murphy7602b872016-04-28 17:12:09 +0100651 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
Will Deacon518f7132014-11-14 17:17:54 +0000652 iova &= ~12UL;
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800653 iova |= ARM_SMMU_CB_ASID(smmu, cfg);
Robin Murphy75df1382015-12-07 18:18:52 +0000654 do {
655 writel_relaxed(iova, reg);
656 iova += granule;
657 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000658 } else {
659 iova >>= 12;
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800660 iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48;
Robin Murphy75df1382015-12-07 18:18:52 +0000661 do {
662 writeq_relaxed(iova, reg);
663 iova += granule >> 12;
664 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000665 }
Will Deacon518f7132014-11-14 17:17:54 +0000666 } else if (smmu->version == ARM_SMMU_V2) {
667 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
668 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
669 ARM_SMMU_CB_S2_TLBIIPAS2;
Robin Murphy75df1382015-12-07 18:18:52 +0000670 iova >>= 12;
671 do {
Robin Murphyf9a05f02016-04-13 18:13:01 +0100672 smmu_write_atomic_lq(iova, reg);
Robin Murphy75df1382015-12-07 18:18:52 +0000673 iova += granule >> 12;
674 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000675 } else {
676 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800677 writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg);
Will Deacon518f7132014-11-14 17:17:54 +0000678 }
679}
680
Will Deacon518f7132014-11-14 17:17:54 +0000681static struct iommu_gather_ops arm_smmu_gather_ops = {
682 .tlb_flush_all = arm_smmu_tlb_inv_context,
683 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
684 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon518f7132014-11-14 17:17:54 +0000685};
686
Will Deacon45ae7cf2013-06-24 18:31:25 +0100687static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
688{
Will Deacon3714ce1d2016-08-05 19:49:45 +0100689 u32 fsr, fsynr;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100690 unsigned long iova;
691 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100692 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100693 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
694 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100695 void __iomem *cb_base;
696
Will Deacon44680ee2014-06-25 11:29:12 +0100697 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100698 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
699
700 if (!(fsr & FSR_FAULT))
701 return IRQ_NONE;
702
Will Deacon45ae7cf2013-06-24 18:31:25 +0100703 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
Robin Murphyf9a05f02016-04-13 18:13:01 +0100704 iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100705
Will Deacon3714ce1d2016-08-05 19:49:45 +0100706 dev_err_ratelimited(smmu->dev,
707 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cb=%d\n",
708 fsr, iova, fsynr, cfg->cbndx);
709
Will Deacon45ae7cf2013-06-24 18:31:25 +0100710 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
Will Deacon3714ce1d2016-08-05 19:49:45 +0100711 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100712}
713
714static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
715{
716 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
717 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000718 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100719
720 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
721 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
722 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
723 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
724
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000725 if (!gfsr)
726 return IRQ_NONE;
727
Will Deacon45ae7cf2013-06-24 18:31:25 +0100728 dev_err_ratelimited(smmu->dev,
729 "Unexpected global fault, this could be serious\n");
730 dev_err_ratelimited(smmu->dev,
731 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
732 gfsr, gfsynr0, gfsynr1, gfsynr2);
733
734 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100735 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100736}
737
Will Deacon518f7132014-11-14 17:17:54 +0000738static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
739 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100740{
741 u32 reg;
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100742 u64 reg64;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100743 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100744 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
745 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deaconc88ae5d2015-10-13 17:53:24 +0100746 void __iomem *cb_base, *gr1_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100747
Will Deacon45ae7cf2013-06-24 18:31:25 +0100748 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100749 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
750 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100751
Will Deacon4a1c93c2015-03-04 12:21:03 +0000752 if (smmu->version > ARM_SMMU_V1) {
Robin Murphy7602b872016-04-28 17:12:09 +0100753 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
754 reg = CBA2R_RW64_64BIT;
755 else
756 reg = CBA2R_RW64_32BIT;
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800757 /* 16-bit VMIDs live in CBA2R */
758 if (smmu->features & ARM_SMMU_FEAT_VMID16)
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800759 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT;
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800760
Will Deacon4a1c93c2015-03-04 12:21:03 +0000761 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
762 }
763
Will Deacon45ae7cf2013-06-24 18:31:25 +0100764 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100765 reg = cfg->cbar;
Robin Murphyb7862e32016-04-13 18:13:03 +0100766 if (smmu->version < ARM_SMMU_V2)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700767 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100768
Will Deacon57ca90f2014-02-06 14:59:05 +0000769 /*
770 * Use the weakest shareability/memory types, so they are
771 * overridden by the ttbcr/pte.
772 */
773 if (stage1) {
774 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
775 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800776 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
777 /* 8-bit VMIDs live in CBAR */
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800778 reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000779 }
Will Deacon44680ee2014-06-25 11:29:12 +0100780 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100781
Will Deacon518f7132014-11-14 17:17:54 +0000782 /* TTBRs */
783 if (stage1) {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100784 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100785
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800786 reg64 |= ((u64)ARM_SMMU_CB_ASID(smmu, cfg)) << TTBRn_ASID_SHIFT;
Robin Murphyf9a05f02016-04-13 18:13:01 +0100787 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100788
789 reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
Tirumalesh Chalamarla1bd37a62016-03-04 13:56:09 -0800790 reg64 |= ((u64)ARM_SMMU_CB_ASID(smmu, cfg)) << TTBRn_ASID_SHIFT;
Robin Murphyf9a05f02016-04-13 18:13:01 +0100791 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1);
Will Deacon518f7132014-11-14 17:17:54 +0000792 } else {
Tirumalesh Chalamarla668b4ad2015-08-19 00:40:30 +0100793 reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
Robin Murphyf9a05f02016-04-13 18:13:01 +0100794 writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
Will Deacon518f7132014-11-14 17:17:54 +0000795 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100796
Will Deacon518f7132014-11-14 17:17:54 +0000797 /* TTBCR */
798 if (stage1) {
799 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
800 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
801 if (smmu->version > ARM_SMMU_V1) {
802 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
Will Deacon5dc56162015-05-08 17:44:22 +0100803 reg |= TTBCR2_SEP_UPSTREAM;
Will Deacon518f7132014-11-14 17:17:54 +0000804 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100805 }
806 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000807 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
808 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100809 }
810
Will Deacon518f7132014-11-14 17:17:54 +0000811 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100812 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000813 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100814 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000815 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
816 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100817 }
818
Will Deacon45ae7cf2013-06-24 18:31:25 +0100819 /* SCTLR */
Will Deacon3714ce1d2016-08-05 19:49:45 +0100820 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100821 if (stage1)
822 reg |= SCTLR_S1_ASIDPNE;
823#ifdef __BIG_ENDIAN
824 reg |= SCTLR_E;
825#endif
Will Deacon25724842013-08-21 13:49:53 +0100826 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100827}
828
829static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100830 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100831{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100832 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000833 unsigned long ias, oas;
834 struct io_pgtable_ops *pgtbl_ops;
835 struct io_pgtable_cfg pgtbl_cfg;
836 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100837 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100838 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100839
Will Deacon518f7132014-11-14 17:17:54 +0000840 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100841 if (smmu_domain->smmu)
842 goto out_unlock;
843
Robin Murphy98006992016-04-20 14:53:33 +0100844 /* We're bypassing these SIDs, so don't allocate an actual context */
845 if (domain->type == IOMMU_DOMAIN_DMA) {
846 smmu_domain->smmu = smmu;
847 goto out_unlock;
848 }
849
Will Deaconc752ce42014-06-25 22:46:31 +0100850 /*
851 * Mapping the requested stage onto what we support is surprisingly
852 * complicated, mainly because the spec allows S1+S2 SMMUs without
853 * support for nested translation. That means we end up with the
854 * following table:
855 *
856 * Requested Supported Actual
857 * S1 N S1
858 * S1 S1+S2 S1
859 * S1 S2 S2
860 * S1 S1 S1
861 * N N N
862 * N S1+S2 S2
863 * N S2 S2
864 * N S1 S1
865 *
866 * Note that you can't actually request stage-2 mappings.
867 */
868 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
869 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
870 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
871 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
872
Robin Murphy7602b872016-04-28 17:12:09 +0100873 /*
874 * Choosing a suitable context format is even more fiddly. Until we
875 * grow some way for the caller to express a preference, and/or move
876 * the decision into the io-pgtable code where it arguably belongs,
877 * just aim for the closest thing to the rest of the system, and hope
878 * that the hardware isn't esoteric enough that we can't assume AArch64
879 * support to be a superset of AArch32 support...
880 */
881 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
882 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
883 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
884 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
885 ARM_SMMU_FEAT_FMT_AARCH64_16K |
886 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
887 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
888
889 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
890 ret = -EINVAL;
891 goto out_unlock;
892 }
893
Will Deaconc752ce42014-06-25 22:46:31 +0100894 switch (smmu_domain->stage) {
895 case ARM_SMMU_DOMAIN_S1:
896 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
897 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000898 ias = smmu->va_size;
899 oas = smmu->ipa_size;
Robin Murphy7602b872016-04-28 17:12:09 +0100900 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
Will Deacon518f7132014-11-14 17:17:54 +0000901 fmt = ARM_64_LPAE_S1;
Robin Murphy7602b872016-04-28 17:12:09 +0100902 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000903 fmt = ARM_32_LPAE_S1;
Robin Murphy7602b872016-04-28 17:12:09 +0100904 ias = min(ias, 32UL);
905 oas = min(oas, 40UL);
906 }
Will Deaconc752ce42014-06-25 22:46:31 +0100907 break;
908 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100909 /*
910 * We will likely want to change this if/when KVM gets
911 * involved.
912 */
Will Deaconc752ce42014-06-25 22:46:31 +0100913 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100914 cfg->cbar = CBAR_TYPE_S2_TRANS;
915 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000916 ias = smmu->ipa_size;
917 oas = smmu->pa_size;
Robin Murphy7602b872016-04-28 17:12:09 +0100918 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
Will Deacon518f7132014-11-14 17:17:54 +0000919 fmt = ARM_64_LPAE_S2;
Robin Murphy7602b872016-04-28 17:12:09 +0100920 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000921 fmt = ARM_32_LPAE_S2;
Robin Murphy7602b872016-04-28 17:12:09 +0100922 ias = min(ias, 40UL);
923 oas = min(oas, 40UL);
924 }
Will Deaconc752ce42014-06-25 22:46:31 +0100925 break;
926 default:
927 ret = -EINVAL;
928 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100929 }
930
931 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
932 smmu->num_context_banks);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200933 if (ret < 0)
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100934 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100935
Will Deacon44680ee2014-06-25 11:29:12 +0100936 cfg->cbndx = ret;
Robin Murphyb7862e32016-04-13 18:13:03 +0100937 if (smmu->version < ARM_SMMU_V2) {
Will Deacon44680ee2014-06-25 11:29:12 +0100938 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
939 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100940 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100941 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100942 }
943
Will Deacon518f7132014-11-14 17:17:54 +0000944 pgtbl_cfg = (struct io_pgtable_cfg) {
Robin Murphyd5466352016-05-09 17:20:09 +0100945 .pgsize_bitmap = smmu->pgsize_bitmap,
Will Deacon518f7132014-11-14 17:17:54 +0000946 .ias = ias,
947 .oas = oas,
948 .tlb = &arm_smmu_gather_ops,
Robin Murphy2df7a252015-07-29 19:46:06 +0100949 .iommu_dev = smmu->dev,
Will Deacon518f7132014-11-14 17:17:54 +0000950 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100951
Will Deacon518f7132014-11-14 17:17:54 +0000952 smmu_domain->smmu = smmu;
953 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
954 if (!pgtbl_ops) {
955 ret = -ENOMEM;
956 goto out_clear_smmu;
957 }
958
Robin Murphyd5466352016-05-09 17:20:09 +0100959 /* Update the domain's page sizes to reflect the page table format */
960 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
Will Deacon518f7132014-11-14 17:17:54 +0000961
962 /* Initialise the context bank with our page table cfg */
963 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
964
965 /*
966 * Request context fault interrupt. Do this last to avoid the
967 * handler seeing a half-initialised domain state.
968 */
Will Deacon44680ee2014-06-25 11:29:12 +0100969 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Peng Fanbee14002016-07-04 17:38:22 +0800970 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
971 IRQF_SHARED, "arm-smmu-context-fault", domain);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200972 if (ret < 0) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100973 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100974 cfg->irptndx, irq);
975 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100976 }
977
Will Deacon518f7132014-11-14 17:17:54 +0000978 mutex_unlock(&smmu_domain->init_mutex);
979
980 /* Publish page table ops for map/unmap */
981 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100982 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100983
Will Deacon518f7132014-11-14 17:17:54 +0000984out_clear_smmu:
985 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100986out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000987 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100988 return ret;
989}
990
991static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
992{
Joerg Roedel1d672632015-03-26 13:43:10 +0100993 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100994 struct arm_smmu_device *smmu = smmu_domain->smmu;
995 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100996 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100997 int irq;
998
Robin Murphy98006992016-04-20 14:53:33 +0100999 if (!smmu || domain->type == IOMMU_DOMAIN_DMA)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001000 return;
1001
Will Deacon518f7132014-11-14 17:17:54 +00001002 /*
1003 * Disable the context bank and free the page tables before freeing
1004 * it.
1005 */
Will Deacon44680ee2014-06-25 11:29:12 +01001006 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +01001007 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +01001008
Will Deacon44680ee2014-06-25 11:29:12 +01001009 if (cfg->irptndx != INVALID_IRPTNDX) {
1010 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Peng Fanbee14002016-07-04 17:38:22 +08001011 devm_free_irq(smmu->dev, irq, domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001012 }
1013
Markus Elfring44830b02015-11-06 18:32:41 +01001014 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon44680ee2014-06-25 11:29:12 +01001015 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001016}
1017
Joerg Roedel1d672632015-03-26 13:43:10 +01001018static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001019{
1020 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001021
Robin Murphy9adb9592016-01-26 18:06:36 +00001022 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
Joerg Roedel1d672632015-03-26 13:43:10 +01001023 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001024 /*
1025 * Allocate the domain and initialise some of its data structures.
1026 * We can't really do anything meaningful until we've added a
1027 * master.
1028 */
1029 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1030 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +01001031 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001032
Robin Murphy9adb9592016-01-26 18:06:36 +00001033 if (type == IOMMU_DOMAIN_DMA &&
1034 iommu_get_dma_cookie(&smmu_domain->domain)) {
1035 kfree(smmu_domain);
1036 return NULL;
1037 }
1038
Will Deacon518f7132014-11-14 17:17:54 +00001039 mutex_init(&smmu_domain->init_mutex);
1040 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +01001041
1042 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001043}
1044
Joerg Roedel1d672632015-03-26 13:43:10 +01001045static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001046{
Joerg Roedel1d672632015-03-26 13:43:10 +01001047 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +01001048
1049 /*
1050 * Free the domain resources. We assume that all devices have
1051 * already been detached.
1052 */
Robin Murphy9adb9592016-01-26 18:06:36 +00001053 iommu_put_dma_cookie(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001054 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001055 kfree(smmu_domain);
1056}
1057
1058static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001059 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001060{
1061 int i;
1062 struct arm_smmu_smr *smrs;
1063 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1064
1065 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1066 return 0;
1067
Will Deacona9a1b0b2014-05-01 18:05:08 +01001068 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001069 return -EEXIST;
1070
Mitchel Humpherys29073202014-07-08 09:52:18 -07001071 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001072 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001073 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1074 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001075 return -ENOMEM;
1076 }
1077
Will Deacon44680ee2014-06-25 11:29:12 +01001078 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001079 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001080 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1081 smmu->num_mapping_groups);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001082 if (idx < 0) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001083 dev_err(smmu->dev, "failed to allocate free SMR\n");
1084 goto err_free_smrs;
1085 }
1086
1087 smrs[i] = (struct arm_smmu_smr) {
1088 .idx = idx,
1089 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001090 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001091 };
1092 }
1093
1094 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001095 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001096 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1097 smrs[i].mask << SMR_MASK_SHIFT;
1098 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1099 }
1100
Will Deacona9a1b0b2014-05-01 18:05:08 +01001101 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001102 return 0;
1103
1104err_free_smrs:
1105 while (--i >= 0)
1106 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1107 kfree(smrs);
1108 return -ENOSPC;
1109}
1110
1111static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001112 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001113{
1114 int i;
1115 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001116 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001117
Will Deacon43b412b2014-07-15 11:22:24 +01001118 if (!smrs)
1119 return;
1120
Will Deacon45ae7cf2013-06-24 18:31:25 +01001121 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001122 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001123 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001124
Will Deacon45ae7cf2013-06-24 18:31:25 +01001125 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1126 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1127 }
1128
Will Deacona9a1b0b2014-05-01 18:05:08 +01001129 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001130 kfree(smrs);
1131}
1132
Will Deacon45ae7cf2013-06-24 18:31:25 +01001133static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001134 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001135{
1136 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001137 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001138 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1139
Will Deacon5f634952016-04-20 14:53:32 +01001140 /*
1141 * FIXME: This won't be needed once we have IOMMU-backed DMA ops
1142 * for all devices behind the SMMU. Note that we need to take
1143 * care configuring SMRs for devices both a platform_device and
1144 * and a PCI device (i.e. a PCI host controller)
1145 */
1146 if (smmu_domain->domain.type == IOMMU_DOMAIN_DMA)
1147 return 0;
1148
Will Deacon8f68f8e2014-07-15 11:27:08 +01001149 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001150 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001151 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001152 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001153
Will Deacona9a1b0b2014-05-01 18:05:08 +01001154 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001155 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001156
Will Deacona9a1b0b2014-05-01 18:05:08 +01001157 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Robin Murphyd3461802016-01-26 18:06:34 +00001158 s2cr = S2CR_TYPE_TRANS | S2CR_PRIVCFG_UNPRIV |
Will Deacon44680ee2014-06-25 11:29:12 +01001159 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001160 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1161 }
1162
1163 return 0;
1164}
1165
1166static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001167 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001168{
Will Deacon43b412b2014-07-15 11:22:24 +01001169 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001170 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001171 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001172
Will Deacon8f68f8e2014-07-15 11:27:08 +01001173 /* An IOMMU group is torn down by the first device to be removed */
1174 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1175 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001176
1177 /*
1178 * We *must* clear the S2CR first, because freeing the SMR means
1179 * that it can be re-allocated immediately.
1180 */
Will Deacon43b412b2014-07-15 11:22:24 +01001181 for (i = 0; i < cfg->num_streamids; ++i) {
1182 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Robin Murphy25a1c962016-02-10 14:25:33 +00001183 u32 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
Will Deacon43b412b2014-07-15 11:22:24 +01001184
Robin Murphy25a1c962016-02-10 14:25:33 +00001185 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(idx));
Will Deacon43b412b2014-07-15 11:22:24 +01001186 }
1187
Will Deacona9a1b0b2014-05-01 18:05:08 +01001188 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001189}
1190
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001191static void arm_smmu_detach_dev(struct device *dev,
1192 struct arm_smmu_master_cfg *cfg)
1193{
1194 struct iommu_domain *domain = dev->archdata.iommu;
1195 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1196
1197 dev->archdata.iommu = NULL;
1198 arm_smmu_domain_remove_master(smmu_domain, cfg);
1199}
1200
Will Deacon45ae7cf2013-06-24 18:31:25 +01001201static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1202{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001203 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001204 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001205 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001206 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001207
Will Deacon8f68f8e2014-07-15 11:27:08 +01001208 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001209 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001210 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1211 return -ENXIO;
1212 }
1213
Will Deacon518f7132014-11-14 17:17:54 +00001214 /* Ensure that the domain is finalised */
1215 ret = arm_smmu_init_domain_context(domain, smmu);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001216 if (ret < 0)
Will Deacon518f7132014-11-14 17:17:54 +00001217 return ret;
1218
Will Deacon45ae7cf2013-06-24 18:31:25 +01001219 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001220 * Sanity check the domain. We don't support domains across
1221 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001222 */
Will Deacon518f7132014-11-14 17:17:54 +00001223 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001224 dev_err(dev,
1225 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001226 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1227 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001228 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001229
1230 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001231 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001232 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001233 return -ENODEV;
1234
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001235 /* Detach the dev from its current domain */
1236 if (dev->archdata.iommu)
1237 arm_smmu_detach_dev(dev, cfg);
1238
Will Deacon844e35b2014-07-17 11:23:51 +01001239 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1240 if (!ret)
1241 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001242 return ret;
1243}
1244
Will Deacon45ae7cf2013-06-24 18:31:25 +01001245static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001246 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001247{
Will Deacon518f7132014-11-14 17:17:54 +00001248 int ret;
1249 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001250 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001251 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001252
Will Deacon518f7132014-11-14 17:17:54 +00001253 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001254 return -ENODEV;
1255
Will Deacon518f7132014-11-14 17:17:54 +00001256 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1257 ret = ops->map(ops, iova, paddr, size, prot);
1258 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1259 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001260}
1261
1262static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1263 size_t size)
1264{
Will Deacon518f7132014-11-14 17:17:54 +00001265 size_t ret;
1266 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001267 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001268 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001269
Will Deacon518f7132014-11-14 17:17:54 +00001270 if (!ops)
1271 return 0;
1272
1273 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1274 ret = ops->unmap(ops, iova, size);
1275 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1276 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001277}
1278
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001279static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1280 dma_addr_t iova)
1281{
Joerg Roedel1d672632015-03-26 13:43:10 +01001282 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001283 struct arm_smmu_device *smmu = smmu_domain->smmu;
1284 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1285 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1286 struct device *dev = smmu->dev;
1287 void __iomem *cb_base;
1288 u32 tmp;
1289 u64 phys;
Robin Murphy661d9622015-05-27 17:09:34 +01001290 unsigned long va;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001291
1292 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1293
Robin Murphy661d9622015-05-27 17:09:34 +01001294 /* ATS1 registers can only be written atomically */
1295 va = iova & ~0xfffUL;
Robin Murphy661d9622015-05-27 17:09:34 +01001296 if (smmu->version == ARM_SMMU_V2)
Robin Murphyf9a05f02016-04-13 18:13:01 +01001297 smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
1298 else /* Register is only 32-bit in v1 */
Robin Murphy661d9622015-05-27 17:09:34 +01001299 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001300
1301 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1302 !(tmp & ATSR_ACTIVE), 5, 50)) {
1303 dev_err(dev,
Fabio Estevam077124c2015-08-18 17:12:24 +01001304 "iova to phys timed out on %pad. Falling back to software table walk.\n",
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001305 &iova);
1306 return ops->iova_to_phys(ops, iova);
1307 }
1308
Robin Murphyf9a05f02016-04-13 18:13:01 +01001309 phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001310 if (phys & CB_PAR_F) {
1311 dev_err(dev, "translation fault!\n");
1312 dev_err(dev, "PAR = 0x%llx\n", phys);
1313 return 0;
1314 }
1315
1316 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1317}
1318
Will Deacon45ae7cf2013-06-24 18:31:25 +01001319static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001320 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001321{
Will Deacon518f7132014-11-14 17:17:54 +00001322 phys_addr_t ret;
1323 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001324 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001325 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001326
Will Deacon518f7132014-11-14 17:17:54 +00001327 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001328 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001329
Will Deacon518f7132014-11-14 17:17:54 +00001330 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001331 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1332 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001333 ret = arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001334 } else {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001335 ret = ops->iova_to_phys(ops, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001336 }
1337
Will Deacon518f7132014-11-14 17:17:54 +00001338 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001339
Will Deacon518f7132014-11-14 17:17:54 +00001340 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001341}
1342
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001343static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001344{
Will Deacond0948942014-06-24 17:30:10 +01001345 switch (cap) {
1346 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001347 /*
1348 * Return true here as the SMMU can always send out coherent
1349 * requests.
1350 */
1351 return true;
Will Deacond0948942014-06-24 17:30:10 +01001352 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001353 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001354 case IOMMU_CAP_NOEXEC:
1355 return true;
Will Deacond0948942014-06-24 17:30:10 +01001356 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001357 return false;
Will Deacond0948942014-06-24 17:30:10 +01001358 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001359}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001360
Will Deacona9a1b0b2014-05-01 18:05:08 +01001361static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1362{
1363 *((u16 *)data) = alias;
1364 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001365}
1366
Will Deacon8f68f8e2014-07-15 11:27:08 +01001367static void __arm_smmu_release_pci_iommudata(void *data)
1368{
1369 kfree(data);
1370}
1371
Joerg Roedelaf659932015-10-21 23:51:41 +02001372static int arm_smmu_init_pci_device(struct pci_dev *pdev,
1373 struct iommu_group *group)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001374{
Will Deacon03edb222015-01-19 14:27:33 +00001375 struct arm_smmu_master_cfg *cfg;
Joerg Roedelaf659932015-10-21 23:51:41 +02001376 u16 sid;
1377 int i;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001378
Will Deacon03edb222015-01-19 14:27:33 +00001379 cfg = iommu_group_get_iommudata(group);
1380 if (!cfg) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001381 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001382 if (!cfg)
1383 return -ENOMEM;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001384
Will Deacon03edb222015-01-19 14:27:33 +00001385 iommu_group_set_iommudata(group, cfg,
1386 __arm_smmu_release_pci_iommudata);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001387 }
1388
Joerg Roedelaf659932015-10-21 23:51:41 +02001389 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS)
1390 return -ENOSPC;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001391
Will Deacon03edb222015-01-19 14:27:33 +00001392 /*
1393 * Assume Stream ID == Requester ID for now.
1394 * We need a way to describe the ID mappings in FDT.
1395 */
1396 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1397 for (i = 0; i < cfg->num_streamids; ++i)
1398 if (cfg->streamids[i] == sid)
1399 break;
1400
1401 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1402 if (i == cfg->num_streamids)
1403 cfg->streamids[cfg->num_streamids++] = sid;
1404
1405 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001406}
1407
Joerg Roedelaf659932015-10-21 23:51:41 +02001408static int arm_smmu_init_platform_device(struct device *dev,
1409 struct iommu_group *group)
Will Deacon03edb222015-01-19 14:27:33 +00001410{
Will Deacon03edb222015-01-19 14:27:33 +00001411 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
Joerg Roedelaf659932015-10-21 23:51:41 +02001412 struct arm_smmu_master *master;
Will Deacon03edb222015-01-19 14:27:33 +00001413
1414 if (!smmu)
1415 return -ENODEV;
1416
1417 master = find_smmu_master(smmu, dev->of_node);
1418 if (!master)
1419 return -ENODEV;
1420
Will Deacon03edb222015-01-19 14:27:33 +00001421 iommu_group_set_iommudata(group, &master->cfg, NULL);
Joerg Roedelaf659932015-10-21 23:51:41 +02001422
1423 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001424}
1425
1426static int arm_smmu_add_device(struct device *dev)
1427{
Joerg Roedelaf659932015-10-21 23:51:41 +02001428 struct iommu_group *group;
Will Deacon03edb222015-01-19 14:27:33 +00001429
Joerg Roedelaf659932015-10-21 23:51:41 +02001430 group = iommu_group_get_for_dev(dev);
1431 if (IS_ERR(group))
1432 return PTR_ERR(group);
1433
Peng Fan9a4a9d82015-11-20 16:56:18 +08001434 iommu_group_put(group);
Joerg Roedelaf659932015-10-21 23:51:41 +02001435 return 0;
Will Deacon03edb222015-01-19 14:27:33 +00001436}
1437
Will Deacon45ae7cf2013-06-24 18:31:25 +01001438static void arm_smmu_remove_device(struct device *dev)
1439{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001440 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001441}
1442
Joerg Roedelaf659932015-10-21 23:51:41 +02001443static struct iommu_group *arm_smmu_device_group(struct device *dev)
1444{
1445 struct iommu_group *group;
1446 int ret;
1447
1448 if (dev_is_pci(dev))
1449 group = pci_device_group(dev);
1450 else
1451 group = generic_device_group(dev);
1452
1453 if (IS_ERR(group))
1454 return group;
1455
1456 if (dev_is_pci(dev))
1457 ret = arm_smmu_init_pci_device(to_pci_dev(dev), group);
1458 else
1459 ret = arm_smmu_init_platform_device(dev, group);
1460
1461 if (ret) {
1462 iommu_group_put(group);
1463 group = ERR_PTR(ret);
1464 }
1465
1466 return group;
1467}
1468
Will Deaconc752ce42014-06-25 22:46:31 +01001469static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1470 enum iommu_attr attr, void *data)
1471{
Joerg Roedel1d672632015-03-26 13:43:10 +01001472 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001473
1474 switch (attr) {
1475 case DOMAIN_ATTR_NESTING:
1476 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1477 return 0;
1478 default:
1479 return -ENODEV;
1480 }
1481}
1482
1483static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1484 enum iommu_attr attr, void *data)
1485{
Will Deacon518f7132014-11-14 17:17:54 +00001486 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001487 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001488
Will Deacon518f7132014-11-14 17:17:54 +00001489 mutex_lock(&smmu_domain->init_mutex);
1490
Will Deaconc752ce42014-06-25 22:46:31 +01001491 switch (attr) {
1492 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001493 if (smmu_domain->smmu) {
1494 ret = -EPERM;
1495 goto out_unlock;
1496 }
1497
Will Deaconc752ce42014-06-25 22:46:31 +01001498 if (*(int *)data)
1499 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1500 else
1501 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1502
Will Deacon518f7132014-11-14 17:17:54 +00001503 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001504 default:
Will Deacon518f7132014-11-14 17:17:54 +00001505 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001506 }
Will Deacon518f7132014-11-14 17:17:54 +00001507
1508out_unlock:
1509 mutex_unlock(&smmu_domain->init_mutex);
1510 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001511}
1512
Will Deacon518f7132014-11-14 17:17:54 +00001513static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001514 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001515 .domain_alloc = arm_smmu_domain_alloc,
1516 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001517 .attach_dev = arm_smmu_attach_dev,
Will Deaconc752ce42014-06-25 22:46:31 +01001518 .map = arm_smmu_map,
1519 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001520 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001521 .iova_to_phys = arm_smmu_iova_to_phys,
1522 .add_device = arm_smmu_add_device,
1523 .remove_device = arm_smmu_remove_device,
Joerg Roedelaf659932015-10-21 23:51:41 +02001524 .device_group = arm_smmu_device_group,
Will Deaconc752ce42014-06-25 22:46:31 +01001525 .domain_get_attr = arm_smmu_domain_get_attr,
1526 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001527 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001528};
1529
1530static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1531{
1532 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001533 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001534 int i = 0;
Peng Fan3ca37122016-05-03 21:50:30 +08001535 u32 reg, major;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001536
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001537 /* clear global FSR */
1538 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1539 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001540
Robin Murphy25a1c962016-02-10 14:25:33 +00001541 /* Mark all SMRn as invalid and all S2CRn as bypass unless overridden */
1542 reg = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001543 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001544 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Robin Murphy25a1c962016-02-10 14:25:33 +00001545 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001546 }
1547
Peng Fan3ca37122016-05-03 21:50:30 +08001548 /*
1549 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1550 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1551 * bit is only present in MMU-500r2 onwards.
1552 */
1553 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
1554 major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
1555 if ((smmu->model == ARM_MMU500) && (major >= 2)) {
1556 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
1557 reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
1558 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
1559 }
1560
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001561 /* Make sure all context banks are disabled and clear CB_FSR */
1562 for (i = 0; i < smmu->num_context_banks; ++i) {
1563 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1564 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1565 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
Robin Murphyf0cfffc2016-04-13 18:12:59 +01001566 /*
1567 * Disable MMU-500's not-particularly-beneficial next-page
1568 * prefetcher for the sake of errata #841119 and #826419.
1569 */
1570 if (smmu->model == ARM_MMU500) {
1571 reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
1572 reg &= ~ARM_MMU500_ACTLR_CPRE;
1573 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
1574 }
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001575 }
Will Deacon1463fe42013-07-31 19:21:27 +01001576
Will Deacon45ae7cf2013-06-24 18:31:25 +01001577 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001578 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1579 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1580
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001581 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001582
Will Deacon45ae7cf2013-06-24 18:31:25 +01001583 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001584 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001585
1586 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001587 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001588
Robin Murphy25a1c962016-02-10 14:25:33 +00001589 /* Enable client access, handling unmatched streams as appropriate */
1590 reg &= ~sCR0_CLIENTPD;
1591 if (disable_bypass)
1592 reg |= sCR0_USFCFG;
1593 else
1594 reg &= ~sCR0_USFCFG;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001595
1596 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001597 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001598
1599 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001600 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001601
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -08001602 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1603 reg |= sCR0_VMID16EN;
1604
Will Deacon45ae7cf2013-06-24 18:31:25 +01001605 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001606 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001607 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001608}
1609
1610static int arm_smmu_id_size_to_bits(int size)
1611{
1612 switch (size) {
1613 case 0:
1614 return 32;
1615 case 1:
1616 return 36;
1617 case 2:
1618 return 40;
1619 case 3:
1620 return 42;
1621 case 4:
1622 return 44;
1623 case 5:
1624 default:
1625 return 48;
1626 }
1627}
1628
1629static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1630{
1631 unsigned long size;
1632 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1633 u32 id;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001634 bool cttw_dt, cttw_reg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001635
1636 dev_notice(smmu->dev, "probing hardware configuration...\n");
Robin Murphyb7862e32016-04-13 18:13:03 +01001637 dev_notice(smmu->dev, "SMMUv%d with:\n",
1638 smmu->version == ARM_SMMU_V2 ? 2 : 1);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001639
1640 /* ID0 */
1641 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001642
1643 /* Restrict available stages based on module parameter */
1644 if (force_stage == 1)
1645 id &= ~(ID0_S2TS | ID0_NTS);
1646 else if (force_stage == 2)
1647 id &= ~(ID0_S1TS | ID0_NTS);
1648
Will Deacon45ae7cf2013-06-24 18:31:25 +01001649 if (id & ID0_S1TS) {
1650 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1651 dev_notice(smmu->dev, "\tstage 1 translation\n");
1652 }
1653
1654 if (id & ID0_S2TS) {
1655 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1656 dev_notice(smmu->dev, "\tstage 2 translation\n");
1657 }
1658
1659 if (id & ID0_NTS) {
1660 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1661 dev_notice(smmu->dev, "\tnested translation\n");
1662 }
1663
1664 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001665 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001666 dev_err(smmu->dev, "\tno translation support!\n");
1667 return -ENODEV;
1668 }
1669
Robin Murphyb7862e32016-04-13 18:13:03 +01001670 if ((id & ID0_S1TS) &&
1671 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001672 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1673 dev_notice(smmu->dev, "\taddress translation ops\n");
1674 }
1675
Robin Murphybae2c2d2015-07-29 19:46:05 +01001676 /*
1677 * In order for DMA API calls to work properly, we must defer to what
1678 * the DT says about coherency, regardless of what the hardware claims.
1679 * Fortunately, this also opens up a workaround for systems where the
1680 * ID register value has ended up configured incorrectly.
1681 */
1682 cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
1683 cttw_reg = !!(id & ID0_CTTW);
1684 if (cttw_dt)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001685 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001686 if (cttw_dt || cttw_reg)
1687 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1688 cttw_dt ? "" : "non-");
1689 if (cttw_dt != cttw_reg)
1690 dev_notice(smmu->dev,
1691 "\t(IDR0.CTTW overridden by dma-coherent property)\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001692
1693 if (id & ID0_SMS) {
1694 u32 smr, sid, mask;
1695
1696 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1697 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1698 ID0_NUMSMRG_MASK;
1699 if (smmu->num_mapping_groups == 0) {
1700 dev_err(smmu->dev,
1701 "stream-matching supported, but no SMRs present!\n");
1702 return -ENODEV;
1703 }
1704
1705 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1706 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1707 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1708 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1709
1710 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1711 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1712 if ((mask & sid) != sid) {
1713 dev_err(smmu->dev,
1714 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1715 mask, sid);
1716 return -ENODEV;
1717 }
1718
1719 dev_notice(smmu->dev,
1720 "\tstream matching with %u register groups, mask 0x%x",
1721 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001722 } else {
1723 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1724 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001725 }
1726
Robin Murphy7602b872016-04-28 17:12:09 +01001727 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1728 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1729 if (!(id & ID0_PTFS_NO_AARCH32S))
1730 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1731 }
1732
Will Deacon45ae7cf2013-06-24 18:31:25 +01001733 /* ID1 */
1734 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001735 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001736
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001737 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001738 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001739 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001740 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001741 dev_warn(smmu->dev,
1742 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1743 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001744
Will Deacon518f7132014-11-14 17:17:54 +00001745 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001746 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1747 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1748 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1749 return -ENODEV;
1750 }
1751 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1752 smmu->num_context_banks, smmu->num_s2_context_banks);
Robin Murphye086d912016-04-13 18:12:58 +01001753 /*
1754 * Cavium CN88xx erratum #27704.
1755 * Ensure ASID and VMID allocation is unique across all SMMUs in
1756 * the system.
1757 */
1758 if (smmu->model == CAVIUM_SMMUV2) {
1759 smmu->cavium_id_base =
1760 atomic_add_return(smmu->num_context_banks,
1761 &cavium_smmu_context_count);
1762 smmu->cavium_id_base -= smmu->num_context_banks;
1763 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001764
1765 /* ID2 */
1766 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1767 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001768 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001769
Will Deacon518f7132014-11-14 17:17:54 +00001770 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001771 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001772 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001773
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -08001774 if (id & ID2_VMID16)
1775 smmu->features |= ARM_SMMU_FEAT_VMID16;
1776
Robin Murphyf1d84542015-03-04 16:41:05 +00001777 /*
1778 * What the page table walker can address actually depends on which
1779 * descriptor format is in use, but since a) we don't know that yet,
1780 * and b) it can vary per context bank, this will have to do...
1781 */
1782 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1783 dev_warn(smmu->dev,
1784 "failed to set DMA mask for table walker\n");
1785
Robin Murphyb7862e32016-04-13 18:13:03 +01001786 if (smmu->version < ARM_SMMU_V2) {
Will Deacon518f7132014-11-14 17:17:54 +00001787 smmu->va_size = smmu->ipa_size;
Robin Murphyb7862e32016-04-13 18:13:03 +01001788 if (smmu->version == ARM_SMMU_V1_64K)
1789 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001790 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001791 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001792 smmu->va_size = arm_smmu_id_size_to_bits(size);
Will Deacon518f7132014-11-14 17:17:54 +00001793 if (id & ID2_PTFS_4K)
Robin Murphy7602b872016-04-28 17:12:09 +01001794 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
Will Deacon518f7132014-11-14 17:17:54 +00001795 if (id & ID2_PTFS_16K)
Robin Murphy7602b872016-04-28 17:12:09 +01001796 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
Will Deacon518f7132014-11-14 17:17:54 +00001797 if (id & ID2_PTFS_64K)
Robin Murphy7602b872016-04-28 17:12:09 +01001798 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001799 }
1800
Robin Murphy7602b872016-04-28 17:12:09 +01001801 /* Now we've corralled the various formats, what'll it do? */
Robin Murphy7602b872016-04-28 17:12:09 +01001802 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
Robin Murphyd5466352016-05-09 17:20:09 +01001803 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
Robin Murphy7602b872016-04-28 17:12:09 +01001804 if (smmu->features &
1805 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
Robin Murphyd5466352016-05-09 17:20:09 +01001806 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
Robin Murphy7602b872016-04-28 17:12:09 +01001807 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
Robin Murphyd5466352016-05-09 17:20:09 +01001808 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
Robin Murphy7602b872016-04-28 17:12:09 +01001809 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
Robin Murphyd5466352016-05-09 17:20:09 +01001810 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
Robin Murphy7602b872016-04-28 17:12:09 +01001811
Robin Murphyd5466352016-05-09 17:20:09 +01001812 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1813 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1814 else
1815 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1816 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1817 smmu->pgsize_bitmap);
1818
Will Deacon518f7132014-11-14 17:17:54 +00001819
Will Deacon28d60072014-09-01 16:24:48 +01001820 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1821 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001822 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001823
1824 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1825 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001826 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001827
Will Deacon45ae7cf2013-06-24 18:31:25 +01001828 return 0;
1829}
1830
Robin Murphy67b65a32016-04-13 18:12:57 +01001831struct arm_smmu_match_data {
1832 enum arm_smmu_arch_version version;
1833 enum arm_smmu_implementation model;
1834};
1835
1836#define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1837static struct arm_smmu_match_data name = { .version = ver, .model = imp }
1838
1839ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1840ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
Robin Murphyb7862e32016-04-13 18:13:03 +01001841ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
Robin Murphyf0cfffc2016-04-13 18:12:59 +01001842ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
Robin Murphye086d912016-04-13 18:12:58 +01001843ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
Robin Murphy67b65a32016-04-13 18:12:57 +01001844
Joerg Roedel09b52692014-10-02 12:24:45 +02001845static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy67b65a32016-04-13 18:12:57 +01001846 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1847 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1848 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
Robin Murphyb7862e32016-04-13 18:13:03 +01001849 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
Robin Murphyf0cfffc2016-04-13 18:12:59 +01001850 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
Robin Murphye086d912016-04-13 18:12:58 +01001851 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
Robin Murphy09360402014-08-28 17:51:59 +01001852 { },
1853};
1854MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1855
Will Deacon45ae7cf2013-06-24 18:31:25 +01001856static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1857{
Robin Murphy09360402014-08-28 17:51:59 +01001858 const struct of_device_id *of_id;
Robin Murphy67b65a32016-04-13 18:12:57 +01001859 const struct arm_smmu_match_data *data;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001860 struct resource *res;
1861 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001862 struct device *dev = &pdev->dev;
1863 struct rb_node *node;
Joerg Roedelcb6c27b2016-04-04 17:49:22 +02001864 struct of_phandle_iterator it;
1865 struct arm_smmu_phandle_args *masterspec;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001866 int num_irqs, i, err;
1867
1868 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1869 if (!smmu) {
1870 dev_err(dev, "failed to allocate arm_smmu_device\n");
1871 return -ENOMEM;
1872 }
1873 smmu->dev = dev;
1874
Robin Murphy09360402014-08-28 17:51:59 +01001875 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
Robin Murphy67b65a32016-04-13 18:12:57 +01001876 data = of_id->data;
1877 smmu->version = data->version;
1878 smmu->model = data->model;
Robin Murphy09360402014-08-28 17:51:59 +01001879
Will Deacon45ae7cf2013-06-24 18:31:25 +01001880 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001881 smmu->base = devm_ioremap_resource(dev, res);
1882 if (IS_ERR(smmu->base))
1883 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001884 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001885
1886 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1887 &smmu->num_global_irqs)) {
1888 dev_err(dev, "missing #global-interrupts property\n");
1889 return -ENODEV;
1890 }
1891
1892 num_irqs = 0;
1893 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1894 num_irqs++;
1895 if (num_irqs > smmu->num_global_irqs)
1896 smmu->num_context_irqs++;
1897 }
1898
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001899 if (!smmu->num_context_irqs) {
1900 dev_err(dev, "found %d interrupts but expected at least %d\n",
1901 num_irqs, smmu->num_global_irqs + 1);
1902 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001903 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001904
1905 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1906 GFP_KERNEL);
1907 if (!smmu->irqs) {
1908 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1909 return -ENOMEM;
1910 }
1911
1912 for (i = 0; i < num_irqs; ++i) {
1913 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001914
Will Deacon45ae7cf2013-06-24 18:31:25 +01001915 if (irq < 0) {
1916 dev_err(dev, "failed to get irq index %d\n", i);
1917 return -ENODEV;
1918 }
1919 smmu->irqs[i] = irq;
1920 }
1921
Olav Haugan3c8766d2014-08-22 17:12:32 -07001922 err = arm_smmu_device_cfg_probe(smmu);
1923 if (err)
1924 return err;
1925
Will Deacon45ae7cf2013-06-24 18:31:25 +01001926 i = 0;
1927 smmu->masters = RB_ROOT;
Joerg Roedelcb6c27b2016-04-04 17:49:22 +02001928
1929 err = -ENOMEM;
1930 /* No need to zero the memory for masterspec */
1931 masterspec = kmalloc(sizeof(*masterspec), GFP_KERNEL);
1932 if (!masterspec)
1933 goto out_put_masters;
1934
1935 of_for_each_phandle(&it, err, dev->of_node,
1936 "mmu-masters", "#stream-id-cells", 0) {
1937 int count = of_phandle_iterator_args(&it, masterspec->args,
1938 MAX_MASTER_STREAMIDS);
1939 masterspec->np = of_node_get(it.node);
1940 masterspec->args_count = count;
1941
1942 err = register_smmu_master(smmu, dev, masterspec);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001943 if (err) {
1944 dev_err(dev, "failed to add master %s\n",
Joerg Roedelcb6c27b2016-04-04 17:49:22 +02001945 masterspec->np->name);
1946 kfree(masterspec);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001947 goto out_put_masters;
1948 }
1949
1950 i++;
1951 }
Joerg Roedelcb6c27b2016-04-04 17:49:22 +02001952
Will Deacon45ae7cf2013-06-24 18:31:25 +01001953 dev_notice(dev, "registered %d master devices\n", i);
1954
Joerg Roedelcb6c27b2016-04-04 17:49:22 +02001955 kfree(masterspec);
1956
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001957 parse_driver_options(smmu);
1958
Robin Murphyb7862e32016-04-13 18:13:03 +01001959 if (smmu->version == ARM_SMMU_V2 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001960 smmu->num_context_banks != smmu->num_context_irqs) {
1961 dev_err(dev,
1962 "found only %d context interrupt(s) but %d required\n",
1963 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001964 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001965 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001966 }
1967
Will Deacon45ae7cf2013-06-24 18:31:25 +01001968 for (i = 0; i < smmu->num_global_irqs; ++i) {
Peng Fanbee14002016-07-04 17:38:22 +08001969 err = devm_request_irq(smmu->dev, smmu->irqs[i],
1970 arm_smmu_global_fault,
1971 IRQF_SHARED,
1972 "arm-smmu global fault",
1973 smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001974 if (err) {
1975 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1976 i, smmu->irqs[i]);
Peng Fanbee14002016-07-04 17:38:22 +08001977 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001978 }
1979 }
1980
1981 INIT_LIST_HEAD(&smmu->list);
1982 spin_lock(&arm_smmu_devices_lock);
1983 list_add(&smmu->list, &arm_smmu_devices);
1984 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001985
1986 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001987 return 0;
1988
Will Deacon45ae7cf2013-06-24 18:31:25 +01001989out_put_masters:
1990 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001991 struct arm_smmu_master *master
1992 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001993 of_node_put(master->of_node);
1994 }
1995
1996 return err;
1997}
1998
1999static int arm_smmu_device_remove(struct platform_device *pdev)
2000{
2001 int i;
2002 struct device *dev = &pdev->dev;
2003 struct arm_smmu_device *curr, *smmu = NULL;
2004 struct rb_node *node;
2005
2006 spin_lock(&arm_smmu_devices_lock);
2007 list_for_each_entry(curr, &arm_smmu_devices, list) {
2008 if (curr->dev == dev) {
2009 smmu = curr;
2010 list_del(&smmu->list);
2011 break;
2012 }
2013 }
2014 spin_unlock(&arm_smmu_devices_lock);
2015
2016 if (!smmu)
2017 return -ENODEV;
2018
Will Deacon45ae7cf2013-06-24 18:31:25 +01002019 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07002020 struct arm_smmu_master *master
2021 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002022 of_node_put(master->of_node);
2023 }
2024
Will Deaconecfadb62013-07-31 19:21:28 +01002025 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002026 dev_err(dev, "removing device with active domains!\n");
2027
2028 for (i = 0; i < smmu->num_global_irqs; ++i)
Peng Fanbee14002016-07-04 17:38:22 +08002029 devm_free_irq(smmu->dev, smmu->irqs[i], smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002030
2031 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07002032 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002033 return 0;
2034}
2035
Will Deacon45ae7cf2013-06-24 18:31:25 +01002036static struct platform_driver arm_smmu_driver = {
2037 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01002038 .name = "arm-smmu",
2039 .of_match_table = of_match_ptr(arm_smmu_of_match),
2040 },
2041 .probe = arm_smmu_device_dt_probe,
2042 .remove = arm_smmu_device_remove,
2043};
2044
2045static int __init arm_smmu_init(void)
2046{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00002047 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002048 int ret;
2049
Thierry Reding0e7d37a2014-11-07 15:26:18 +00002050 /*
2051 * Play nice with systems that don't have an ARM SMMU by checking that
2052 * an ARM SMMU exists in the system before proceeding with the driver
2053 * and IOMMU bus operation registration.
2054 */
2055 np = of_find_matching_node(NULL, arm_smmu_of_match);
2056 if (!np)
2057 return 0;
2058
2059 of_node_put(np);
2060
Will Deacon45ae7cf2013-06-24 18:31:25 +01002061 ret = platform_driver_register(&arm_smmu_driver);
2062 if (ret)
2063 return ret;
2064
2065 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01002066 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002067 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2068
Will Deacond123cf82014-02-04 22:17:53 +00002069#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01002070 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01002071 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00002072#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01002073
Will Deacona9a1b0b2014-05-01 18:05:08 +01002074#ifdef CONFIG_PCI
Wei Chen112c8982016-06-13 17:20:17 +08002075 if (!iommu_present(&pci_bus_type)) {
2076 pci_request_acs();
Will Deacona9a1b0b2014-05-01 18:05:08 +01002077 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
Wei Chen112c8982016-06-13 17:20:17 +08002078 }
Will Deacona9a1b0b2014-05-01 18:05:08 +01002079#endif
2080
Will Deacon45ae7cf2013-06-24 18:31:25 +01002081 return 0;
2082}
2083
2084static void __exit arm_smmu_exit(void)
2085{
2086 return platform_driver_unregister(&arm_smmu_driver);
2087}
2088
Andreas Herrmannb1950b22013-10-01 13:39:05 +01002089subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002090module_exit(arm_smmu_exit);
2091
2092MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2093MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2094MODULE_LICENSE("GPL v2");