blob: 677fba9e6b11e33c2fcbaa41e15383f313ae14f6 [file] [log] [blame]
Will Deacon45ae7cf2013-06-24 18:31:25 +01001/*
2 * IOMMU API for ARM architected SMMU implementations.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 *
17 * Copyright (C) 2013 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 *
21 * This driver currently supports:
22 * - SMMUv1 and v2 implementations
23 * - Stream-matching and stream-indexing
24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU
Will Deacon45ae7cf2013-06-24 18:31:25 +010026 * - Context fault reporting
27 */
28
29#define pr_fmt(fmt) "arm-smmu: " fmt
30
31#include <linux/delay.h>
32#include <linux/dma-mapping.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/io.h>
36#include <linux/iommu.h>
Mitchel Humpherys859a7322014-10-29 21:13:40 +000037#include <linux/iopoll.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010038#include <linux/module.h>
39#include <linux/of.h>
Robin Murphybae2c2d2015-07-29 19:46:05 +010040#include <linux/of_address.h>
Will Deacona9a1b0b2014-05-01 18:05:08 +010041#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010042#include <linux/platform_device.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h>
45
46#include <linux/amba/bus.h>
47
Will Deacon518f7132014-11-14 17:17:54 +000048#include "io-pgtable.h"
Will Deacon45ae7cf2013-06-24 18:31:25 +010049
50/* Maximum number of stream IDs assigned to a single device */
Andreas Herrmann636e97b2014-01-30 18:18:08 +000051#define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
Will Deacon45ae7cf2013-06-24 18:31:25 +010052
53/* Maximum number of context banks per SMMU */
54#define ARM_SMMU_MAX_CBS 128
55
56/* Maximum number of mapping groups per SMMU */
57#define ARM_SMMU_MAX_SMRS 128
58
Will Deacon45ae7cf2013-06-24 18:31:25 +010059/* SMMU global address space */
60#define ARM_SMMU_GR0(smmu) ((smmu)->base)
Will Deaconc757e852014-07-30 11:33:25 +010061#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +010062
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +000063/*
64 * SMMU global address space with conditional offset to access secure
65 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
66 * nsGFSYNR0: 0x450)
67 */
68#define ARM_SMMU_GR0_NS(smmu) \
69 ((smmu)->base + \
70 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
71 ? 0x400 : 0))
72
Will Deacon45ae7cf2013-06-24 18:31:25 +010073/* Configuration registers */
74#define ARM_SMMU_GR0_sCR0 0x0
75#define sCR0_CLIENTPD (1 << 0)
76#define sCR0_GFRE (1 << 1)
77#define sCR0_GFIE (1 << 2)
78#define sCR0_GCFGFRE (1 << 4)
79#define sCR0_GCFGFIE (1 << 5)
80#define sCR0_USFCFG (1 << 10)
81#define sCR0_VMIDPNE (1 << 11)
82#define sCR0_PTM (1 << 12)
83#define sCR0_FB (1 << 13)
84#define sCR0_BSU_SHIFT 14
85#define sCR0_BSU_MASK 0x3
86
87/* Identification registers */
88#define ARM_SMMU_GR0_ID0 0x20
89#define ARM_SMMU_GR0_ID1 0x24
90#define ARM_SMMU_GR0_ID2 0x28
91#define ARM_SMMU_GR0_ID3 0x2c
92#define ARM_SMMU_GR0_ID4 0x30
93#define ARM_SMMU_GR0_ID5 0x34
94#define ARM_SMMU_GR0_ID6 0x38
95#define ARM_SMMU_GR0_ID7 0x3c
96#define ARM_SMMU_GR0_sGFSR 0x48
97#define ARM_SMMU_GR0_sGFSYNR0 0x50
98#define ARM_SMMU_GR0_sGFSYNR1 0x54
99#define ARM_SMMU_GR0_sGFSYNR2 0x58
Will Deacon45ae7cf2013-06-24 18:31:25 +0100100
101#define ID0_S1TS (1 << 30)
102#define ID0_S2TS (1 << 29)
103#define ID0_NTS (1 << 28)
104#define ID0_SMS (1 << 27)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000105#define ID0_ATOSNS (1 << 26)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100106#define ID0_CTTW (1 << 14)
107#define ID0_NUMIRPT_SHIFT 16
108#define ID0_NUMIRPT_MASK 0xff
Olav Haugan3c8766d2014-08-22 17:12:32 -0700109#define ID0_NUMSIDB_SHIFT 9
110#define ID0_NUMSIDB_MASK 0xf
Will Deacon45ae7cf2013-06-24 18:31:25 +0100111#define ID0_NUMSMRG_SHIFT 0
112#define ID0_NUMSMRG_MASK 0xff
113
114#define ID1_PAGESIZE (1 << 31)
115#define ID1_NUMPAGENDXB_SHIFT 28
116#define ID1_NUMPAGENDXB_MASK 7
117#define ID1_NUMS2CB_SHIFT 16
118#define ID1_NUMS2CB_MASK 0xff
119#define ID1_NUMCB_SHIFT 0
120#define ID1_NUMCB_MASK 0xff
121
122#define ID2_OAS_SHIFT 4
123#define ID2_OAS_MASK 0xf
124#define ID2_IAS_SHIFT 0
125#define ID2_IAS_MASK 0xf
126#define ID2_UBS_SHIFT 8
127#define ID2_UBS_MASK 0xf
128#define ID2_PTFS_4K (1 << 12)
129#define ID2_PTFS_16K (1 << 13)
130#define ID2_PTFS_64K (1 << 14)
131
Will Deacon45ae7cf2013-06-24 18:31:25 +0100132/* Global TLB invalidation */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100133#define ARM_SMMU_GR0_TLBIVMID 0x64
134#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
135#define ARM_SMMU_GR0_TLBIALLH 0x6c
136#define ARM_SMMU_GR0_sTLBGSYNC 0x70
137#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
138#define sTLBGSTATUS_GSACTIVE (1 << 0)
139#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
140
141/* Stream mapping registers */
142#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
143#define SMR_VALID (1 << 31)
144#define SMR_MASK_SHIFT 16
145#define SMR_MASK_MASK 0x7fff
146#define SMR_ID_SHIFT 0
147#define SMR_ID_MASK 0x7fff
148
149#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
150#define S2CR_CBNDX_SHIFT 0
151#define S2CR_CBNDX_MASK 0xff
152#define S2CR_TYPE_SHIFT 16
153#define S2CR_TYPE_MASK 0x3
154#define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
155#define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
156#define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
157
158/* Context bank attribute registers */
159#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
160#define CBAR_VMID_SHIFT 0
161#define CBAR_VMID_MASK 0xff
Will Deacon57ca90f2014-02-06 14:59:05 +0000162#define CBAR_S1_BPSHCFG_SHIFT 8
163#define CBAR_S1_BPSHCFG_MASK 3
164#define CBAR_S1_BPSHCFG_NSH 3
Will Deacon45ae7cf2013-06-24 18:31:25 +0100165#define CBAR_S1_MEMATTR_SHIFT 12
166#define CBAR_S1_MEMATTR_MASK 0xf
167#define CBAR_S1_MEMATTR_WB 0xf
168#define CBAR_TYPE_SHIFT 16
169#define CBAR_TYPE_MASK 0x3
170#define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
171#define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
172#define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
173#define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
174#define CBAR_IRPTNDX_SHIFT 24
175#define CBAR_IRPTNDX_MASK 0xff
176
177#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
178#define CBA2R_RW64_32BIT (0 << 0)
179#define CBA2R_RW64_64BIT (1 << 0)
180
181/* Translation context bank */
182#define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
Will Deaconc757e852014-07-30 11:33:25 +0100183#define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100184
185#define ARM_SMMU_CB_SCTLR 0x0
186#define ARM_SMMU_CB_RESUME 0x8
187#define ARM_SMMU_CB_TTBCR2 0x10
188#define ARM_SMMU_CB_TTBR0_LO 0x20
189#define ARM_SMMU_CB_TTBR0_HI 0x24
Will Deacon518f7132014-11-14 17:17:54 +0000190#define ARM_SMMU_CB_TTBR1_LO 0x28
191#define ARM_SMMU_CB_TTBR1_HI 0x2c
Will Deacon45ae7cf2013-06-24 18:31:25 +0100192#define ARM_SMMU_CB_TTBCR 0x30
193#define ARM_SMMU_CB_S1_MAIR0 0x38
Will Deacon518f7132014-11-14 17:17:54 +0000194#define ARM_SMMU_CB_S1_MAIR1 0x3c
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000195#define ARM_SMMU_CB_PAR_LO 0x50
196#define ARM_SMMU_CB_PAR_HI 0x54
Will Deacon45ae7cf2013-06-24 18:31:25 +0100197#define ARM_SMMU_CB_FSR 0x58
198#define ARM_SMMU_CB_FAR_LO 0x60
199#define ARM_SMMU_CB_FAR_HI 0x64
200#define ARM_SMMU_CB_FSYNR0 0x68
Will Deacon518f7132014-11-14 17:17:54 +0000201#define ARM_SMMU_CB_S1_TLBIVA 0x600
Will Deacon1463fe42013-07-31 19:21:27 +0100202#define ARM_SMMU_CB_S1_TLBIASID 0x610
Will Deacon518f7132014-11-14 17:17:54 +0000203#define ARM_SMMU_CB_S1_TLBIVAL 0x620
204#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
205#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
Robin Murphy661d9622015-05-27 17:09:34 +0100206#define ARM_SMMU_CB_ATS1PR 0x800
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000207#define ARM_SMMU_CB_ATSR 0x8f0
Will Deacon45ae7cf2013-06-24 18:31:25 +0100208
209#define SCTLR_S1_ASIDPNE (1 << 12)
210#define SCTLR_CFCFG (1 << 7)
211#define SCTLR_CFIE (1 << 6)
212#define SCTLR_CFRE (1 << 5)
213#define SCTLR_E (1 << 4)
214#define SCTLR_AFE (1 << 2)
215#define SCTLR_TRE (1 << 1)
216#define SCTLR_M (1 << 0)
217#define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
218
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000219#define CB_PAR_F (1 << 0)
220
221#define ATSR_ACTIVE (1 << 0)
222
Will Deacon45ae7cf2013-06-24 18:31:25 +0100223#define RESUME_RETRY (0 << 0)
224#define RESUME_TERMINATE (1 << 0)
225
Will Deacon45ae7cf2013-06-24 18:31:25 +0100226#define TTBCR2_SEP_SHIFT 15
Will Deacon5dc56162015-05-08 17:44:22 +0100227#define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100228
Will Deacon518f7132014-11-14 17:17:54 +0000229#define TTBRn_HI_ASID_SHIFT 16
Will Deacon45ae7cf2013-06-24 18:31:25 +0100230
231#define FSR_MULTI (1 << 31)
232#define FSR_SS (1 << 30)
233#define FSR_UUT (1 << 8)
234#define FSR_ASF (1 << 7)
235#define FSR_TLBLKF (1 << 6)
236#define FSR_TLBMCF (1 << 5)
237#define FSR_EF (1 << 4)
238#define FSR_PF (1 << 3)
239#define FSR_AFF (1 << 2)
240#define FSR_TF (1 << 1)
241
Mitchel Humpherys29073202014-07-08 09:52:18 -0700242#define FSR_IGN (FSR_AFF | FSR_ASF | \
243 FSR_TLBMCF | FSR_TLBLKF)
244#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
Will Deaconadaba322013-07-31 19:21:26 +0100245 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100246
247#define FSYNR0_WNR (1 << 4)
248
Will Deacon4cf740b2014-07-14 19:47:39 +0100249static int force_stage;
Will Deacone3ce0c92015-05-27 17:09:35 +0100250module_param_named(force_stage, force_stage, int, S_IRUGO);
Will Deacon4cf740b2014-07-14 19:47:39 +0100251MODULE_PARM_DESC(force_stage,
252 "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.");
253
Robin Murphy09360402014-08-28 17:51:59 +0100254enum arm_smmu_arch_version {
255 ARM_SMMU_V1 = 1,
256 ARM_SMMU_V2,
257};
258
Will Deacon45ae7cf2013-06-24 18:31:25 +0100259struct arm_smmu_smr {
260 u8 idx;
261 u16 mask;
262 u16 id;
263};
264
Will Deacona9a1b0b2014-05-01 18:05:08 +0100265struct arm_smmu_master_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100266 int num_streamids;
267 u16 streamids[MAX_MASTER_STREAMIDS];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100268 struct arm_smmu_smr *smrs;
269};
270
Will Deacona9a1b0b2014-05-01 18:05:08 +0100271struct arm_smmu_master {
272 struct device_node *of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100273 struct rb_node node;
274 struct arm_smmu_master_cfg cfg;
275};
276
Will Deacon45ae7cf2013-06-24 18:31:25 +0100277struct arm_smmu_device {
278 struct device *dev;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100279
280 void __iomem *base;
281 unsigned long size;
Will Deaconc757e852014-07-30 11:33:25 +0100282 unsigned long pgshift;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100283
284#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
285#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
286#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
287#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
288#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
Mitchel Humpherys859a7322014-10-29 21:13:40 +0000289#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100290 u32 features;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000291
292#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
293 u32 options;
Robin Murphy09360402014-08-28 17:51:59 +0100294 enum arm_smmu_arch_version version;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100295
296 u32 num_context_banks;
297 u32 num_s2_context_banks;
298 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
299 atomic_t irptndx;
300
301 u32 num_mapping_groups;
302 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
303
Will Deacon518f7132014-11-14 17:17:54 +0000304 unsigned long va_size;
305 unsigned long ipa_size;
306 unsigned long pa_size;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100307
308 u32 num_global_irqs;
309 u32 num_context_irqs;
310 unsigned int *irqs;
311
Will Deacon45ae7cf2013-06-24 18:31:25 +0100312 struct list_head list;
313 struct rb_root masters;
314};
315
316struct arm_smmu_cfg {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100317 u8 cbndx;
318 u8 irptndx;
319 u32 cbar;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100320};
Dan Carpenterfaea13b72013-08-21 09:33:30 +0100321#define INVALID_IRPTNDX 0xff
Will Deacon45ae7cf2013-06-24 18:31:25 +0100322
Will Deaconecfadb62013-07-31 19:21:28 +0100323#define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
324#define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
325
Will Deaconc752ce42014-06-25 22:46:31 +0100326enum arm_smmu_domain_stage {
327 ARM_SMMU_DOMAIN_S1 = 0,
328 ARM_SMMU_DOMAIN_S2,
329 ARM_SMMU_DOMAIN_NESTED,
330};
331
Will Deacon45ae7cf2013-06-24 18:31:25 +0100332struct arm_smmu_domain {
Will Deacon44680ee2014-06-25 11:29:12 +0100333 struct arm_smmu_device *smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000334 struct io_pgtable_ops *pgtbl_ops;
335 spinlock_t pgtbl_lock;
Will Deacon44680ee2014-06-25 11:29:12 +0100336 struct arm_smmu_cfg cfg;
Will Deaconc752ce42014-06-25 22:46:31 +0100337 enum arm_smmu_domain_stage stage;
Will Deacon518f7132014-11-14 17:17:54 +0000338 struct mutex init_mutex; /* Protects smmu pointer */
Joerg Roedel1d672632015-03-26 13:43:10 +0100339 struct iommu_domain domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100340};
341
Will Deacon518f7132014-11-14 17:17:54 +0000342static struct iommu_ops arm_smmu_ops;
343
Will Deacon45ae7cf2013-06-24 18:31:25 +0100344static DEFINE_SPINLOCK(arm_smmu_devices_lock);
345static LIST_HEAD(arm_smmu_devices);
346
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000347struct arm_smmu_option_prop {
348 u32 opt;
349 const char *prop;
350};
351
Mitchel Humpherys29073202014-07-08 09:52:18 -0700352static struct arm_smmu_option_prop arm_smmu_options[] = {
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000353 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
354 { 0, NULL},
355};
356
Joerg Roedel1d672632015-03-26 13:43:10 +0100357static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
358{
359 return container_of(dom, struct arm_smmu_domain, domain);
360}
361
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000362static void parse_driver_options(struct arm_smmu_device *smmu)
363{
364 int i = 0;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700365
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000366 do {
367 if (of_property_read_bool(smmu->dev->of_node,
368 arm_smmu_options[i].prop)) {
369 smmu->options |= arm_smmu_options[i].opt;
370 dev_notice(smmu->dev, "option %s\n",
371 arm_smmu_options[i].prop);
372 }
373 } while (arm_smmu_options[++i].opt);
374}
375
Will Deacon8f68f8e2014-07-15 11:27:08 +0100376static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100377{
378 if (dev_is_pci(dev)) {
379 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700380
Will Deacona9a1b0b2014-05-01 18:05:08 +0100381 while (!pci_is_root_bus(bus))
382 bus = bus->parent;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100383 return bus->bridge->parent->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100384 }
385
Will Deacon8f68f8e2014-07-15 11:27:08 +0100386 return dev->of_node;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100387}
388
Will Deacon45ae7cf2013-06-24 18:31:25 +0100389static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
390 struct device_node *dev_node)
391{
392 struct rb_node *node = smmu->masters.rb_node;
393
394 while (node) {
395 struct arm_smmu_master *master;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700396
Will Deacon45ae7cf2013-06-24 18:31:25 +0100397 master = container_of(node, struct arm_smmu_master, node);
398
399 if (dev_node < master->of_node)
400 node = node->rb_left;
401 else if (dev_node > master->of_node)
402 node = node->rb_right;
403 else
404 return master;
405 }
406
407 return NULL;
408}
409
Will Deacona9a1b0b2014-05-01 18:05:08 +0100410static struct arm_smmu_master_cfg *
Will Deacon8f68f8e2014-07-15 11:27:08 +0100411find_smmu_master_cfg(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100412{
Will Deacon8f68f8e2014-07-15 11:27:08 +0100413 struct arm_smmu_master_cfg *cfg = NULL;
414 struct iommu_group *group = iommu_group_get(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100415
Will Deacon8f68f8e2014-07-15 11:27:08 +0100416 if (group) {
417 cfg = iommu_group_get_iommudata(group);
418 iommu_group_put(group);
419 }
Will Deacona9a1b0b2014-05-01 18:05:08 +0100420
Will Deacon8f68f8e2014-07-15 11:27:08 +0100421 return cfg;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100422}
423
Will Deacon45ae7cf2013-06-24 18:31:25 +0100424static int insert_smmu_master(struct arm_smmu_device *smmu,
425 struct arm_smmu_master *master)
426{
427 struct rb_node **new, *parent;
428
429 new = &smmu->masters.rb_node;
430 parent = NULL;
431 while (*new) {
Mitchel Humpherys29073202014-07-08 09:52:18 -0700432 struct arm_smmu_master *this
433 = container_of(*new, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100434
435 parent = *new;
436 if (master->of_node < this->of_node)
437 new = &((*new)->rb_left);
438 else if (master->of_node > this->of_node)
439 new = &((*new)->rb_right);
440 else
441 return -EEXIST;
442 }
443
444 rb_link_node(&master->node, parent, new);
445 rb_insert_color(&master->node, &smmu->masters);
446 return 0;
447}
448
449static int register_smmu_master(struct arm_smmu_device *smmu,
450 struct device *dev,
451 struct of_phandle_args *masterspec)
452{
453 int i;
454 struct arm_smmu_master *master;
455
456 master = find_smmu_master(smmu, masterspec->np);
457 if (master) {
458 dev_err(dev,
459 "rejecting multiple registrations for master device %s\n",
460 masterspec->np->name);
461 return -EBUSY;
462 }
463
464 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
465 dev_err(dev,
466 "reached maximum number (%d) of stream IDs for master device %s\n",
467 MAX_MASTER_STREAMIDS, masterspec->np->name);
468 return -ENOSPC;
469 }
470
471 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
472 if (!master)
473 return -ENOMEM;
474
Will Deacona9a1b0b2014-05-01 18:05:08 +0100475 master->of_node = masterspec->np;
476 master->cfg.num_streamids = masterspec->args_count;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100477
Olav Haugan3c8766d2014-08-22 17:12:32 -0700478 for (i = 0; i < master->cfg.num_streamids; ++i) {
479 u16 streamid = masterspec->args[i];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100480
Olav Haugan3c8766d2014-08-22 17:12:32 -0700481 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
482 (streamid >= smmu->num_mapping_groups)) {
483 dev_err(dev,
484 "stream ID for master device %s greater than maximum allowed (%d)\n",
485 masterspec->np->name, smmu->num_mapping_groups);
486 return -ERANGE;
487 }
488 master->cfg.streamids[i] = streamid;
489 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100490 return insert_smmu_master(smmu, master);
491}
492
Will Deacon44680ee2014-06-25 11:29:12 +0100493static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100494{
Will Deacon44680ee2014-06-25 11:29:12 +0100495 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100496 struct arm_smmu_master *master = NULL;
Will Deacon8f68f8e2014-07-15 11:27:08 +0100497 struct device_node *dev_node = dev_get_dev_node(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100498
499 spin_lock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100500 list_for_each_entry(smmu, &arm_smmu_devices, list) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100501 master = find_smmu_master(smmu, dev_node);
502 if (master)
503 break;
504 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100505 spin_unlock(&arm_smmu_devices_lock);
Will Deacon44680ee2014-06-25 11:29:12 +0100506
Will Deacona9a1b0b2014-05-01 18:05:08 +0100507 return master ? smmu : NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100508}
509
510static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
511{
512 int idx;
513
514 do {
515 idx = find_next_zero_bit(map, end, start);
516 if (idx == end)
517 return -ENOSPC;
518 } while (test_and_set_bit(idx, map));
519
520 return idx;
521}
522
523static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
524{
525 clear_bit(idx, map);
526}
527
528/* Wait for any pending TLB invalidations to complete */
Will Deacon518f7132014-11-14 17:17:54 +0000529static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100530{
531 int count = 0;
532 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
533
534 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
535 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
536 & sTLBGSTATUS_GSACTIVE) {
537 cpu_relax();
538 if (++count == TLB_LOOP_TIMEOUT) {
539 dev_err_ratelimited(smmu->dev,
540 "TLB sync timed out -- SMMU may be deadlocked\n");
541 return;
542 }
543 udelay(1);
544 }
545}
546
Will Deacon518f7132014-11-14 17:17:54 +0000547static void arm_smmu_tlb_sync(void *cookie)
Will Deacon1463fe42013-07-31 19:21:27 +0100548{
Will Deacon518f7132014-11-14 17:17:54 +0000549 struct arm_smmu_domain *smmu_domain = cookie;
550 __arm_smmu_tlb_sync(smmu_domain->smmu);
551}
552
553static void arm_smmu_tlb_inv_context(void *cookie)
554{
555 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100556 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
557 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100558 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon518f7132014-11-14 17:17:54 +0000559 void __iomem *base;
Will Deacon1463fe42013-07-31 19:21:27 +0100560
561 if (stage1) {
562 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deaconecfadb62013-07-31 19:21:28 +0100563 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
564 base + ARM_SMMU_CB_S1_TLBIASID);
Will Deacon1463fe42013-07-31 19:21:27 +0100565 } else {
566 base = ARM_SMMU_GR0(smmu);
Will Deaconecfadb62013-07-31 19:21:28 +0100567 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
568 base + ARM_SMMU_GR0_TLBIVMID);
Will Deacon1463fe42013-07-31 19:21:27 +0100569 }
570
Will Deacon518f7132014-11-14 17:17:54 +0000571 __arm_smmu_tlb_sync(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100572}
573
Will Deacon518f7132014-11-14 17:17:54 +0000574static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
575 bool leaf, void *cookie)
576{
577 struct arm_smmu_domain *smmu_domain = cookie;
578 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
579 struct arm_smmu_device *smmu = smmu_domain->smmu;
580 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
581 void __iomem *reg;
582
583 if (stage1) {
584 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
585 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
586
587 if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
588 iova &= ~12UL;
589 iova |= ARM_SMMU_CB_ASID(cfg);
590 writel_relaxed(iova, reg);
591#ifdef CONFIG_64BIT
592 } else {
593 iova >>= 12;
594 iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
595 writeq_relaxed(iova, reg);
596#endif
597 }
598#ifdef CONFIG_64BIT
599 } else if (smmu->version == ARM_SMMU_V2) {
600 reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
601 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
602 ARM_SMMU_CB_S2_TLBIIPAS2;
603 writeq_relaxed(iova >> 12, reg);
604#endif
605 } else {
606 reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
607 writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
608 }
609}
610
Will Deacon518f7132014-11-14 17:17:54 +0000611static struct iommu_gather_ops arm_smmu_gather_ops = {
612 .tlb_flush_all = arm_smmu_tlb_inv_context,
613 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
614 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon518f7132014-11-14 17:17:54 +0000615};
616
Will Deacon45ae7cf2013-06-24 18:31:25 +0100617static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
618{
619 int flags, ret;
620 u32 fsr, far, fsynr, resume;
621 unsigned long iova;
622 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100623 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100624 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
625 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100626 void __iomem *cb_base;
627
Will Deacon44680ee2014-06-25 11:29:12 +0100628 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100629 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
630
631 if (!(fsr & FSR_FAULT))
632 return IRQ_NONE;
633
634 if (fsr & FSR_IGN)
635 dev_err_ratelimited(smmu->dev,
Hans Wennborg70c9a7d2014-08-06 05:42:01 +0100636 "Unexpected context fault (fsr 0x%x)\n",
Will Deacon45ae7cf2013-06-24 18:31:25 +0100637 fsr);
638
639 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
640 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
641
642 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
643 iova = far;
644#ifdef CONFIG_64BIT
645 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
646 iova |= ((unsigned long)far << 32);
647#endif
648
649 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
650 ret = IRQ_HANDLED;
651 resume = RESUME_RETRY;
652 } else {
Andreas Herrmann2ef0f032013-10-01 13:39:08 +0100653 dev_err_ratelimited(smmu->dev,
654 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100655 iova, fsynr, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100656 ret = IRQ_NONE;
657 resume = RESUME_TERMINATE;
658 }
659
660 /* Clear the faulting FSR */
661 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
662
663 /* Retry or terminate any stalled transactions */
664 if (fsr & FSR_SS)
665 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
666
667 return ret;
668}
669
670static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
671{
672 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
673 struct arm_smmu_device *smmu = dev;
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000674 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100675
676 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
677 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
678 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
679 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
680
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000681 if (!gfsr)
682 return IRQ_NONE;
683
Will Deacon45ae7cf2013-06-24 18:31:25 +0100684 dev_err_ratelimited(smmu->dev,
685 "Unexpected global fault, this could be serious\n");
686 dev_err_ratelimited(smmu->dev,
687 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
688 gfsr, gfsynr0, gfsynr1, gfsynr2);
689
690 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
Will Deaconadaba322013-07-31 19:21:26 +0100691 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100692}
693
Will Deacon518f7132014-11-14 17:17:54 +0000694static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
695 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100696{
697 u32 reg;
698 bool stage1;
Will Deacon44680ee2014-06-25 11:29:12 +0100699 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
700 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100701 void __iomem *cb_base, *gr0_base, *gr1_base;
702
703 gr0_base = ARM_SMMU_GR0(smmu);
704 gr1_base = ARM_SMMU_GR1(smmu);
Will Deacon44680ee2014-06-25 11:29:12 +0100705 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
706 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100707
Will Deacon4a1c93c2015-03-04 12:21:03 +0000708 if (smmu->version > ARM_SMMU_V1) {
709 /*
710 * CBA2R.
711 * *Must* be initialised before CBAR thanks to VMID16
712 * architectural oversight affected some implementations.
713 */
714#ifdef CONFIG_64BIT
715 reg = CBA2R_RW64_64BIT;
716#else
717 reg = CBA2R_RW64_32BIT;
718#endif
719 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
720 }
721
Will Deacon45ae7cf2013-06-24 18:31:25 +0100722 /* CBAR */
Will Deacon44680ee2014-06-25 11:29:12 +0100723 reg = cfg->cbar;
Robin Murphy09360402014-08-28 17:51:59 +0100724 if (smmu->version == ARM_SMMU_V1)
Mitchel Humpherys29073202014-07-08 09:52:18 -0700725 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100726
Will Deacon57ca90f2014-02-06 14:59:05 +0000727 /*
728 * Use the weakest shareability/memory types, so they are
729 * overridden by the ttbcr/pte.
730 */
731 if (stage1) {
732 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
733 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
734 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100735 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
Will Deacon57ca90f2014-02-06 14:59:05 +0000736 }
Will Deacon44680ee2014-06-25 11:29:12 +0100737 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100738
Will Deacon518f7132014-11-14 17:17:54 +0000739 /* TTBRs */
740 if (stage1) {
741 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
742 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
743 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
Will Deacon44680ee2014-06-25 11:29:12 +0100744 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
Will Deacon518f7132014-11-14 17:17:54 +0000745 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100746
Will Deacon518f7132014-11-14 17:17:54 +0000747 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
748 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
749 reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
750 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
751 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
752 } else {
753 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
754 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
755 reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
756 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
757 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100758
Will Deacon518f7132014-11-14 17:17:54 +0000759 /* TTBCR */
760 if (stage1) {
761 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
762 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
763 if (smmu->version > ARM_SMMU_V1) {
764 reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
Will Deacon5dc56162015-05-08 17:44:22 +0100765 reg |= TTBCR2_SEP_UPSTREAM;
Will Deacon518f7132014-11-14 17:17:54 +0000766 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100767 }
768 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000769 reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
770 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100771 }
772
Will Deacon518f7132014-11-14 17:17:54 +0000773 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100774 if (stage1) {
Will Deacon518f7132014-11-14 17:17:54 +0000775 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100776 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
Will Deacon518f7132014-11-14 17:17:54 +0000777 reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
778 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100779 }
780
Will Deacon45ae7cf2013-06-24 18:31:25 +0100781 /* SCTLR */
782 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
783 if (stage1)
784 reg |= SCTLR_S1_ASIDPNE;
785#ifdef __BIG_ENDIAN
786 reg |= SCTLR_E;
787#endif
Will Deacon25724842013-08-21 13:49:53 +0100788 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100789}
790
791static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100792 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100793{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100794 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000795 unsigned long ias, oas;
796 struct io_pgtable_ops *pgtbl_ops;
797 struct io_pgtable_cfg pgtbl_cfg;
798 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100799 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100800 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100801
Will Deacon518f7132014-11-14 17:17:54 +0000802 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100803 if (smmu_domain->smmu)
804 goto out_unlock;
805
Will Deaconc752ce42014-06-25 22:46:31 +0100806 /*
807 * Mapping the requested stage onto what we support is surprisingly
808 * complicated, mainly because the spec allows S1+S2 SMMUs without
809 * support for nested translation. That means we end up with the
810 * following table:
811 *
812 * Requested Supported Actual
813 * S1 N S1
814 * S1 S1+S2 S1
815 * S1 S2 S2
816 * S1 S1 S1
817 * N N N
818 * N S1+S2 S2
819 * N S2 S2
820 * N S1 S1
821 *
822 * Note that you can't actually request stage-2 mappings.
823 */
824 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
825 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
826 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
827 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
828
829 switch (smmu_domain->stage) {
830 case ARM_SMMU_DOMAIN_S1:
831 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
832 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000833 ias = smmu->va_size;
834 oas = smmu->ipa_size;
835 if (IS_ENABLED(CONFIG_64BIT))
836 fmt = ARM_64_LPAE_S1;
837 else
838 fmt = ARM_32_LPAE_S1;
Will Deaconc752ce42014-06-25 22:46:31 +0100839 break;
840 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100841 /*
842 * We will likely want to change this if/when KVM gets
843 * involved.
844 */
Will Deaconc752ce42014-06-25 22:46:31 +0100845 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100846 cfg->cbar = CBAR_TYPE_S2_TRANS;
847 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000848 ias = smmu->ipa_size;
849 oas = smmu->pa_size;
850 if (IS_ENABLED(CONFIG_64BIT))
851 fmt = ARM_64_LPAE_S2;
852 else
853 fmt = ARM_32_LPAE_S2;
Will Deaconc752ce42014-06-25 22:46:31 +0100854 break;
855 default:
856 ret = -EINVAL;
857 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100858 }
859
860 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
861 smmu->num_context_banks);
862 if (IS_ERR_VALUE(ret))
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100863 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100864
Will Deacon44680ee2014-06-25 11:29:12 +0100865 cfg->cbndx = ret;
Robin Murphy09360402014-08-28 17:51:59 +0100866 if (smmu->version == ARM_SMMU_V1) {
Will Deacon44680ee2014-06-25 11:29:12 +0100867 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
868 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100869 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100870 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100871 }
872
Will Deacon518f7132014-11-14 17:17:54 +0000873 pgtbl_cfg = (struct io_pgtable_cfg) {
874 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
875 .ias = ias,
876 .oas = oas,
877 .tlb = &arm_smmu_gather_ops,
Robin Murphy2df7a252015-07-29 19:46:06 +0100878 .iommu_dev = smmu->dev,
Will Deacon518f7132014-11-14 17:17:54 +0000879 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100880
Will Deacon518f7132014-11-14 17:17:54 +0000881 smmu_domain->smmu = smmu;
882 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
883 if (!pgtbl_ops) {
884 ret = -ENOMEM;
885 goto out_clear_smmu;
886 }
887
888 /* Update our support page sizes to reflect the page table format */
889 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
890
891 /* Initialise the context bank with our page table cfg */
892 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
893
894 /*
895 * Request context fault interrupt. Do this last to avoid the
896 * handler seeing a half-initialised domain state.
897 */
Will Deacon44680ee2014-06-25 11:29:12 +0100898 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100899 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
900 "arm-smmu-context-fault", domain);
901 if (IS_ERR_VALUE(ret)) {
902 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100903 cfg->irptndx, irq);
904 cfg->irptndx = INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100905 }
906
Will Deacon518f7132014-11-14 17:17:54 +0000907 mutex_unlock(&smmu_domain->init_mutex);
908
909 /* Publish page table ops for map/unmap */
910 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100911 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100912
Will Deacon518f7132014-11-14 17:17:54 +0000913out_clear_smmu:
914 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100915out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000916 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100917 return ret;
918}
919
920static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
921{
Joerg Roedel1d672632015-03-26 13:43:10 +0100922 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100923 struct arm_smmu_device *smmu = smmu_domain->smmu;
924 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon1463fe42013-07-31 19:21:27 +0100925 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100926 int irq;
927
928 if (!smmu)
929 return;
930
Will Deacon518f7132014-11-14 17:17:54 +0000931 /*
932 * Disable the context bank and free the page tables before freeing
933 * it.
934 */
Will Deacon44680ee2014-06-25 11:29:12 +0100935 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100936 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
Will Deacon1463fe42013-07-31 19:21:27 +0100937
Will Deacon44680ee2014-06-25 11:29:12 +0100938 if (cfg->irptndx != INVALID_IRPTNDX) {
939 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Will Deacon45ae7cf2013-06-24 18:31:25 +0100940 free_irq(irq, domain);
941 }
942
Will Deacon518f7132014-11-14 17:17:54 +0000943 if (smmu_domain->pgtbl_ops)
944 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
945
Will Deacon44680ee2014-06-25 11:29:12 +0100946 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100947}
948
Joerg Roedel1d672632015-03-26 13:43:10 +0100949static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100950{
951 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100952
Joerg Roedel1d672632015-03-26 13:43:10 +0100953 if (type != IOMMU_DOMAIN_UNMANAGED)
954 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100955 /*
956 * Allocate the domain and initialise some of its data structures.
957 * We can't really do anything meaningful until we've added a
958 * master.
959 */
960 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
961 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +0100962 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100963
Will Deacon518f7132014-11-14 17:17:54 +0000964 mutex_init(&smmu_domain->init_mutex);
965 spin_lock_init(&smmu_domain->pgtbl_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +0100966
967 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100968}
969
Joerg Roedel1d672632015-03-26 13:43:10 +0100970static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100971{
Joerg Roedel1d672632015-03-26 13:43:10 +0100972 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100973
974 /*
975 * Free the domain resources. We assume that all devices have
976 * already been detached.
977 */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100978 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100979 kfree(smmu_domain);
980}
981
982static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +0100983 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100984{
985 int i;
986 struct arm_smmu_smr *smrs;
987 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
988
989 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
990 return 0;
991
Will Deacona9a1b0b2014-05-01 18:05:08 +0100992 if (cfg->smrs)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100993 return -EEXIST;
994
Mitchel Humpherys29073202014-07-08 09:52:18 -0700995 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100996 if (!smrs) {
Will Deacona9a1b0b2014-05-01 18:05:08 +0100997 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
998 cfg->num_streamids);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100999 return -ENOMEM;
1000 }
1001
Will Deacon44680ee2014-06-25 11:29:12 +01001002 /* Allocate the SMRs on the SMMU */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001003 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001004 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1005 smmu->num_mapping_groups);
1006 if (IS_ERR_VALUE(idx)) {
1007 dev_err(smmu->dev, "failed to allocate free SMR\n");
1008 goto err_free_smrs;
1009 }
1010
1011 smrs[i] = (struct arm_smmu_smr) {
1012 .idx = idx,
1013 .mask = 0, /* We don't currently share SMRs */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001014 .id = cfg->streamids[i],
Will Deacon45ae7cf2013-06-24 18:31:25 +01001015 };
1016 }
1017
1018 /* It worked! Now, poke the actual hardware */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001019 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001020 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1021 smrs[i].mask << SMR_MASK_SHIFT;
1022 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1023 }
1024
Will Deacona9a1b0b2014-05-01 18:05:08 +01001025 cfg->smrs = smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001026 return 0;
1027
1028err_free_smrs:
1029 while (--i >= 0)
1030 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1031 kfree(smrs);
1032 return -ENOSPC;
1033}
1034
1035static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001036 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001037{
1038 int i;
1039 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001040 struct arm_smmu_smr *smrs = cfg->smrs;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001041
Will Deacon43b412b2014-07-15 11:22:24 +01001042 if (!smrs)
1043 return;
1044
Will Deacon45ae7cf2013-06-24 18:31:25 +01001045 /* Invalidate the SMRs before freeing back to the allocator */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001046 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001047 u8 idx = smrs[i].idx;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001048
Will Deacon45ae7cf2013-06-24 18:31:25 +01001049 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1050 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1051 }
1052
Will Deacona9a1b0b2014-05-01 18:05:08 +01001053 cfg->smrs = NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001054 kfree(smrs);
1055}
1056
Will Deacon45ae7cf2013-06-24 18:31:25 +01001057static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001058 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001059{
1060 int i, ret;
Will Deacon44680ee2014-06-25 11:29:12 +01001061 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001062 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1063
Will Deacon8f68f8e2014-07-15 11:27:08 +01001064 /* Devices in an IOMMU group may already be configured */
Will Deacona9a1b0b2014-05-01 18:05:08 +01001065 ret = arm_smmu_master_configure_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001066 if (ret)
Will Deacon8f68f8e2014-07-15 11:27:08 +01001067 return ret == -EEXIST ? 0 : ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001068
Will Deacona9a1b0b2014-05-01 18:05:08 +01001069 for (i = 0; i < cfg->num_streamids; ++i) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001070 u32 idx, s2cr;
Mitchel Humpherys29073202014-07-08 09:52:18 -07001071
Will Deacona9a1b0b2014-05-01 18:05:08 +01001072 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
Kefeng Wang6069d232014-04-18 10:20:48 +08001073 s2cr = S2CR_TYPE_TRANS |
Will Deacon44680ee2014-06-25 11:29:12 +01001074 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001075 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1076 }
1077
1078 return 0;
1079}
1080
1081static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
Will Deacona9a1b0b2014-05-01 18:05:08 +01001082 struct arm_smmu_master_cfg *cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001083{
Will Deacon43b412b2014-07-15 11:22:24 +01001084 int i;
Will Deacon44680ee2014-06-25 11:29:12 +01001085 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon43b412b2014-07-15 11:22:24 +01001086 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001087
Will Deacon8f68f8e2014-07-15 11:27:08 +01001088 /* An IOMMU group is torn down by the first device to be removed */
1089 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1090 return;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001091
1092 /*
1093 * We *must* clear the S2CR first, because freeing the SMR means
1094 * that it can be re-allocated immediately.
1095 */
Will Deacon43b412b2014-07-15 11:22:24 +01001096 for (i = 0; i < cfg->num_streamids; ++i) {
1097 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1098
1099 writel_relaxed(S2CR_TYPE_BYPASS,
1100 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1101 }
1102
Will Deacona9a1b0b2014-05-01 18:05:08 +01001103 arm_smmu_master_free_smrs(smmu, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001104}
1105
1106static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1107{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001108 int ret;
Joerg Roedel1d672632015-03-26 13:43:10 +01001109 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001110 struct arm_smmu_device *smmu;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001111 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001112
Will Deacon8f68f8e2014-07-15 11:27:08 +01001113 smmu = find_smmu_for_device(dev);
Will Deacon44680ee2014-06-25 11:29:12 +01001114 if (!smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001115 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1116 return -ENXIO;
1117 }
1118
Will Deacon844e35b2014-07-17 11:23:51 +01001119 if (dev->archdata.iommu) {
1120 dev_err(dev, "already attached to IOMMU domain\n");
1121 return -EEXIST;
1122 }
1123
Will Deacon518f7132014-11-14 17:17:54 +00001124 /* Ensure that the domain is finalised */
1125 ret = arm_smmu_init_domain_context(domain, smmu);
1126 if (IS_ERR_VALUE(ret))
1127 return ret;
1128
Will Deacon45ae7cf2013-06-24 18:31:25 +01001129 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001130 * Sanity check the domain. We don't support domains across
1131 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001132 */
Will Deacon518f7132014-11-14 17:17:54 +00001133 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001134 dev_err(dev,
1135 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Mitchel Humpherysa18037b2014-07-30 18:58:13 +01001136 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1137 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001138 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001139
1140 /* Looks ok, so add the device to the domain */
Will Deacon8f68f8e2014-07-15 11:27:08 +01001141 cfg = find_smmu_master_cfg(dev);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001142 if (!cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001143 return -ENODEV;
1144
Will Deacon844e35b2014-07-17 11:23:51 +01001145 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1146 if (!ret)
1147 dev->archdata.iommu = domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001148 return ret;
1149}
1150
1151static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1152{
Joerg Roedel1d672632015-03-26 13:43:10 +01001153 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001154 struct arm_smmu_master_cfg *cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001155
Will Deacon8f68f8e2014-07-15 11:27:08 +01001156 cfg = find_smmu_master_cfg(dev);
Will Deacon844e35b2014-07-17 11:23:51 +01001157 if (!cfg)
1158 return;
1159
1160 dev->archdata.iommu = NULL;
1161 arm_smmu_domain_remove_master(smmu_domain, cfg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001162}
1163
Will Deacon45ae7cf2013-06-24 18:31:25 +01001164static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Will Deaconb410aed2014-02-20 16:31:06 +00001165 phys_addr_t paddr, size_t size, int prot)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001166{
Will Deacon518f7132014-11-14 17:17:54 +00001167 int ret;
1168 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001169 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001170 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001171
Will Deacon518f7132014-11-14 17:17:54 +00001172 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001173 return -ENODEV;
1174
Will Deacon518f7132014-11-14 17:17:54 +00001175 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1176 ret = ops->map(ops, iova, paddr, size, prot);
1177 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1178 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001179}
1180
1181static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1182 size_t size)
1183{
Will Deacon518f7132014-11-14 17:17:54 +00001184 size_t ret;
1185 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001186 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001187 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001188
Will Deacon518f7132014-11-14 17:17:54 +00001189 if (!ops)
1190 return 0;
1191
1192 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1193 ret = ops->unmap(ops, iova, size);
1194 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1195 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001196}
1197
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001198static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1199 dma_addr_t iova)
1200{
Joerg Roedel1d672632015-03-26 13:43:10 +01001201 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001202 struct arm_smmu_device *smmu = smmu_domain->smmu;
1203 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1204 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1205 struct device *dev = smmu->dev;
1206 void __iomem *cb_base;
1207 u32 tmp;
1208 u64 phys;
Robin Murphy661d9622015-05-27 17:09:34 +01001209 unsigned long va;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001210
1211 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
1212
Robin Murphy661d9622015-05-27 17:09:34 +01001213 /* ATS1 registers can only be written atomically */
1214 va = iova & ~0xfffUL;
1215#ifdef CONFIG_64BIT
1216 if (smmu->version == ARM_SMMU_V2)
1217 writeq_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
1218 else
1219#endif
1220 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001221
1222 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1223 !(tmp & ATSR_ACTIVE), 5, 50)) {
1224 dev_err(dev,
Fabio Estevam077124c2015-08-18 17:12:24 +01001225 "iova to phys timed out on %pad. Falling back to software table walk.\n",
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001226 &iova);
1227 return ops->iova_to_phys(ops, iova);
1228 }
1229
1230 phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
1231 phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
1232
1233 if (phys & CB_PAR_F) {
1234 dev_err(dev, "translation fault!\n");
1235 dev_err(dev, "PAR = 0x%llx\n", phys);
1236 return 0;
1237 }
1238
1239 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1240}
1241
Will Deacon45ae7cf2013-06-24 18:31:25 +01001242static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001243 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001244{
Will Deacon518f7132014-11-14 17:17:54 +00001245 phys_addr_t ret;
1246 unsigned long flags;
Joerg Roedel1d672632015-03-26 13:43:10 +01001247 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon518f7132014-11-14 17:17:54 +00001248 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001249
Will Deacon518f7132014-11-14 17:17:54 +00001250 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001251 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001252
Will Deacon518f7132014-11-14 17:17:54 +00001253 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001254 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1255 smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001256 ret = arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001257 } else {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001258 ret = ops->iova_to_phys(ops, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001259 }
1260
Will Deacon518f7132014-11-14 17:17:54 +00001261 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001262
Will Deacon518f7132014-11-14 17:17:54 +00001263 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001264}
1265
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001266static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001267{
Will Deacond0948942014-06-24 17:30:10 +01001268 switch (cap) {
1269 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001270 /*
1271 * Return true here as the SMMU can always send out coherent
1272 * requests.
1273 */
1274 return true;
Will Deacond0948942014-06-24 17:30:10 +01001275 case IOMMU_CAP_INTR_REMAP:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001276 return true; /* MSIs are just memory writes */
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001277 case IOMMU_CAP_NOEXEC:
1278 return true;
Will Deacond0948942014-06-24 17:30:10 +01001279 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001280 return false;
Will Deacond0948942014-06-24 17:30:10 +01001281 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001282}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001283
Will Deacona9a1b0b2014-05-01 18:05:08 +01001284static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1285{
1286 *((u16 *)data) = alias;
1287 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001288}
1289
Will Deacon8f68f8e2014-07-15 11:27:08 +01001290static void __arm_smmu_release_pci_iommudata(void *data)
1291{
1292 kfree(data);
1293}
1294
Will Deacon03edb222015-01-19 14:27:33 +00001295static int arm_smmu_add_pci_device(struct pci_dev *pdev)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001296{
Will Deacon03edb222015-01-19 14:27:33 +00001297 int i, ret;
1298 u16 sid;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001299 struct iommu_group *group;
Will Deacon03edb222015-01-19 14:27:33 +00001300 struct arm_smmu_master_cfg *cfg;
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001301
Will Deacon03edb222015-01-19 14:27:33 +00001302 group = iommu_group_get_for_dev(&pdev->dev);
1303 if (IS_ERR(group))
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001304 return PTR_ERR(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001305
Will Deacon03edb222015-01-19 14:27:33 +00001306 cfg = iommu_group_get_iommudata(group);
1307 if (!cfg) {
Will Deacona9a1b0b2014-05-01 18:05:08 +01001308 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1309 if (!cfg) {
1310 ret = -ENOMEM;
1311 goto out_put_group;
1312 }
1313
Will Deacon03edb222015-01-19 14:27:33 +00001314 iommu_group_set_iommudata(group, cfg,
1315 __arm_smmu_release_pci_iommudata);
Will Deacona9a1b0b2014-05-01 18:05:08 +01001316 }
1317
Will Deacon03edb222015-01-19 14:27:33 +00001318 if (cfg->num_streamids >= MAX_MASTER_STREAMIDS) {
1319 ret = -ENOSPC;
1320 goto out_put_group;
1321 }
Will Deacona9a1b0b2014-05-01 18:05:08 +01001322
Will Deacon03edb222015-01-19 14:27:33 +00001323 /*
1324 * Assume Stream ID == Requester ID for now.
1325 * We need a way to describe the ID mappings in FDT.
1326 */
1327 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1328 for (i = 0; i < cfg->num_streamids; ++i)
1329 if (cfg->streamids[i] == sid)
1330 break;
1331
1332 /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
1333 if (i == cfg->num_streamids)
1334 cfg->streamids[cfg->num_streamids++] = sid;
1335
1336 return 0;
Will Deacona9a1b0b2014-05-01 18:05:08 +01001337out_put_group:
1338 iommu_group_put(group);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001339 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001340}
1341
Will Deacon03edb222015-01-19 14:27:33 +00001342static int arm_smmu_add_platform_device(struct device *dev)
1343{
1344 struct iommu_group *group;
1345 struct arm_smmu_master *master;
1346 struct arm_smmu_device *smmu = find_smmu_for_device(dev);
1347
1348 if (!smmu)
1349 return -ENODEV;
1350
1351 master = find_smmu_master(smmu, dev->of_node);
1352 if (!master)
1353 return -ENODEV;
1354
1355 /* No automatic group creation for platform devices */
1356 group = iommu_group_alloc();
1357 if (IS_ERR(group))
1358 return PTR_ERR(group);
1359
1360 iommu_group_set_iommudata(group, &master->cfg, NULL);
1361 return iommu_group_add_device(group, dev);
1362}
1363
1364static int arm_smmu_add_device(struct device *dev)
1365{
1366 if (dev_is_pci(dev))
1367 return arm_smmu_add_pci_device(to_pci_dev(dev));
1368
1369 return arm_smmu_add_platform_device(dev);
1370}
1371
Will Deacon45ae7cf2013-06-24 18:31:25 +01001372static void arm_smmu_remove_device(struct device *dev)
1373{
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001374 iommu_group_remove_device(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001375}
1376
Will Deaconc752ce42014-06-25 22:46:31 +01001377static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1378 enum iommu_attr attr, void *data)
1379{
Joerg Roedel1d672632015-03-26 13:43:10 +01001380 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001381
1382 switch (attr) {
1383 case DOMAIN_ATTR_NESTING:
1384 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1385 return 0;
1386 default:
1387 return -ENODEV;
1388 }
1389}
1390
1391static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1392 enum iommu_attr attr, void *data)
1393{
Will Deacon518f7132014-11-14 17:17:54 +00001394 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001395 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001396
Will Deacon518f7132014-11-14 17:17:54 +00001397 mutex_lock(&smmu_domain->init_mutex);
1398
Will Deaconc752ce42014-06-25 22:46:31 +01001399 switch (attr) {
1400 case DOMAIN_ATTR_NESTING:
Will Deacon518f7132014-11-14 17:17:54 +00001401 if (smmu_domain->smmu) {
1402 ret = -EPERM;
1403 goto out_unlock;
1404 }
1405
Will Deaconc752ce42014-06-25 22:46:31 +01001406 if (*(int *)data)
1407 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1408 else
1409 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1410
Will Deacon518f7132014-11-14 17:17:54 +00001411 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001412 default:
Will Deacon518f7132014-11-14 17:17:54 +00001413 ret = -ENODEV;
Will Deaconc752ce42014-06-25 22:46:31 +01001414 }
Will Deacon518f7132014-11-14 17:17:54 +00001415
1416out_unlock:
1417 mutex_unlock(&smmu_domain->init_mutex);
1418 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001419}
1420
Will Deacon518f7132014-11-14 17:17:54 +00001421static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001422 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001423 .domain_alloc = arm_smmu_domain_alloc,
1424 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001425 .attach_dev = arm_smmu_attach_dev,
1426 .detach_dev = arm_smmu_detach_dev,
1427 .map = arm_smmu_map,
1428 .unmap = arm_smmu_unmap,
Joerg Roedel76771c92014-12-02 13:07:13 +01001429 .map_sg = default_iommu_map_sg,
Will Deaconc752ce42014-06-25 22:46:31 +01001430 .iova_to_phys = arm_smmu_iova_to_phys,
1431 .add_device = arm_smmu_add_device,
1432 .remove_device = arm_smmu_remove_device,
1433 .domain_get_attr = arm_smmu_domain_get_attr,
1434 .domain_set_attr = arm_smmu_domain_set_attr,
Will Deacon518f7132014-11-14 17:17:54 +00001435 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001436};
1437
1438static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1439{
1440 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001441 void __iomem *cb_base;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001442 int i = 0;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001443 u32 reg;
1444
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001445 /* clear global FSR */
1446 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1447 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001448
1449 /* Mark all SMRn as invalid and all S2CRn as bypass */
1450 for (i = 0; i < smmu->num_mapping_groups; ++i) {
Olav Haugan3c8766d2014-08-22 17:12:32 -07001451 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
Mitchel Humpherys29073202014-07-08 09:52:18 -07001452 writel_relaxed(S2CR_TYPE_BYPASS,
1453 gr0_base + ARM_SMMU_GR0_S2CR(i));
Will Deacon45ae7cf2013-06-24 18:31:25 +01001454 }
1455
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001456 /* Make sure all context banks are disabled and clear CB_FSR */
1457 for (i = 0; i < smmu->num_context_banks; ++i) {
1458 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1459 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1460 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1461 }
Will Deacon1463fe42013-07-31 19:21:27 +01001462
Will Deacon45ae7cf2013-06-24 18:31:25 +01001463 /* Invalidate the TLB, just in case */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001464 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1465 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1466
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001467 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001468
Will Deacon45ae7cf2013-06-24 18:31:25 +01001469 /* Enable fault reporting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001470 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001471
1472 /* Disable TLB broadcasting. */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001473 reg |= (sCR0_VMIDPNE | sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001474
1475 /* Enable client access, but bypass when no mapping is found */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001476 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001477
1478 /* Disable forced broadcasting */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001479 reg &= ~sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001480
1481 /* Don't upgrade barriers */
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001482 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001483
1484 /* Push the button */
Will Deacon518f7132014-11-14 17:17:54 +00001485 __arm_smmu_tlb_sync(smmu);
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001486 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001487}
1488
1489static int arm_smmu_id_size_to_bits(int size)
1490{
1491 switch (size) {
1492 case 0:
1493 return 32;
1494 case 1:
1495 return 36;
1496 case 2:
1497 return 40;
1498 case 3:
1499 return 42;
1500 case 4:
1501 return 44;
1502 case 5:
1503 default:
1504 return 48;
1505 }
1506}
1507
1508static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1509{
1510 unsigned long size;
1511 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1512 u32 id;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001513 bool cttw_dt, cttw_reg;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001514
1515 dev_notice(smmu->dev, "probing hardware configuration...\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001516 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1517
1518 /* ID0 */
1519 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001520
1521 /* Restrict available stages based on module parameter */
1522 if (force_stage == 1)
1523 id &= ~(ID0_S2TS | ID0_NTS);
1524 else if (force_stage == 2)
1525 id &= ~(ID0_S1TS | ID0_NTS);
1526
Will Deacon45ae7cf2013-06-24 18:31:25 +01001527 if (id & ID0_S1TS) {
1528 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1529 dev_notice(smmu->dev, "\tstage 1 translation\n");
1530 }
1531
1532 if (id & ID0_S2TS) {
1533 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1534 dev_notice(smmu->dev, "\tstage 2 translation\n");
1535 }
1536
1537 if (id & ID0_NTS) {
1538 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1539 dev_notice(smmu->dev, "\tnested translation\n");
1540 }
1541
1542 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001543 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001544 dev_err(smmu->dev, "\tno translation support!\n");
1545 return -ENODEV;
1546 }
1547
Will Deacond38f0ff2015-06-29 17:47:42 +01001548 if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001549 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1550 dev_notice(smmu->dev, "\taddress translation ops\n");
1551 }
1552
Robin Murphybae2c2d2015-07-29 19:46:05 +01001553 /*
1554 * In order for DMA API calls to work properly, we must defer to what
1555 * the DT says about coherency, regardless of what the hardware claims.
1556 * Fortunately, this also opens up a workaround for systems where the
1557 * ID register value has ended up configured incorrectly.
1558 */
1559 cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
1560 cttw_reg = !!(id & ID0_CTTW);
1561 if (cttw_dt)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001562 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
Robin Murphybae2c2d2015-07-29 19:46:05 +01001563 if (cttw_dt || cttw_reg)
1564 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1565 cttw_dt ? "" : "non-");
1566 if (cttw_dt != cttw_reg)
1567 dev_notice(smmu->dev,
1568 "\t(IDR0.CTTW overridden by dma-coherent property)\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001569
1570 if (id & ID0_SMS) {
1571 u32 smr, sid, mask;
1572
1573 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1574 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1575 ID0_NUMSMRG_MASK;
1576 if (smmu->num_mapping_groups == 0) {
1577 dev_err(smmu->dev,
1578 "stream-matching supported, but no SMRs present!\n");
1579 return -ENODEV;
1580 }
1581
1582 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1583 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1584 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1585 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1586
1587 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1588 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1589 if ((mask & sid) != sid) {
1590 dev_err(smmu->dev,
1591 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1592 mask, sid);
1593 return -ENODEV;
1594 }
1595
1596 dev_notice(smmu->dev,
1597 "\tstream matching with %u register groups, mask 0x%x",
1598 smmu->num_mapping_groups, mask);
Olav Haugan3c8766d2014-08-22 17:12:32 -07001599 } else {
1600 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1601 ID0_NUMSIDB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001602 }
1603
1604 /* ID1 */
1605 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
Will Deaconc757e852014-07-30 11:33:25 +01001606 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001607
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001608 /* Check for size mismatch of SMMU address space from mapped region */
Will Deacon518f7132014-11-14 17:17:54 +00001609 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
Will Deaconc757e852014-07-30 11:33:25 +01001610 size *= 2 << smmu->pgshift;
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001611 if (smmu->size != size)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001612 dev_warn(smmu->dev,
1613 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1614 size, smmu->size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001615
Will Deacon518f7132014-11-14 17:17:54 +00001616 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001617 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1618 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1619 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1620 return -ENODEV;
1621 }
1622 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1623 smmu->num_context_banks, smmu->num_s2_context_banks);
1624
1625 /* ID2 */
1626 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1627 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001628 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001629
Will Deacon518f7132014-11-14 17:17:54 +00001630 /* The output mask is also applied for bypass */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001631 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
Will Deacon518f7132014-11-14 17:17:54 +00001632 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001633
Robin Murphyf1d84542015-03-04 16:41:05 +00001634 /*
1635 * What the page table walker can address actually depends on which
1636 * descriptor format is in use, but since a) we don't know that yet,
1637 * and b) it can vary per context bank, this will have to do...
1638 */
1639 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1640 dev_warn(smmu->dev,
1641 "failed to set DMA mask for table walker\n");
1642
Robin Murphy09360402014-08-28 17:51:59 +01001643 if (smmu->version == ARM_SMMU_V1) {
Will Deacon518f7132014-11-14 17:17:54 +00001644 smmu->va_size = smmu->ipa_size;
1645 size = SZ_4K | SZ_2M | SZ_1G;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001646 } else {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001647 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
Will Deacon518f7132014-11-14 17:17:54 +00001648 smmu->va_size = arm_smmu_id_size_to_bits(size);
1649#ifndef CONFIG_64BIT
1650 smmu->va_size = min(32UL, smmu->va_size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001651#endif
Will Deacon518f7132014-11-14 17:17:54 +00001652 size = 0;
1653 if (id & ID2_PTFS_4K)
1654 size |= SZ_4K | SZ_2M | SZ_1G;
1655 if (id & ID2_PTFS_16K)
1656 size |= SZ_16K | SZ_32M;
1657 if (id & ID2_PTFS_64K)
1658 size |= SZ_64K | SZ_512M;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001659 }
1660
Will Deacon518f7132014-11-14 17:17:54 +00001661 arm_smmu_ops.pgsize_bitmap &= size;
1662 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
1663
Will Deacon28d60072014-09-01 16:24:48 +01001664 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1665 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001666 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001667
1668 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1669 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001670 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001671
Will Deacon45ae7cf2013-06-24 18:31:25 +01001672 return 0;
1673}
1674
Joerg Roedel09b52692014-10-02 12:24:45 +02001675static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy09360402014-08-28 17:51:59 +01001676 { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
1677 { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
1678 { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
Robin Murphyd3aba042014-08-28 17:52:00 +01001679 { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
Robin Murphy09360402014-08-28 17:51:59 +01001680 { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
1681 { },
1682};
1683MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1684
Will Deacon45ae7cf2013-06-24 18:31:25 +01001685static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1686{
Robin Murphy09360402014-08-28 17:51:59 +01001687 const struct of_device_id *of_id;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001688 struct resource *res;
1689 struct arm_smmu_device *smmu;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001690 struct device *dev = &pdev->dev;
1691 struct rb_node *node;
1692 struct of_phandle_args masterspec;
1693 int num_irqs, i, err;
1694
1695 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1696 if (!smmu) {
1697 dev_err(dev, "failed to allocate arm_smmu_device\n");
1698 return -ENOMEM;
1699 }
1700 smmu->dev = dev;
1701
Robin Murphy09360402014-08-28 17:51:59 +01001702 of_id = of_match_node(arm_smmu_of_match, dev->of_node);
1703 smmu->version = (enum arm_smmu_arch_version)of_id->data;
1704
Will Deacon45ae7cf2013-06-24 18:31:25 +01001705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall8a7f4312013-08-19 12:20:37 +01001706 smmu->base = devm_ioremap_resource(dev, res);
1707 if (IS_ERR(smmu->base))
1708 return PTR_ERR(smmu->base);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001709 smmu->size = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001710
1711 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1712 &smmu->num_global_irqs)) {
1713 dev_err(dev, "missing #global-interrupts property\n");
1714 return -ENODEV;
1715 }
1716
1717 num_irqs = 0;
1718 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1719 num_irqs++;
1720 if (num_irqs > smmu->num_global_irqs)
1721 smmu->num_context_irqs++;
1722 }
1723
Andreas Herrmann44a08de2013-10-01 13:39:07 +01001724 if (!smmu->num_context_irqs) {
1725 dev_err(dev, "found %d interrupts but expected at least %d\n",
1726 num_irqs, smmu->num_global_irqs + 1);
1727 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001728 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001729
1730 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1731 GFP_KERNEL);
1732 if (!smmu->irqs) {
1733 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1734 return -ENOMEM;
1735 }
1736
1737 for (i = 0; i < num_irqs; ++i) {
1738 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07001739
Will Deacon45ae7cf2013-06-24 18:31:25 +01001740 if (irq < 0) {
1741 dev_err(dev, "failed to get irq index %d\n", i);
1742 return -ENODEV;
1743 }
1744 smmu->irqs[i] = irq;
1745 }
1746
Olav Haugan3c8766d2014-08-22 17:12:32 -07001747 err = arm_smmu_device_cfg_probe(smmu);
1748 if (err)
1749 return err;
1750
Will Deacon45ae7cf2013-06-24 18:31:25 +01001751 i = 0;
1752 smmu->masters = RB_ROOT;
1753 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1754 "#stream-id-cells", i,
1755 &masterspec)) {
1756 err = register_smmu_master(smmu, dev, &masterspec);
1757 if (err) {
1758 dev_err(dev, "failed to add master %s\n",
1759 masterspec.np->name);
1760 goto out_put_masters;
1761 }
1762
1763 i++;
1764 }
1765 dev_notice(dev, "registered %d master devices\n", i);
1766
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001767 parse_driver_options(smmu);
1768
Robin Murphy09360402014-08-28 17:51:59 +01001769 if (smmu->version > ARM_SMMU_V1 &&
Will Deacon45ae7cf2013-06-24 18:31:25 +01001770 smmu->num_context_banks != smmu->num_context_irqs) {
1771 dev_err(dev,
1772 "found only %d context interrupt(s) but %d required\n",
1773 smmu->num_context_irqs, smmu->num_context_banks);
Wei Yongjun89a23cde2013-11-15 09:42:30 +00001774 err = -ENODEV;
Will Deacon44680ee2014-06-25 11:29:12 +01001775 goto out_put_masters;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001776 }
1777
Will Deacon45ae7cf2013-06-24 18:31:25 +01001778 for (i = 0; i < smmu->num_global_irqs; ++i) {
1779 err = request_irq(smmu->irqs[i],
1780 arm_smmu_global_fault,
1781 IRQF_SHARED,
1782 "arm-smmu global fault",
1783 smmu);
1784 if (err) {
1785 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1786 i, smmu->irqs[i]);
1787 goto out_free_irqs;
1788 }
1789 }
1790
1791 INIT_LIST_HEAD(&smmu->list);
1792 spin_lock(&arm_smmu_devices_lock);
1793 list_add(&smmu->list, &arm_smmu_devices);
1794 spin_unlock(&arm_smmu_devices_lock);
Will Deaconfd90cec2013-08-21 13:56:34 +01001795
1796 arm_smmu_device_reset(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001797 return 0;
1798
1799out_free_irqs:
1800 while (i--)
1801 free_irq(smmu->irqs[i], smmu);
1802
Will Deacon45ae7cf2013-06-24 18:31:25 +01001803out_put_masters:
1804 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001805 struct arm_smmu_master *master
1806 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001807 of_node_put(master->of_node);
1808 }
1809
1810 return err;
1811}
1812
1813static int arm_smmu_device_remove(struct platform_device *pdev)
1814{
1815 int i;
1816 struct device *dev = &pdev->dev;
1817 struct arm_smmu_device *curr, *smmu = NULL;
1818 struct rb_node *node;
1819
1820 spin_lock(&arm_smmu_devices_lock);
1821 list_for_each_entry(curr, &arm_smmu_devices, list) {
1822 if (curr->dev == dev) {
1823 smmu = curr;
1824 list_del(&smmu->list);
1825 break;
1826 }
1827 }
1828 spin_unlock(&arm_smmu_devices_lock);
1829
1830 if (!smmu)
1831 return -ENODEV;
1832
Will Deacon45ae7cf2013-06-24 18:31:25 +01001833 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
Mitchel Humpherys29073202014-07-08 09:52:18 -07001834 struct arm_smmu_master *master
1835 = container_of(node, struct arm_smmu_master, node);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001836 of_node_put(master->of_node);
1837 }
1838
Will Deaconecfadb62013-07-31 19:21:28 +01001839 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001840 dev_err(dev, "removing device with active domains!\n");
1841
1842 for (i = 0; i < smmu->num_global_irqs; ++i)
1843 free_irq(smmu->irqs[i], smmu);
1844
1845 /* Turn the thing off */
Mitchel Humpherys29073202014-07-08 09:52:18 -07001846 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001847 return 0;
1848}
1849
Will Deacon45ae7cf2013-06-24 18:31:25 +01001850static struct platform_driver arm_smmu_driver = {
1851 .driver = {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001852 .name = "arm-smmu",
1853 .of_match_table = of_match_ptr(arm_smmu_of_match),
1854 },
1855 .probe = arm_smmu_device_dt_probe,
1856 .remove = arm_smmu_device_remove,
1857};
1858
1859static int __init arm_smmu_init(void)
1860{
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001861 struct device_node *np;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001862 int ret;
1863
Thierry Reding0e7d37a2014-11-07 15:26:18 +00001864 /*
1865 * Play nice with systems that don't have an ARM SMMU by checking that
1866 * an ARM SMMU exists in the system before proceeding with the driver
1867 * and IOMMU bus operation registration.
1868 */
1869 np = of_find_matching_node(NULL, arm_smmu_of_match);
1870 if (!np)
1871 return 0;
1872
1873 of_node_put(np);
1874
Will Deacon45ae7cf2013-06-24 18:31:25 +01001875 ret = platform_driver_register(&arm_smmu_driver);
1876 if (ret)
1877 return ret;
1878
1879 /* Oh, for a proper bus abstraction */
Dan Carpenter6614ee72013-08-21 09:34:20 +01001880 if (!iommu_present(&platform_bus_type))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001881 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
1882
Will Deacond123cf82014-02-04 22:17:53 +00001883#ifdef CONFIG_ARM_AMBA
Dan Carpenter6614ee72013-08-21 09:34:20 +01001884 if (!iommu_present(&amba_bustype))
Will Deacon45ae7cf2013-06-24 18:31:25 +01001885 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
Will Deacond123cf82014-02-04 22:17:53 +00001886#endif
Will Deacon45ae7cf2013-06-24 18:31:25 +01001887
Will Deacona9a1b0b2014-05-01 18:05:08 +01001888#ifdef CONFIG_PCI
1889 if (!iommu_present(&pci_bus_type))
1890 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
1891#endif
1892
Will Deacon45ae7cf2013-06-24 18:31:25 +01001893 return 0;
1894}
1895
1896static void __exit arm_smmu_exit(void)
1897{
1898 return platform_driver_unregister(&arm_smmu_driver);
1899}
1900
Andreas Herrmannb1950b22013-10-01 13:39:05 +01001901subsys_initcall(arm_smmu_init);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001902module_exit(arm_smmu_exit);
1903
1904MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
1905MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
1906MODULE_LICENSE("GPL v2");