blob: ac9eac966cf58d752597859f8fdea6ff2e1ed9b8 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Rob Clark2b037742017-08-09 10:43:03 -04002/*
3 * IOMMU API for ARM architected SMMU implementations.
4 *
Rob Clark2b037742017-08-09 10:43:03 -04005 * Copyright (C) 2013 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
Robin Murphyc5fc6482019-08-15 19:37:32 +010010#ifndef _ARM_SMMU_H
11#define _ARM_SMMU_H
Rob Clark2b037742017-08-09 10:43:03 -040012
Robin Murphyfc058d32019-08-15 19:37:33 +010013#include <linux/atomic.h>
Robin Murphy0caf5f42019-08-15 19:37:23 +010014#include <linux/bits.h>
Robin Murphyfc058d32019-08-15 19:37:33 +010015#include <linux/clk.h>
16#include <linux/device.h>
Robin Murphyd720e642019-08-20 12:38:49 +010017#include <linux/io-64-nonatomic-hi-lo.h>
Robin Murphyba7e4a02019-08-15 19:37:37 +010018#include <linux/io-pgtable.h>
Robin Murphyfc058d32019-08-15 19:37:33 +010019#include <linux/iommu.h>
20#include <linux/mutex.h>
21#include <linux/spinlock.h>
22#include <linux/types.h>
Robin Murphy0caf5f42019-08-15 19:37:23 +010023
Rob Clark2b037742017-08-09 10:43:03 -040024/* Configuration registers */
25#define ARM_SMMU_GR0_sCR0 0x0
Robin Murphy0caf5f42019-08-15 19:37:23 +010026#define sCR0_VMID16EN BIT(31)
27#define sCR0_BSU GENMASK(15, 14)
28#define sCR0_FB BIT(13)
29#define sCR0_PTM BIT(12)
30#define sCR0_VMIDPNE BIT(11)
31#define sCR0_USFCFG BIT(10)
32#define sCR0_GCFGFIE BIT(5)
33#define sCR0_GCFGFRE BIT(4)
34#define sCR0_EXIDENABLE BIT(3)
35#define sCR0_GFIE BIT(2)
36#define sCR0_GFRE BIT(1)
37#define sCR0_CLIENTPD BIT(0)
Rob Clark2b037742017-08-09 10:43:03 -040038
39/* Auxiliary Configuration register */
40#define ARM_SMMU_GR0_sACR 0x10
41
42/* Identification registers */
43#define ARM_SMMU_GR0_ID0 0x20
Robin Murphy0caf5f42019-08-15 19:37:23 +010044#define ID0_S1TS BIT(30)
45#define ID0_S2TS BIT(29)
46#define ID0_NTS BIT(28)
47#define ID0_SMS BIT(27)
48#define ID0_ATOSNS BIT(26)
49#define ID0_PTFS_NO_AARCH32 BIT(25)
50#define ID0_PTFS_NO_AARCH32S BIT(24)
51#define ID0_NUMIRPT GENMASK(23, 16)
52#define ID0_CTTW BIT(14)
53#define ID0_NUMSIDB GENMASK(12, 9)
54#define ID0_EXIDS BIT(8)
55#define ID0_NUMSMRG GENMASK(7, 0)
56
Rob Clark2b037742017-08-09 10:43:03 -040057#define ARM_SMMU_GR0_ID1 0x24
Robin Murphy0caf5f42019-08-15 19:37:23 +010058#define ID1_PAGESIZE BIT(31)
59#define ID1_NUMPAGENDXB GENMASK(30, 28)
60#define ID1_NUMS2CB GENMASK(23, 16)
61#define ID1_NUMCB GENMASK(7, 0)
62
Rob Clark2b037742017-08-09 10:43:03 -040063#define ARM_SMMU_GR0_ID2 0x28
Robin Murphy0caf5f42019-08-15 19:37:23 +010064#define ID2_VMID16 BIT(15)
65#define ID2_PTFS_64K BIT(14)
66#define ID2_PTFS_16K BIT(13)
67#define ID2_PTFS_4K BIT(12)
68#define ID2_UBS GENMASK(11, 8)
69#define ID2_OAS GENMASK(7, 4)
70#define ID2_IAS GENMASK(3, 0)
71
Rob Clark2b037742017-08-09 10:43:03 -040072#define ARM_SMMU_GR0_ID3 0x2c
73#define ARM_SMMU_GR0_ID4 0x30
74#define ARM_SMMU_GR0_ID5 0x34
75#define ARM_SMMU_GR0_ID6 0x38
Robin Murphy0caf5f42019-08-15 19:37:23 +010076
Rob Clark2b037742017-08-09 10:43:03 -040077#define ARM_SMMU_GR0_ID7 0x3c
Robin Murphy0caf5f42019-08-15 19:37:23 +010078#define ID7_MAJOR GENMASK(7, 4)
79#define ID7_MINOR GENMASK(3, 0)
80
Rob Clark2b037742017-08-09 10:43:03 -040081#define ARM_SMMU_GR0_sGFSR 0x48
82#define ARM_SMMU_GR0_sGFSYNR0 0x50
83#define ARM_SMMU_GR0_sGFSYNR1 0x54
84#define ARM_SMMU_GR0_sGFSYNR2 0x58
85
Rob Clark2b037742017-08-09 10:43:03 -040086/* Global TLB invalidation */
87#define ARM_SMMU_GR0_TLBIVMID 0x64
88#define ARM_SMMU_GR0_TLBIALLNSNH 0x68
89#define ARM_SMMU_GR0_TLBIALLH 0x6c
90#define ARM_SMMU_GR0_sTLBGSYNC 0x70
Robin Murphy0caf5f42019-08-15 19:37:23 +010091
Rob Clark2b037742017-08-09 10:43:03 -040092#define ARM_SMMU_GR0_sTLBGSTATUS 0x74
Robin Murphy0caf5f42019-08-15 19:37:23 +010093#define sTLBGSTATUS_GSACTIVE BIT(0)
Rob Clark2b037742017-08-09 10:43:03 -040094
95/* Stream mapping registers */
96#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
Robin Murphy0caf5f42019-08-15 19:37:23 +010097#define SMR_VALID BIT(31)
98#define SMR_MASK GENMASK(31, 16)
99#define SMR_ID GENMASK(15, 0)
Rob Clark2b037742017-08-09 10:43:03 -0400100
101#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
Robin Murphy0caf5f42019-08-15 19:37:23 +0100102#define S2CR_PRIVCFG GENMASK(25, 24)
Rob Clark2b037742017-08-09 10:43:03 -0400103enum arm_smmu_s2cr_privcfg {
104 S2CR_PRIVCFG_DEFAULT,
105 S2CR_PRIVCFG_DIPAN,
106 S2CR_PRIVCFG_UNPRIV,
107 S2CR_PRIVCFG_PRIV,
108};
Robin Murphy0caf5f42019-08-15 19:37:23 +0100109#define S2CR_TYPE GENMASK(17, 16)
110enum arm_smmu_s2cr_type {
111 S2CR_TYPE_TRANS,
112 S2CR_TYPE_BYPASS,
113 S2CR_TYPE_FAULT,
114};
115#define S2CR_EXIDVALID BIT(10)
116#define S2CR_CBNDX GENMASK(7, 0)
Rob Clark2b037742017-08-09 10:43:03 -0400117
118/* Context bank attribute registers */
119#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
Robin Murphy5114e962019-08-15 19:37:24 +0100120#define CBAR_IRPTNDX GENMASK(31, 24)
121#define CBAR_TYPE GENMASK(17, 16)
122enum arm_smmu_cbar_type {
123 CBAR_TYPE_S2_TRANS,
124 CBAR_TYPE_S1_TRANS_S2_BYPASS,
125 CBAR_TYPE_S1_TRANS_S2_FAULT,
126 CBAR_TYPE_S1_TRANS_S2_TRANS,
127};
128#define CBAR_S1_MEMATTR GENMASK(15, 12)
Rob Clark2b037742017-08-09 10:43:03 -0400129#define CBAR_S1_MEMATTR_WB 0xf
Robin Murphy5114e962019-08-15 19:37:24 +0100130#define CBAR_S1_BPSHCFG GENMASK(9, 8)
131#define CBAR_S1_BPSHCFG_NSH 3
132#define CBAR_VMID GENMASK(7, 0)
Rob Clark2b037742017-08-09 10:43:03 -0400133
Vivek Gautambc580b52019-04-22 12:40:36 +0530134#define ARM_SMMU_GR1_CBFRSYNRA(n) (0x400 + ((n) << 2))
135
Rob Clark2b037742017-08-09 10:43:03 -0400136#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
Robin Murphy5114e962019-08-15 19:37:24 +0100137#define CBA2R_VMID16 GENMASK(31, 16)
138#define CBA2R_VA64 BIT(0)
Rob Clark2b037742017-08-09 10:43:03 -0400139
140#define ARM_SMMU_CB_SCTLR 0x0
Robin Murphy620565a2019-08-15 19:37:25 +0100141#define SCTLR_S1_ASIDPNE BIT(12)
142#define SCTLR_CFCFG BIT(7)
143#define SCTLR_CFIE BIT(6)
144#define SCTLR_CFRE BIT(5)
145#define SCTLR_E BIT(4)
146#define SCTLR_AFE BIT(2)
147#define SCTLR_TRE BIT(1)
148#define SCTLR_M BIT(0)
149
Rob Clark2b037742017-08-09 10:43:03 -0400150#define ARM_SMMU_CB_ACTLR 0x4
Robin Murphy620565a2019-08-15 19:37:25 +0100151
Rob Clark2b037742017-08-09 10:43:03 -0400152#define ARM_SMMU_CB_RESUME 0x8
Robin Murphy620565a2019-08-15 19:37:25 +0100153#define RESUME_TERMINATE BIT(0)
154
155#define ARM_SMMU_CB_TCR2 0x10
156#define TCR2_SEP GENMASK(17, 15)
157#define TCR2_SEP_UPSTREAM 0x7
158#define TCR2_AS BIT(4)
159
Rob Clark2b037742017-08-09 10:43:03 -0400160#define ARM_SMMU_CB_TTBR0 0x20
161#define ARM_SMMU_CB_TTBR1 0x28
Robin Murphy620565a2019-08-15 19:37:25 +0100162#define TTBRn_ASID GENMASK_ULL(63, 48)
163
164#define ARM_SMMU_CB_TCR 0x30
Rob Clark2b037742017-08-09 10:43:03 -0400165#define ARM_SMMU_CB_CONTEXTIDR 0x34
166#define ARM_SMMU_CB_S1_MAIR0 0x38
167#define ARM_SMMU_CB_S1_MAIR1 0x3c
Robin Murphy620565a2019-08-15 19:37:25 +0100168
Rob Clark2b037742017-08-09 10:43:03 -0400169#define ARM_SMMU_CB_PAR 0x50
Robin Murphy620565a2019-08-15 19:37:25 +0100170#define CB_PAR_F BIT(0)
171
Rob Clark2b037742017-08-09 10:43:03 -0400172#define ARM_SMMU_CB_FSR 0x58
Robin Murphy620565a2019-08-15 19:37:25 +0100173#define FSR_MULTI BIT(31)
174#define FSR_SS BIT(30)
175#define FSR_UUT BIT(8)
176#define FSR_ASF BIT(7)
177#define FSR_TLBLKF BIT(6)
178#define FSR_TLBMCF BIT(5)
179#define FSR_EF BIT(4)
180#define FSR_PF BIT(3)
181#define FSR_AFF BIT(2)
182#define FSR_TF BIT(1)
183
184#define FSR_IGN (FSR_AFF | FSR_ASF | \
185 FSR_TLBMCF | FSR_TLBLKF)
186#define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
187 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
188
Rob Clark2b037742017-08-09 10:43:03 -0400189#define ARM_SMMU_CB_FAR 0x60
Robin Murphy620565a2019-08-15 19:37:25 +0100190
Rob Clark2b037742017-08-09 10:43:03 -0400191#define ARM_SMMU_CB_FSYNR0 0x68
Robin Murphy620565a2019-08-15 19:37:25 +0100192#define FSYNR0_WNR BIT(4)
193
Rob Clark2b037742017-08-09 10:43:03 -0400194#define ARM_SMMU_CB_S1_TLBIVA 0x600
195#define ARM_SMMU_CB_S1_TLBIASID 0x610
196#define ARM_SMMU_CB_S1_TLBIVAL 0x620
197#define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
198#define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
199#define ARM_SMMU_CB_TLBSYNC 0x7f0
200#define ARM_SMMU_CB_TLBSTATUS 0x7f4
201#define ARM_SMMU_CB_ATS1PR 0x800
Robin Murphy620565a2019-08-15 19:37:25 +0100202
Rob Clark2b037742017-08-09 10:43:03 -0400203#define ARM_SMMU_CB_ATSR 0x8f0
Robin Murphy620565a2019-08-15 19:37:25 +0100204#define ATSR_ACTIVE BIT(0)
Rob Clark2b037742017-08-09 10:43:03 -0400205
Robin Murphyfc058d32019-08-15 19:37:33 +0100206
207/* Maximum number of context banks per SMMU */
208#define ARM_SMMU_MAX_CBS 128
209
210
211/* Shared driver definitions */
212enum arm_smmu_arch_version {
213 ARM_SMMU_V1,
214 ARM_SMMU_V1_64K,
215 ARM_SMMU_V2,
216};
217
218enum arm_smmu_implementation {
219 GENERIC_SMMU,
220 ARM_MMU500,
221 CAVIUM_SMMUV2,
222 QCOM_SMMUV2,
223};
224
225struct arm_smmu_device {
226 struct device *dev;
227
228 void __iomem *base;
229 unsigned int numpage;
230 unsigned int pgshift;
231
232#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
233#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
234#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
235#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
236#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
237#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
238#define ARM_SMMU_FEAT_VMID16 (1 << 6)
239#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
240#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
241#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
242#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
243#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
244#define ARM_SMMU_FEAT_EXIDS (1 << 12)
245 u32 features;
246
Robin Murphyfc058d32019-08-15 19:37:33 +0100247 enum arm_smmu_arch_version version;
248 enum arm_smmu_implementation model;
Robin Murphy6d7dff62019-08-15 19:37:34 +0100249 const struct arm_smmu_impl *impl;
Robin Murphyfc058d32019-08-15 19:37:33 +0100250
251 u32 num_context_banks;
252 u32 num_s2_context_banks;
253 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
254 struct arm_smmu_cb *cbs;
255 atomic_t irptndx;
256
257 u32 num_mapping_groups;
258 u16 streamid_mask;
259 u16 smr_mask_mask;
260 struct arm_smmu_smr *smrs;
261 struct arm_smmu_s2cr *s2crs;
262 struct mutex stream_map_mutex;
263
264 unsigned long va_size;
265 unsigned long ipa_size;
266 unsigned long pa_size;
267 unsigned long pgsize_bitmap;
268
269 u32 num_global_irqs;
270 u32 num_context_irqs;
271 unsigned int *irqs;
272 struct clk_bulk_data *clks;
273 int num_clks;
274
Robin Murphyfc058d32019-08-15 19:37:33 +0100275 spinlock_t global_sync_lock;
276
277 /* IOMMU core code handle */
278 struct iommu_device iommu;
279};
280
Robin Murphyba7e4a02019-08-15 19:37:37 +0100281enum arm_smmu_context_fmt {
282 ARM_SMMU_CTX_FMT_NONE,
283 ARM_SMMU_CTX_FMT_AARCH64,
284 ARM_SMMU_CTX_FMT_AARCH32_L,
285 ARM_SMMU_CTX_FMT_AARCH32_S,
286};
287
288struct arm_smmu_cfg {
289 u8 cbndx;
290 u8 irptndx;
291 union {
292 u16 asid;
293 u16 vmid;
294 };
295 enum arm_smmu_cbar_type cbar;
296 enum arm_smmu_context_fmt fmt;
297};
298#define INVALID_IRPTNDX 0xff
299
300enum arm_smmu_domain_stage {
301 ARM_SMMU_DOMAIN_S1 = 0,
302 ARM_SMMU_DOMAIN_S2,
303 ARM_SMMU_DOMAIN_NESTED,
304 ARM_SMMU_DOMAIN_BYPASS,
305};
306
307struct arm_smmu_domain {
308 struct arm_smmu_device *smmu;
309 struct io_pgtable_ops *pgtbl_ops;
310 const struct iommu_gather_ops *tlb_ops;
311 struct arm_smmu_cfg cfg;
312 enum arm_smmu_domain_stage stage;
313 bool non_strict;
314 struct mutex init_mutex; /* Protects smmu pointer */
315 spinlock_t cb_lock; /* Serialises ATS1* ops and TLB syncs */
316 struct iommu_domain domain;
317};
318
Robin Murphyfc058d32019-08-15 19:37:33 +0100319
320/* Implementation details, yay! */
Robin Murphy6d7dff62019-08-15 19:37:34 +0100321struct arm_smmu_impl {
322 u32 (*read_reg)(struct arm_smmu_device *smmu, int page, int offset);
323 void (*write_reg)(struct arm_smmu_device *smmu, int page, int offset,
324 u32 val);
325 u64 (*read_reg64)(struct arm_smmu_device *smmu, int page, int offset);
326 void (*write_reg64)(struct arm_smmu_device *smmu, int page, int offset,
327 u64 val);
Robin Murphy3995e182019-08-15 19:37:35 +0100328 int (*cfg_probe)(struct arm_smmu_device *smmu);
Robin Murphy62b993a2019-08-15 19:37:36 +0100329 int (*reset)(struct arm_smmu_device *smmu);
Robin Murphyba7e4a02019-08-15 19:37:37 +0100330 int (*init_context)(struct arm_smmu_domain *smmu_domain);
Robin Murphy6d7dff62019-08-15 19:37:34 +0100331};
332
333static inline void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n)
334{
335 return smmu->base + (n << smmu->pgshift);
336}
337
338static inline u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset)
339{
340 if (smmu->impl && unlikely(smmu->impl->read_reg))
341 return smmu->impl->read_reg(smmu, page, offset);
342 return readl_relaxed(arm_smmu_page(smmu, page) + offset);
343}
344
345static inline void arm_smmu_writel(struct arm_smmu_device *smmu, int page,
346 int offset, u32 val)
347{
348 if (smmu->impl && unlikely(smmu->impl->write_reg))
349 smmu->impl->write_reg(smmu, page, offset, val);
350 else
351 writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
352}
353
354static inline u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset)
355{
356 if (smmu->impl && unlikely(smmu->impl->read_reg64))
357 return smmu->impl->read_reg64(smmu, page, offset);
358 return readq_relaxed(arm_smmu_page(smmu, page) + offset);
359}
360
361static inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page,
362 int offset, u64 val)
363{
364 if (smmu->impl && unlikely(smmu->impl->write_reg64))
365 smmu->impl->write_reg64(smmu, page, offset, val);
366 else
367 writeq_relaxed(val, arm_smmu_page(smmu, page) + offset);
368}
369
370#define ARM_SMMU_GR0 0
371#define ARM_SMMU_GR1 1
372#define ARM_SMMU_CB(s, n) ((s)->numpage + (n))
373
374#define arm_smmu_gr0_read(s, o) \
375 arm_smmu_readl((s), ARM_SMMU_GR0, (o))
376#define arm_smmu_gr0_write(s, o, v) \
377 arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v))
378
379#define arm_smmu_gr1_read(s, o) \
380 arm_smmu_readl((s), ARM_SMMU_GR1, (o))
381#define arm_smmu_gr1_write(s, o, v) \
382 arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v))
383
384#define arm_smmu_cb_read(s, n, o) \
385 arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o))
386#define arm_smmu_cb_write(s, n, o, v) \
387 arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v))
388#define arm_smmu_cb_readq(s, n, o) \
389 arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o))
390#define arm_smmu_cb_writeq(s, n, o, v) \
391 arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
392
Robin Murphyfc058d32019-08-15 19:37:33 +0100393struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu);
394
Robin Murphyc5fc6482019-08-15 19:37:32 +0100395#endif /* _ARM_SMMU_H */