]>
Commit | Line | Data |
---|---|---|
57d3f11c | 1 | // SPDX-License-Identifier: GPL-2.0 |
d25a2a16 LP |
2 | /* |
3 | * IPMMU VMSA | |
4 | * | |
5 | * Copyright (C) 2014 Renesas Electronics Corporation | |
d25a2a16 LP |
6 | */ |
7 | ||
dbb70692 | 8 | #include <linux/bitmap.h> |
d25a2a16 | 9 | #include <linux/delay.h> |
3ae47292 | 10 | #include <linux/dma-iommu.h> |
d25a2a16 LP |
11 | #include <linux/dma-mapping.h> |
12 | #include <linux/err.h> | |
13 | #include <linux/export.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/io.h> | |
16 | #include <linux/iommu.h> | |
17 | #include <linux/module.h> | |
275f5053 | 18 | #include <linux/of.h> |
33f3ac9b | 19 | #include <linux/of_device.h> |
cda52fcd | 20 | #include <linux/of_iommu.h> |
7b2d5961 | 21 | #include <linux/of_platform.h> |
d25a2a16 LP |
22 | #include <linux/platform_device.h> |
23 | #include <linux/sizes.h> | |
24 | #include <linux/slab.h> | |
58b8e8bf | 25 | #include <linux/sys_soc.h> |
d25a2a16 | 26 | |
3ae47292 | 27 | #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) |
d25a2a16 LP |
28 | #include <asm/dma-iommu.h> |
29 | #include <asm/pgalloc.h> | |
49c875f0 RM |
30 | #else |
31 | #define arm_iommu_create_mapping(...) NULL | |
32 | #define arm_iommu_attach_device(...) -ENODEV | |
33 | #define arm_iommu_release_mapping(...) do {} while (0) | |
34 | #define arm_iommu_detach_device(...) do {} while (0) | |
3ae47292 | 35 | #endif |
d25a2a16 | 36 | |
f20ed39f LP |
37 | #include "io-pgtable.h" |
38 | ||
5fd16341 | 39 | #define IPMMU_CTX_MAX 8 |
dbb70692 | 40 | |
33f3ac9b MD |
41 | struct ipmmu_features { |
42 | bool use_ns_alias_offset; | |
fd5140e2 | 43 | bool has_cache_leaf_nodes; |
5fd16341 | 44 | unsigned int number_of_contexts; |
f5c85891 | 45 | bool setup_imbuscr; |
c295f504 | 46 | bool twobit_imttbcr_sl0; |
2ae86955 | 47 | bool reserved_context; |
33f3ac9b MD |
48 | }; |
49 | ||
d25a2a16 LP |
50 | struct ipmmu_vmsa_device { |
51 | struct device *dev; | |
52 | void __iomem *base; | |
01da21e5 | 53 | struct iommu_device iommu; |
fd5140e2 | 54 | struct ipmmu_vmsa_device *root; |
33f3ac9b | 55 | const struct ipmmu_features *features; |
d25a2a16 | 56 | unsigned int num_utlbs; |
5fd16341 | 57 | unsigned int num_ctx; |
dbb70692 MD |
58 | spinlock_t lock; /* Protects ctx and domains[] */ |
59 | DECLARE_BITMAP(ctx, IPMMU_CTX_MAX); | |
60 | struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX]; | |
d25a2a16 | 61 | |
b354c73e | 62 | struct iommu_group *group; |
d25a2a16 LP |
63 | struct dma_iommu_mapping *mapping; |
64 | }; | |
65 | ||
66 | struct ipmmu_vmsa_domain { | |
67 | struct ipmmu_vmsa_device *mmu; | |
5914c5fd | 68 | struct iommu_domain io_domain; |
d25a2a16 | 69 | |
f20ed39f LP |
70 | struct io_pgtable_cfg cfg; |
71 | struct io_pgtable_ops *iop; | |
72 | ||
d25a2a16 | 73 | unsigned int context_id; |
46583e8c | 74 | struct mutex mutex; /* Protects mappings */ |
d25a2a16 LP |
75 | }; |
76 | ||
5914c5fd JR |
77 | static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom) |
78 | { | |
79 | return container_of(dom, struct ipmmu_vmsa_domain, io_domain); | |
80 | } | |
81 | ||
e4efe4a9 | 82 | static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev) |
0fbc8b04 | 83 | { |
3c49ed32 | 84 | return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL; |
0fbc8b04 MD |
85 | } |
86 | ||
d25a2a16 LP |
87 | #define TLB_LOOP_TIMEOUT 100 /* 100us */ |
88 | ||
89 | /* ----------------------------------------------------------------------------- | |
90 | * Registers Definition | |
91 | */ | |
92 | ||
275f5053 LP |
93 | #define IM_NS_ALIAS_OFFSET 0x800 |
94 | ||
d25a2a16 LP |
95 | #define IM_CTX_SIZE 0x40 |
96 | ||
97 | #define IMCTR 0x0000 | |
98 | #define IMCTR_TRE (1 << 17) | |
99 | #define IMCTR_AFE (1 << 16) | |
100 | #define IMCTR_RTSEL_MASK (3 << 4) | |
101 | #define IMCTR_RTSEL_SHIFT 4 | |
102 | #define IMCTR_TREN (1 << 3) | |
103 | #define IMCTR_INTEN (1 << 2) | |
104 | #define IMCTR_FLUSH (1 << 1) | |
105 | #define IMCTR_MMUEN (1 << 0) | |
106 | ||
107 | #define IMCAAR 0x0004 | |
108 | ||
109 | #define IMTTBCR 0x0008 | |
110 | #define IMTTBCR_EAE (1 << 31) | |
111 | #define IMTTBCR_PMB (1 << 30) | |
112 | #define IMTTBCR_SH1_NON_SHAREABLE (0 << 28) | |
113 | #define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28) | |
114 | #define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28) | |
115 | #define IMTTBCR_SH1_MASK (3 << 28) | |
116 | #define IMTTBCR_ORGN1_NC (0 << 26) | |
117 | #define IMTTBCR_ORGN1_WB_WA (1 << 26) | |
118 | #define IMTTBCR_ORGN1_WT (2 << 26) | |
119 | #define IMTTBCR_ORGN1_WB (3 << 26) | |
120 | #define IMTTBCR_ORGN1_MASK (3 << 26) | |
121 | #define IMTTBCR_IRGN1_NC (0 << 24) | |
122 | #define IMTTBCR_IRGN1_WB_WA (1 << 24) | |
123 | #define IMTTBCR_IRGN1_WT (2 << 24) | |
124 | #define IMTTBCR_IRGN1_WB (3 << 24) | |
125 | #define IMTTBCR_IRGN1_MASK (3 << 24) | |
126 | #define IMTTBCR_TSZ1_MASK (7 << 16) | |
127 | #define IMTTBCR_TSZ1_SHIFT 16 | |
128 | #define IMTTBCR_SH0_NON_SHAREABLE (0 << 12) | |
129 | #define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12) | |
130 | #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) | |
131 | #define IMTTBCR_SH0_MASK (3 << 12) | |
132 | #define IMTTBCR_ORGN0_NC (0 << 10) | |
133 | #define IMTTBCR_ORGN0_WB_WA (1 << 10) | |
134 | #define IMTTBCR_ORGN0_WT (2 << 10) | |
135 | #define IMTTBCR_ORGN0_WB (3 << 10) | |
136 | #define IMTTBCR_ORGN0_MASK (3 << 10) | |
137 | #define IMTTBCR_IRGN0_NC (0 << 8) | |
138 | #define IMTTBCR_IRGN0_WB_WA (1 << 8) | |
139 | #define IMTTBCR_IRGN0_WT (2 << 8) | |
140 | #define IMTTBCR_IRGN0_WB (3 << 8) | |
141 | #define IMTTBCR_IRGN0_MASK (3 << 8) | |
142 | #define IMTTBCR_SL0_LVL_2 (0 << 4) | |
143 | #define IMTTBCR_SL0_LVL_1 (1 << 4) | |
144 | #define IMTTBCR_TSZ0_MASK (7 << 0) | |
145 | #define IMTTBCR_TSZ0_SHIFT O | |
146 | ||
c295f504 MD |
147 | #define IMTTBCR_SL0_TWOBIT_LVL_3 (0 << 6) |
148 | #define IMTTBCR_SL0_TWOBIT_LVL_2 (1 << 6) | |
149 | #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) | |
150 | ||
d25a2a16 LP |
151 | #define IMBUSCR 0x000c |
152 | #define IMBUSCR_DVM (1 << 2) | |
153 | #define IMBUSCR_BUSSEL_SYS (0 << 0) | |
154 | #define IMBUSCR_BUSSEL_CCI (1 << 0) | |
155 | #define IMBUSCR_BUSSEL_IMCAAR (2 << 0) | |
156 | #define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0) | |
157 | #define IMBUSCR_BUSSEL_MASK (3 << 0) | |
158 | ||
159 | #define IMTTLBR0 0x0010 | |
160 | #define IMTTUBR0 0x0014 | |
161 | #define IMTTLBR1 0x0018 | |
162 | #define IMTTUBR1 0x001c | |
163 | ||
164 | #define IMSTR 0x0020 | |
165 | #define IMSTR_ERRLVL_MASK (3 << 12) | |
166 | #define IMSTR_ERRLVL_SHIFT 12 | |
167 | #define IMSTR_ERRCODE_TLB_FORMAT (1 << 8) | |
168 | #define IMSTR_ERRCODE_ACCESS_PERM (4 << 8) | |
169 | #define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8) | |
170 | #define IMSTR_ERRCODE_MASK (7 << 8) | |
171 | #define IMSTR_MHIT (1 << 4) | |
172 | #define IMSTR_ABORT (1 << 2) | |
173 | #define IMSTR_PF (1 << 1) | |
174 | #define IMSTR_TF (1 << 0) | |
175 | ||
176 | #define IMMAIR0 0x0028 | |
177 | #define IMMAIR1 0x002c | |
178 | #define IMMAIR_ATTR_MASK 0xff | |
179 | #define IMMAIR_ATTR_DEVICE 0x04 | |
180 | #define IMMAIR_ATTR_NC 0x44 | |
181 | #define IMMAIR_ATTR_WBRWA 0xff | |
182 | #define IMMAIR_ATTR_SHIFT(n) ((n) << 3) | |
183 | #define IMMAIR_ATTR_IDX_NC 0 | |
184 | #define IMMAIR_ATTR_IDX_WBRWA 1 | |
185 | #define IMMAIR_ATTR_IDX_DEV 2 | |
186 | ||
187 | #define IMEAR 0x0030 | |
188 | ||
189 | #define IMPCTR 0x0200 | |
190 | #define IMPSTR 0x0208 | |
191 | #define IMPEAR 0x020c | |
192 | #define IMPMBA(n) (0x0280 + ((n) * 4)) | |
193 | #define IMPMBD(n) (0x02c0 + ((n) * 4)) | |
194 | ||
ddbbddd7 MD |
195 | #define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n)) |
196 | #define IMUCTR0(n) (0x0300 + ((n) * 16)) | |
197 | #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) | |
d25a2a16 LP |
198 | #define IMUCTR_FIXADDEN (1 << 31) |
199 | #define IMUCTR_FIXADD_MASK (0xff << 16) | |
200 | #define IMUCTR_FIXADD_SHIFT 16 | |
201 | #define IMUCTR_TTSEL_MMU(n) ((n) << 4) | |
202 | #define IMUCTR_TTSEL_PMB (8 << 4) | |
203 | #define IMUCTR_TTSEL_MASK (15 << 4) | |
204 | #define IMUCTR_FLUSH (1 << 1) | |
205 | #define IMUCTR_MMUEN (1 << 0) | |
206 | ||
ddbbddd7 MD |
207 | #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n)) |
208 | #define IMUASID0(n) (0x0308 + ((n) * 16)) | |
209 | #define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) | |
d25a2a16 LP |
210 | #define IMUASID_ASID8_MASK (0xff << 8) |
211 | #define IMUASID_ASID8_SHIFT 8 | |
212 | #define IMUASID_ASID0_MASK (0xff << 0) | |
213 | #define IMUASID_ASID0_SHIFT 0 | |
214 | ||
fd5140e2 MD |
215 | /* ----------------------------------------------------------------------------- |
216 | * Root device handling | |
217 | */ | |
218 | ||
219 | static struct platform_driver ipmmu_driver; | |
220 | ||
221 | static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu) | |
222 | { | |
223 | return mmu->root == mmu; | |
224 | } | |
225 | ||
226 | static int __ipmmu_check_device(struct device *dev, void *data) | |
227 | { | |
228 | struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev); | |
229 | struct ipmmu_vmsa_device **rootp = data; | |
230 | ||
231 | if (ipmmu_is_root(mmu)) | |
232 | *rootp = mmu; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static struct ipmmu_vmsa_device *ipmmu_find_root(void) | |
238 | { | |
239 | struct ipmmu_vmsa_device *root = NULL; | |
240 | ||
241 | return driver_for_each_device(&ipmmu_driver.driver, NULL, &root, | |
242 | __ipmmu_check_device) == 0 ? root : NULL; | |
243 | } | |
244 | ||
d25a2a16 LP |
245 | /* ----------------------------------------------------------------------------- |
246 | * Read/Write Access | |
247 | */ | |
248 | ||
249 | static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset) | |
250 | { | |
251 | return ioread32(mmu->base + offset); | |
252 | } | |
253 | ||
254 | static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset, | |
255 | u32 data) | |
256 | { | |
257 | iowrite32(data, mmu->base + offset); | |
258 | } | |
259 | ||
d574893a MD |
260 | static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain, |
261 | unsigned int reg) | |
d25a2a16 | 262 | { |
fd5140e2 MD |
263 | return ipmmu_read(domain->mmu->root, |
264 | domain->context_id * IM_CTX_SIZE + reg); | |
d25a2a16 LP |
265 | } |
266 | ||
d574893a MD |
267 | static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain, |
268 | unsigned int reg, u32 data) | |
d25a2a16 | 269 | { |
fd5140e2 MD |
270 | ipmmu_write(domain->mmu->root, |
271 | domain->context_id * IM_CTX_SIZE + reg, data); | |
d25a2a16 LP |
272 | } |
273 | ||
d574893a MD |
274 | static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain, |
275 | unsigned int reg, u32 data) | |
276 | { | |
277 | if (domain->mmu != domain->mmu->root) | |
278 | ipmmu_write(domain->mmu, | |
279 | domain->context_id * IM_CTX_SIZE + reg, data); | |
280 | ||
281 | ipmmu_write(domain->mmu->root, | |
282 | domain->context_id * IM_CTX_SIZE + reg, data); | |
283 | } | |
284 | ||
d25a2a16 LP |
285 | /* ----------------------------------------------------------------------------- |
286 | * TLB and microTLB Management | |
287 | */ | |
288 | ||
289 | /* Wait for any pending TLB invalidations to complete */ | |
290 | static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain) | |
291 | { | |
292 | unsigned int count = 0; | |
293 | ||
d574893a | 294 | while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) { |
d25a2a16 LP |
295 | cpu_relax(); |
296 | if (++count == TLB_LOOP_TIMEOUT) { | |
297 | dev_err_ratelimited(domain->mmu->dev, | |
298 | "TLB sync timed out -- MMU may be deadlocked\n"); | |
299 | return; | |
300 | } | |
301 | udelay(1); | |
302 | } | |
303 | } | |
304 | ||
305 | static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) | |
306 | { | |
307 | u32 reg; | |
308 | ||
d574893a | 309 | reg = ipmmu_ctx_read_root(domain, IMCTR); |
d25a2a16 | 310 | reg |= IMCTR_FLUSH; |
d574893a | 311 | ipmmu_ctx_write_all(domain, IMCTR, reg); |
d25a2a16 LP |
312 | |
313 | ipmmu_tlb_sync(domain); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Enable MMU translation for the microTLB. | |
318 | */ | |
319 | static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain, | |
192d2045 | 320 | unsigned int utlb) |
d25a2a16 LP |
321 | { |
322 | struct ipmmu_vmsa_device *mmu = domain->mmu; | |
323 | ||
192d2045 LP |
324 | /* |
325 | * TODO: Reference-count the microTLB as several bus masters can be | |
326 | * connected to the same microTLB. | |
327 | */ | |
328 | ||
d25a2a16 | 329 | /* TODO: What should we set the ASID to ? */ |
192d2045 | 330 | ipmmu_write(mmu, IMUASID(utlb), 0); |
d25a2a16 | 331 | /* TODO: Do we need to flush the microTLB ? */ |
192d2045 | 332 | ipmmu_write(mmu, IMUCTR(utlb), |
d25a2a16 LP |
333 | IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH | |
334 | IMUCTR_MMUEN); | |
335 | } | |
336 | ||
337 | /* | |
338 | * Disable MMU translation for the microTLB. | |
339 | */ | |
340 | static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain, | |
192d2045 | 341 | unsigned int utlb) |
d25a2a16 LP |
342 | { |
343 | struct ipmmu_vmsa_device *mmu = domain->mmu; | |
344 | ||
192d2045 | 345 | ipmmu_write(mmu, IMUCTR(utlb), 0); |
d25a2a16 LP |
346 | } |
347 | ||
f20ed39f | 348 | static void ipmmu_tlb_flush_all(void *cookie) |
d25a2a16 | 349 | { |
f20ed39f LP |
350 | struct ipmmu_vmsa_domain *domain = cookie; |
351 | ||
352 | ipmmu_tlb_invalidate(domain); | |
353 | } | |
354 | ||
06c610e8 RM |
355 | static void ipmmu_tlb_add_flush(unsigned long iova, size_t size, |
356 | size_t granule, bool leaf, void *cookie) | |
f20ed39f LP |
357 | { |
358 | /* The hardware doesn't support selective TLB flush. */ | |
359 | } | |
360 | ||
8da4af95 | 361 | static const struct iommu_gather_ops ipmmu_gather_ops = { |
f20ed39f LP |
362 | .tlb_flush_all = ipmmu_tlb_flush_all, |
363 | .tlb_add_flush = ipmmu_tlb_add_flush, | |
364 | .tlb_sync = ipmmu_tlb_flush_all, | |
f20ed39f LP |
365 | }; |
366 | ||
d25a2a16 LP |
367 | /* ----------------------------------------------------------------------------- |
368 | * Domain/Context Management | |
369 | */ | |
370 | ||
dbb70692 MD |
371 | static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu, |
372 | struct ipmmu_vmsa_domain *domain) | |
373 | { | |
374 | unsigned long flags; | |
375 | int ret; | |
376 | ||
377 | spin_lock_irqsave(&mmu->lock, flags); | |
378 | ||
5fd16341 MD |
379 | ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx); |
380 | if (ret != mmu->num_ctx) { | |
dbb70692 MD |
381 | mmu->domains[ret] = domain; |
382 | set_bit(ret, mmu->ctx); | |
5fd16341 MD |
383 | } else |
384 | ret = -EBUSY; | |
dbb70692 MD |
385 | |
386 | spin_unlock_irqrestore(&mmu->lock, flags); | |
387 | ||
388 | return ret; | |
389 | } | |
390 | ||
a175a67d OT |
391 | static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu, |
392 | unsigned int context_id) | |
393 | { | |
394 | unsigned long flags; | |
395 | ||
396 | spin_lock_irqsave(&mmu->lock, flags); | |
397 | ||
398 | clear_bit(context_id, mmu->ctx); | |
399 | mmu->domains[context_id] = NULL; | |
400 | ||
401 | spin_unlock_irqrestore(&mmu->lock, flags); | |
402 | } | |
403 | ||
d25a2a16 LP |
404 | static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) |
405 | { | |
f64232ee | 406 | u64 ttbr; |
c295f504 | 407 | u32 tmp; |
dbb70692 | 408 | int ret; |
f20ed39f LP |
409 | |
410 | /* | |
411 | * Allocate the page table operations. | |
412 | * | |
413 | * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory | |
414 | * access, Long-descriptor format" that the NStable bit being set in a | |
415 | * table descriptor will result in the NStable and NS bits of all child | |
416 | * entries being ignored and considered as being set. The IPMMU seems | |
417 | * not to comply with this, as it generates a secure access page fault | |
418 | * if any of the NStable and NS bits isn't set when running in | |
419 | * non-secure mode. | |
420 | */ | |
421 | domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS; | |
26b6aec6 | 422 | domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K; |
f20ed39f LP |
423 | domain->cfg.ias = 32; |
424 | domain->cfg.oas = 40; | |
425 | domain->cfg.tlb = &ipmmu_gather_ops; | |
3b6bb5b7 GU |
426 | domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32); |
427 | domain->io_domain.geometry.force_aperture = true; | |
ff2ed96d RM |
428 | /* |
429 | * TODO: Add support for coherent walk through CCI with DVM and remove | |
430 | * cache handling. For now, delegate it to the io-pgtable code. | |
431 | */ | |
fd5140e2 | 432 | domain->cfg.iommu_dev = domain->mmu->root->dev; |
f20ed39f | 433 | |
d25a2a16 | 434 | /* |
dbb70692 | 435 | * Find an unused context. |
d25a2a16 | 436 | */ |
fd5140e2 | 437 | ret = ipmmu_domain_allocate_context(domain->mmu->root, domain); |
5fd16341 MD |
438 | if (ret < 0) |
439 | return ret; | |
dbb70692 MD |
440 | |
441 | domain->context_id = ret; | |
d25a2a16 | 442 | |
a175a67d OT |
443 | domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, |
444 | domain); | |
445 | if (!domain->iop) { | |
fd5140e2 MD |
446 | ipmmu_domain_free_context(domain->mmu->root, |
447 | domain->context_id); | |
a175a67d OT |
448 | return -EINVAL; |
449 | } | |
450 | ||
d25a2a16 | 451 | /* TTBR0 */ |
f20ed39f | 452 | ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0]; |
d574893a MD |
453 | ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr); |
454 | ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32); | |
d25a2a16 LP |
455 | |
456 | /* | |
457 | * TTBCR | |
458 | * We use long descriptors with inner-shareable WBWA tables and allocate | |
459 | * the whole 32-bit VA space to TTBR0. | |
460 | */ | |
c295f504 MD |
461 | if (domain->mmu->features->twobit_imttbcr_sl0) |
462 | tmp = IMTTBCR_SL0_TWOBIT_LVL_1; | |
463 | else | |
464 | tmp = IMTTBCR_SL0_LVL_1; | |
465 | ||
d574893a MD |
466 | ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | |
467 | IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA | | |
c295f504 | 468 | IMTTBCR_IRGN0_WB_WA | tmp); |
d25a2a16 | 469 | |
f20ed39f | 470 | /* MAIR0 */ |
d574893a MD |
471 | ipmmu_ctx_write_root(domain, IMMAIR0, |
472 | domain->cfg.arm_lpae_s1_cfg.mair[0]); | |
d25a2a16 LP |
473 | |
474 | /* IMBUSCR */ | |
f5c85891 MD |
475 | if (domain->mmu->features->setup_imbuscr) |
476 | ipmmu_ctx_write_root(domain, IMBUSCR, | |
477 | ipmmu_ctx_read_root(domain, IMBUSCR) & | |
478 | ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK)); | |
d25a2a16 LP |
479 | |
480 | /* | |
481 | * IMSTR | |
482 | * Clear all interrupt flags. | |
483 | */ | |
d574893a | 484 | ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR)); |
d25a2a16 LP |
485 | |
486 | /* | |
487 | * IMCTR | |
488 | * Enable the MMU and interrupt generation. The long-descriptor | |
489 | * translation table format doesn't use TEX remapping. Don't enable AF | |
490 | * software management as we have no use for it. Flush the TLB as | |
491 | * required when modifying the context registers. | |
492 | */ | |
d574893a MD |
493 | ipmmu_ctx_write_all(domain, IMCTR, |
494 | IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN); | |
d25a2a16 LP |
495 | |
496 | return 0; | |
497 | } | |
498 | ||
499 | static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain) | |
500 | { | |
e5b78f2e GU |
501 | if (!domain->mmu) |
502 | return; | |
503 | ||
d25a2a16 LP |
504 | /* |
505 | * Disable the context. Flush the TLB as required when modifying the | |
506 | * context registers. | |
507 | * | |
508 | * TODO: Is TLB flush really needed ? | |
509 | */ | |
d574893a | 510 | ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH); |
d25a2a16 | 511 | ipmmu_tlb_sync(domain); |
fd5140e2 | 512 | ipmmu_domain_free_context(domain->mmu->root, domain->context_id); |
d25a2a16 LP |
513 | } |
514 | ||
515 | /* ----------------------------------------------------------------------------- | |
516 | * Fault Handling | |
517 | */ | |
518 | ||
519 | static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain) | |
520 | { | |
521 | const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF; | |
522 | struct ipmmu_vmsa_device *mmu = domain->mmu; | |
523 | u32 status; | |
524 | u32 iova; | |
525 | ||
d574893a | 526 | status = ipmmu_ctx_read_root(domain, IMSTR); |
d25a2a16 LP |
527 | if (!(status & err_mask)) |
528 | return IRQ_NONE; | |
529 | ||
d574893a | 530 | iova = ipmmu_ctx_read_root(domain, IMEAR); |
d25a2a16 LP |
531 | |
532 | /* | |
533 | * Clear the error status flags. Unlike traditional interrupt flag | |
534 | * registers that must be cleared by writing 1, this status register | |
535 | * seems to require 0. The error address register must be read before, | |
536 | * otherwise its value will be 0. | |
537 | */ | |
d574893a | 538 | ipmmu_ctx_write_root(domain, IMSTR, 0); |
d25a2a16 LP |
539 | |
540 | /* Log fatal errors. */ | |
541 | if (status & IMSTR_MHIT) | |
542 | dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n", | |
543 | iova); | |
544 | if (status & IMSTR_ABORT) | |
545 | dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n", | |
546 | iova); | |
547 | ||
548 | if (!(status & (IMSTR_PF | IMSTR_TF))) | |
549 | return IRQ_NONE; | |
550 | ||
551 | /* | |
552 | * Try to handle page faults and translation faults. | |
553 | * | |
554 | * TODO: We need to look up the faulty device based on the I/O VA. Use | |
555 | * the IOMMU device for now. | |
556 | */ | |
5914c5fd | 557 | if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0)) |
d25a2a16 LP |
558 | return IRQ_HANDLED; |
559 | ||
560 | dev_err_ratelimited(mmu->dev, | |
561 | "Unhandled fault: status 0x%08x iova 0x%08x\n", | |
562 | status, iova); | |
563 | ||
564 | return IRQ_HANDLED; | |
565 | } | |
566 | ||
567 | static irqreturn_t ipmmu_irq(int irq, void *dev) | |
568 | { | |
569 | struct ipmmu_vmsa_device *mmu = dev; | |
dbb70692 MD |
570 | irqreturn_t status = IRQ_NONE; |
571 | unsigned int i; | |
572 | unsigned long flags; | |
d25a2a16 | 573 | |
dbb70692 MD |
574 | spin_lock_irqsave(&mmu->lock, flags); |
575 | ||
576 | /* | |
577 | * Check interrupts for all active contexts. | |
578 | */ | |
5fd16341 | 579 | for (i = 0; i < mmu->num_ctx; i++) { |
dbb70692 MD |
580 | if (!mmu->domains[i]) |
581 | continue; | |
582 | if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED) | |
583 | status = IRQ_HANDLED; | |
584 | } | |
d25a2a16 | 585 | |
dbb70692 | 586 | spin_unlock_irqrestore(&mmu->lock, flags); |
d25a2a16 | 587 | |
dbb70692 | 588 | return status; |
d25a2a16 LP |
589 | } |
590 | ||
d25a2a16 LP |
591 | /* ----------------------------------------------------------------------------- |
592 | * IOMMU Operations | |
593 | */ | |
594 | ||
8e73bf65 | 595 | static struct iommu_domain *__ipmmu_domain_alloc(unsigned type) |
d25a2a16 LP |
596 | { |
597 | struct ipmmu_vmsa_domain *domain; | |
598 | ||
599 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); | |
600 | if (!domain) | |
5914c5fd | 601 | return NULL; |
d25a2a16 | 602 | |
46583e8c | 603 | mutex_init(&domain->mutex); |
d25a2a16 | 604 | |
5914c5fd | 605 | return &domain->io_domain; |
d25a2a16 LP |
606 | } |
607 | ||
1c7e7c02 RM |
608 | static struct iommu_domain *ipmmu_domain_alloc(unsigned type) |
609 | { | |
610 | struct iommu_domain *io_domain = NULL; | |
611 | ||
612 | switch (type) { | |
613 | case IOMMU_DOMAIN_UNMANAGED: | |
614 | io_domain = __ipmmu_domain_alloc(type); | |
615 | break; | |
616 | ||
617 | case IOMMU_DOMAIN_DMA: | |
618 | io_domain = __ipmmu_domain_alloc(type); | |
619 | if (io_domain && iommu_get_dma_cookie(io_domain)) { | |
620 | kfree(io_domain); | |
621 | io_domain = NULL; | |
622 | } | |
623 | break; | |
624 | } | |
625 | ||
626 | return io_domain; | |
627 | } | |
628 | ||
5914c5fd | 629 | static void ipmmu_domain_free(struct iommu_domain *io_domain) |
d25a2a16 | 630 | { |
5914c5fd | 631 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
d25a2a16 LP |
632 | |
633 | /* | |
634 | * Free the domain resources. We assume that all devices have already | |
635 | * been detached. | |
636 | */ | |
1c7e7c02 | 637 | iommu_put_dma_cookie(io_domain); |
d25a2a16 | 638 | ipmmu_domain_destroy_context(domain); |
f20ed39f | 639 | free_io_pgtable_ops(domain->iop); |
d25a2a16 LP |
640 | kfree(domain); |
641 | } | |
642 | ||
643 | static int ipmmu_attach_device(struct iommu_domain *io_domain, | |
644 | struct device *dev) | |
645 | { | |
7b2d5961 | 646 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
e4efe4a9 | 647 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
5914c5fd | 648 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
a166d31e | 649 | unsigned int i; |
d25a2a16 LP |
650 | int ret = 0; |
651 | ||
e4efe4a9 | 652 | if (!mmu) { |
d25a2a16 LP |
653 | dev_err(dev, "Cannot attach to IPMMU\n"); |
654 | return -ENXIO; | |
655 | } | |
656 | ||
46583e8c | 657 | mutex_lock(&domain->mutex); |
d25a2a16 LP |
658 | |
659 | if (!domain->mmu) { | |
660 | /* The domain hasn't been used yet, initialize it. */ | |
661 | domain->mmu = mmu; | |
662 | ret = ipmmu_domain_init_context(domain); | |
5fd16341 MD |
663 | if (ret < 0) { |
664 | dev_err(dev, "Unable to initialize IPMMU context\n"); | |
665 | domain->mmu = NULL; | |
666 | } else { | |
667 | dev_info(dev, "Using IPMMU context %u\n", | |
668 | domain->context_id); | |
669 | } | |
d25a2a16 LP |
670 | } else if (domain->mmu != mmu) { |
671 | /* | |
672 | * Something is wrong, we can't attach two devices using | |
673 | * different IOMMUs to the same domain. | |
674 | */ | |
675 | dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n", | |
676 | dev_name(mmu->dev), dev_name(domain->mmu->dev)); | |
677 | ret = -EINVAL; | |
3ae47292 MD |
678 | } else |
679 | dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id); | |
d25a2a16 | 680 | |
46583e8c | 681 | mutex_unlock(&domain->mutex); |
d25a2a16 LP |
682 | |
683 | if (ret < 0) | |
684 | return ret; | |
685 | ||
7b2d5961 MD |
686 | for (i = 0; i < fwspec->num_ids; ++i) |
687 | ipmmu_utlb_enable(domain, fwspec->ids[i]); | |
d25a2a16 LP |
688 | |
689 | return 0; | |
690 | } | |
691 | ||
692 | static void ipmmu_detach_device(struct iommu_domain *io_domain, | |
693 | struct device *dev) | |
694 | { | |
7b2d5961 | 695 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
5914c5fd | 696 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
a166d31e | 697 | unsigned int i; |
d25a2a16 | 698 | |
7b2d5961 MD |
699 | for (i = 0; i < fwspec->num_ids; ++i) |
700 | ipmmu_utlb_disable(domain, fwspec->ids[i]); | |
d25a2a16 LP |
701 | |
702 | /* | |
703 | * TODO: Optimize by disabling the context when no device is attached. | |
704 | */ | |
705 | } | |
706 | ||
707 | static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova, | |
708 | phys_addr_t paddr, size_t size, int prot) | |
709 | { | |
5914c5fd | 710 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
d25a2a16 LP |
711 | |
712 | if (!domain) | |
713 | return -ENODEV; | |
714 | ||
f20ed39f | 715 | return domain->iop->map(domain->iop, iova, paddr, size, prot); |
d25a2a16 LP |
716 | } |
717 | ||
718 | static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova, | |
719 | size_t size) | |
720 | { | |
5914c5fd | 721 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
d25a2a16 | 722 | |
f20ed39f | 723 | return domain->iop->unmap(domain->iop, iova, size); |
d25a2a16 LP |
724 | } |
725 | ||
32b12449 RM |
726 | static void ipmmu_iotlb_sync(struct iommu_domain *io_domain) |
727 | { | |
728 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); | |
729 | ||
730 | if (domain->mmu) | |
731 | ipmmu_tlb_flush_all(domain); | |
732 | } | |
733 | ||
d25a2a16 LP |
734 | static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain, |
735 | dma_addr_t iova) | |
736 | { | |
5914c5fd | 737 | struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); |
d25a2a16 LP |
738 | |
739 | /* TODO: Is locking needed ? */ | |
740 | ||
f20ed39f | 741 | return domain->iop->iova_to_phys(domain->iop, iova); |
d25a2a16 LP |
742 | } |
743 | ||
7b2d5961 MD |
744 | static int ipmmu_init_platform_device(struct device *dev, |
745 | struct of_phandle_args *args) | |
d25a2a16 | 746 | { |
7b2d5961 | 747 | struct platform_device *ipmmu_pdev; |
bb590c90 | 748 | |
7b2d5961 MD |
749 | ipmmu_pdev = of_find_device_by_node(args->np); |
750 | if (!ipmmu_pdev) | |
bb590c90 LP |
751 | return -ENODEV; |
752 | ||
e4efe4a9 | 753 | dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev); |
383fef5f | 754 | return 0; |
383fef5f MD |
755 | } |
756 | ||
58b8e8bf MD |
757 | static bool ipmmu_slave_whitelist(struct device *dev) |
758 | { | |
759 | /* By default, do not allow use of IPMMU */ | |
760 | return false; | |
761 | } | |
762 | ||
0b8ac140 | 763 | static const struct soc_device_attribute soc_rcar_gen3[] = { |
58b8e8bf | 764 | { .soc_id = "r8a7795", }, |
0b8ac140 | 765 | { .soc_id = "r8a7796", }, |
98dbffd3 | 766 | { .soc_id = "r8a77965", }, |
3701c123 SH |
767 | { .soc_id = "r8a77970", }, |
768 | { .soc_id = "r8a77995", }, | |
58b8e8bf MD |
769 | { /* sentinel */ } |
770 | }; | |
771 | ||
49558da0 MD |
772 | static int ipmmu_of_xlate(struct device *dev, |
773 | struct of_phandle_args *spec) | |
774 | { | |
58b8e8bf | 775 | /* For R-Car Gen3 use a white list to opt-in slave devices */ |
0b8ac140 | 776 | if (soc_device_match(soc_rcar_gen3) && !ipmmu_slave_whitelist(dev)) |
58b8e8bf MD |
777 | return -ENODEV; |
778 | ||
7b2d5961 MD |
779 | iommu_fwspec_add_ids(dev, spec->args, 1); |
780 | ||
49558da0 | 781 | /* Initialize once - xlate() will call multiple times */ |
e4efe4a9 | 782 | if (to_ipmmu(dev)) |
49558da0 MD |
783 | return 0; |
784 | ||
7b2d5961 | 785 | return ipmmu_init_platform_device(dev, spec); |
49558da0 MD |
786 | } |
787 | ||
49c875f0 | 788 | static int ipmmu_init_arm_mapping(struct device *dev) |
383fef5f | 789 | { |
e4efe4a9 | 790 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
383fef5f MD |
791 | struct iommu_group *group; |
792 | int ret; | |
793 | ||
d25a2a16 LP |
794 | /* Create a device group and add the device to it. */ |
795 | group = iommu_group_alloc(); | |
796 | if (IS_ERR(group)) { | |
797 | dev_err(dev, "Failed to allocate IOMMU group\n"); | |
49c875f0 | 798 | return PTR_ERR(group); |
d25a2a16 LP |
799 | } |
800 | ||
801 | ret = iommu_group_add_device(group, dev); | |
802 | iommu_group_put(group); | |
803 | ||
804 | if (ret < 0) { | |
805 | dev_err(dev, "Failed to add device to IPMMU group\n"); | |
49c875f0 | 806 | return ret; |
d25a2a16 LP |
807 | } |
808 | ||
d25a2a16 LP |
809 | /* |
810 | * Create the ARM mapping, used by the ARM DMA mapping core to allocate | |
811 | * VAs. This will allocate a corresponding IOMMU domain. | |
812 | * | |
813 | * TODO: | |
814 | * - Create one mapping per context (TLB). | |
815 | * - Make the mapping size configurable ? We currently use a 2GB mapping | |
816 | * at a 1GB offset to ensure that NULL VAs will fault. | |
817 | */ | |
818 | if (!mmu->mapping) { | |
819 | struct dma_iommu_mapping *mapping; | |
820 | ||
821 | mapping = arm_iommu_create_mapping(&platform_bus_type, | |
720b0cef | 822 | SZ_1G, SZ_2G); |
d25a2a16 LP |
823 | if (IS_ERR(mapping)) { |
824 | dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n"); | |
b8f80bff LP |
825 | ret = PTR_ERR(mapping); |
826 | goto error; | |
d25a2a16 LP |
827 | } |
828 | ||
829 | mmu->mapping = mapping; | |
830 | } | |
831 | ||
832 | /* Attach the ARM VA mapping to the device. */ | |
833 | ret = arm_iommu_attach_device(dev, mmu->mapping); | |
834 | if (ret < 0) { | |
835 | dev_err(dev, "Failed to attach device to VA mapping\n"); | |
836 | goto error; | |
837 | } | |
838 | ||
839 | return 0; | |
840 | ||
841 | error: | |
49c875f0 RM |
842 | iommu_group_remove_device(dev); |
843 | if (mmu->mapping) | |
383fef5f | 844 | arm_iommu_release_mapping(mmu->mapping); |
a166d31e | 845 | |
d25a2a16 LP |
846 | return ret; |
847 | } | |
848 | ||
49c875f0 | 849 | static int ipmmu_add_device(struct device *dev) |
3ae47292 | 850 | { |
3ae47292 MD |
851 | struct iommu_group *group; |
852 | ||
0fbc8b04 MD |
853 | /* |
854 | * Only let through devices that have been verified in xlate() | |
0fbc8b04 | 855 | */ |
e4efe4a9 | 856 | if (!to_ipmmu(dev)) |
3ae47292 MD |
857 | return -ENODEV; |
858 | ||
49c875f0 RM |
859 | if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)) |
860 | return ipmmu_init_arm_mapping(dev); | |
861 | ||
3ae47292 MD |
862 | group = iommu_group_get_for_dev(dev); |
863 | if (IS_ERR(group)) | |
864 | return PTR_ERR(group); | |
865 | ||
49c875f0 | 866 | iommu_group_put(group); |
3ae47292 MD |
867 | return 0; |
868 | } | |
869 | ||
49c875f0 | 870 | static void ipmmu_remove_device(struct device *dev) |
3ae47292 | 871 | { |
49c875f0 | 872 | arm_iommu_detach_device(dev); |
3ae47292 MD |
873 | iommu_group_remove_device(dev); |
874 | } | |
875 | ||
b354c73e | 876 | static struct iommu_group *ipmmu_find_group(struct device *dev) |
3ae47292 | 877 | { |
e4efe4a9 | 878 | struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); |
3ae47292 | 879 | struct iommu_group *group; |
3ae47292 | 880 | |
e4efe4a9 RM |
881 | if (mmu->group) |
882 | return iommu_group_ref_get(mmu->group); | |
b354c73e RM |
883 | |
884 | group = iommu_group_alloc(); | |
885 | if (!IS_ERR(group)) | |
e4efe4a9 | 886 | mmu->group = group; |
3ae47292 MD |
887 | |
888 | return group; | |
889 | } | |
890 | ||
3ae47292 | 891 | static const struct iommu_ops ipmmu_ops = { |
1c7e7c02 RM |
892 | .domain_alloc = ipmmu_domain_alloc, |
893 | .domain_free = ipmmu_domain_free, | |
3ae47292 MD |
894 | .attach_dev = ipmmu_attach_device, |
895 | .detach_dev = ipmmu_detach_device, | |
896 | .map = ipmmu_map, | |
897 | .unmap = ipmmu_unmap, | |
32b12449 RM |
898 | .flush_iotlb_all = ipmmu_iotlb_sync, |
899 | .iotlb_sync = ipmmu_iotlb_sync, | |
3ae47292 | 900 | .iova_to_phys = ipmmu_iova_to_phys, |
49c875f0 RM |
901 | .add_device = ipmmu_add_device, |
902 | .remove_device = ipmmu_remove_device, | |
b354c73e | 903 | .device_group = ipmmu_find_group, |
3ae47292 | 904 | .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, |
49558da0 | 905 | .of_xlate = ipmmu_of_xlate, |
3ae47292 MD |
906 | }; |
907 | ||
d25a2a16 LP |
908 | /* ----------------------------------------------------------------------------- |
909 | * Probe/remove and init | |
910 | */ | |
911 | ||
912 | static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu) | |
913 | { | |
914 | unsigned int i; | |
915 | ||
916 | /* Disable all contexts. */ | |
5fd16341 | 917 | for (i = 0; i < mmu->num_ctx; ++i) |
d25a2a16 LP |
918 | ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0); |
919 | } | |
920 | ||
33f3ac9b MD |
921 | static const struct ipmmu_features ipmmu_features_default = { |
922 | .use_ns_alias_offset = true, | |
fd5140e2 | 923 | .has_cache_leaf_nodes = false, |
5fd16341 | 924 | .number_of_contexts = 1, /* software only tested with one context */ |
f5c85891 | 925 | .setup_imbuscr = true, |
c295f504 | 926 | .twobit_imttbcr_sl0 = false, |
2ae86955 | 927 | .reserved_context = false, |
33f3ac9b MD |
928 | }; |
929 | ||
0b8ac140 | 930 | static const struct ipmmu_features ipmmu_features_rcar_gen3 = { |
58b8e8bf MD |
931 | .use_ns_alias_offset = false, |
932 | .has_cache_leaf_nodes = true, | |
933 | .number_of_contexts = 8, | |
934 | .setup_imbuscr = false, | |
935 | .twobit_imttbcr_sl0 = true, | |
2ae86955 | 936 | .reserved_context = true, |
58b8e8bf MD |
937 | }; |
938 | ||
33f3ac9b MD |
939 | static const struct of_device_id ipmmu_of_ids[] = { |
940 | { | |
941 | .compatible = "renesas,ipmmu-vmsa", | |
942 | .data = &ipmmu_features_default, | |
58b8e8bf MD |
943 | }, { |
944 | .compatible = "renesas,ipmmu-r8a7795", | |
0b8ac140 MD |
945 | .data = &ipmmu_features_rcar_gen3, |
946 | }, { | |
947 | .compatible = "renesas,ipmmu-r8a7796", | |
948 | .data = &ipmmu_features_rcar_gen3, | |
98dbffd3 JM |
949 | }, { |
950 | .compatible = "renesas,ipmmu-r8a77965", | |
951 | .data = &ipmmu_features_rcar_gen3, | |
3701c123 SH |
952 | }, { |
953 | .compatible = "renesas,ipmmu-r8a77970", | |
954 | .data = &ipmmu_features_rcar_gen3, | |
955 | }, { | |
956 | .compatible = "renesas,ipmmu-r8a77995", | |
957 | .data = &ipmmu_features_rcar_gen3, | |
33f3ac9b MD |
958 | }, { |
959 | /* Terminator */ | |
960 | }, | |
961 | }; | |
962 | ||
963 | MODULE_DEVICE_TABLE(of, ipmmu_of_ids); | |
964 | ||
d25a2a16 LP |
965 | static int ipmmu_probe(struct platform_device *pdev) |
966 | { | |
967 | struct ipmmu_vmsa_device *mmu; | |
968 | struct resource *res; | |
969 | int irq; | |
970 | int ret; | |
971 | ||
d25a2a16 LP |
972 | mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL); |
973 | if (!mmu) { | |
974 | dev_err(&pdev->dev, "cannot allocate device data\n"); | |
975 | return -ENOMEM; | |
976 | } | |
977 | ||
978 | mmu->dev = &pdev->dev; | |
ddbbddd7 | 979 | mmu->num_utlbs = 48; |
dbb70692 MD |
980 | spin_lock_init(&mmu->lock); |
981 | bitmap_zero(mmu->ctx, IPMMU_CTX_MAX); | |
33f3ac9b | 982 | mmu->features = of_device_get_match_data(&pdev->dev); |
1c894225 | 983 | dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); |
d25a2a16 LP |
984 | |
985 | /* Map I/O memory and request IRQ. */ | |
986 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
987 | mmu->base = devm_ioremap_resource(&pdev->dev, res); | |
988 | if (IS_ERR(mmu->base)) | |
989 | return PTR_ERR(mmu->base); | |
990 | ||
275f5053 LP |
991 | /* |
992 | * The IPMMU has two register banks, for secure and non-secure modes. | |
993 | * The bank mapped at the beginning of the IPMMU address space | |
994 | * corresponds to the running mode of the CPU. When running in secure | |
995 | * mode the non-secure register bank is also available at an offset. | |
996 | * | |
997 | * Secure mode operation isn't clearly documented and is thus currently | |
998 | * not implemented in the driver. Furthermore, preliminary tests of | |
999 | * non-secure operation with the main register bank were not successful. | |
1000 | * Offset the registers base unconditionally to point to the non-secure | |
1001 | * alias space for now. | |
1002 | */ | |
33f3ac9b MD |
1003 | if (mmu->features->use_ns_alias_offset) |
1004 | mmu->base += IM_NS_ALIAS_OFFSET; | |
275f5053 | 1005 | |
5fd16341 MD |
1006 | mmu->num_ctx = min_t(unsigned int, IPMMU_CTX_MAX, |
1007 | mmu->features->number_of_contexts); | |
1008 | ||
d25a2a16 | 1009 | irq = platform_get_irq(pdev, 0); |
d25a2a16 | 1010 | |
fd5140e2 MD |
1011 | /* |
1012 | * Determine if this IPMMU instance is a root device by checking for | |
1013 | * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property. | |
1014 | */ | |
1015 | if (!mmu->features->has_cache_leaf_nodes || | |
1016 | !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL)) | |
1017 | mmu->root = mmu; | |
1018 | else | |
1019 | mmu->root = ipmmu_find_root(); | |
d25a2a16 | 1020 | |
fd5140e2 MD |
1021 | /* |
1022 | * Wait until the root device has been registered for sure. | |
1023 | */ | |
1024 | if (!mmu->root) | |
1025 | return -EPROBE_DEFER; | |
1026 | ||
1027 | /* Root devices have mandatory IRQs */ | |
1028 | if (ipmmu_is_root(mmu)) { | |
1029 | if (irq < 0) { | |
1030 | dev_err(&pdev->dev, "no IRQ found\n"); | |
1031 | return irq; | |
1032 | } | |
1033 | ||
1034 | ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0, | |
1035 | dev_name(&pdev->dev), mmu); | |
1036 | if (ret < 0) { | |
1037 | dev_err(&pdev->dev, "failed to request IRQ %d\n", irq); | |
1038 | return ret; | |
1039 | } | |
1040 | ||
1041 | ipmmu_device_reset(mmu); | |
2ae86955 YS |
1042 | |
1043 | if (mmu->features->reserved_context) { | |
1044 | dev_info(&pdev->dev, "IPMMU context 0 is reserved\n"); | |
1045 | set_bit(0, mmu->ctx); | |
1046 | } | |
fd5140e2 | 1047 | } |
d25a2a16 | 1048 | |
cda52fcd MD |
1049 | /* |
1050 | * Register the IPMMU to the IOMMU subsystem in the following cases: | |
1051 | * - R-Car Gen2 IPMMU (all devices registered) | |
1052 | * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device) | |
1053 | */ | |
1054 | if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) { | |
1055 | ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL, | |
1056 | dev_name(&pdev->dev)); | |
1057 | if (ret) | |
1058 | return ret; | |
7af9a5fd | 1059 | |
cda52fcd MD |
1060 | iommu_device_set_ops(&mmu->iommu, &ipmmu_ops); |
1061 | iommu_device_set_fwnode(&mmu->iommu, | |
1062 | &pdev->dev.of_node->fwnode); | |
01da21e5 | 1063 | |
cda52fcd MD |
1064 | ret = iommu_device_register(&mmu->iommu); |
1065 | if (ret) | |
1066 | return ret; | |
1067 | ||
1068 | #if defined(CONFIG_IOMMU_DMA) | |
1069 | if (!iommu_present(&platform_bus_type)) | |
1070 | bus_set_iommu(&platform_bus_type, &ipmmu_ops); | |
1071 | #endif | |
1072 | } | |
01da21e5 | 1073 | |
d25a2a16 LP |
1074 | /* |
1075 | * We can't create the ARM mapping here as it requires the bus to have | |
1076 | * an IOMMU, which only happens when bus_set_iommu() is called in | |
1077 | * ipmmu_init() after the probe function returns. | |
1078 | */ | |
1079 | ||
d25a2a16 LP |
1080 | platform_set_drvdata(pdev, mmu); |
1081 | ||
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | static int ipmmu_remove(struct platform_device *pdev) | |
1086 | { | |
1087 | struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev); | |
1088 | ||
7af9a5fd | 1089 | iommu_device_sysfs_remove(&mmu->iommu); |
01da21e5 MD |
1090 | iommu_device_unregister(&mmu->iommu); |
1091 | ||
d25a2a16 LP |
1092 | arm_iommu_release_mapping(mmu->mapping); |
1093 | ||
1094 | ipmmu_device_reset(mmu); | |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | static struct platform_driver ipmmu_driver = { | |
1100 | .driver = { | |
d25a2a16 | 1101 | .name = "ipmmu-vmsa", |
275f5053 | 1102 | .of_match_table = of_match_ptr(ipmmu_of_ids), |
d25a2a16 LP |
1103 | }, |
1104 | .probe = ipmmu_probe, | |
1105 | .remove = ipmmu_remove, | |
1106 | }; | |
1107 | ||
1108 | static int __init ipmmu_init(void) | |
1109 | { | |
5c5c8741 | 1110 | struct device_node *np; |
cda52fcd | 1111 | static bool setup_done; |
d25a2a16 LP |
1112 | int ret; |
1113 | ||
cda52fcd MD |
1114 | if (setup_done) |
1115 | return 0; | |
1116 | ||
5c5c8741 DO |
1117 | np = of_find_matching_node(NULL, ipmmu_of_ids); |
1118 | if (!np) | |
1119 | return 0; | |
1120 | ||
1121 | of_node_put(np); | |
1122 | ||
d25a2a16 LP |
1123 | ret = platform_driver_register(&ipmmu_driver); |
1124 | if (ret < 0) | |
1125 | return ret; | |
1126 | ||
cda52fcd | 1127 | #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) |
d25a2a16 LP |
1128 | if (!iommu_present(&platform_bus_type)) |
1129 | bus_set_iommu(&platform_bus_type, &ipmmu_ops); | |
cda52fcd | 1130 | #endif |
d25a2a16 | 1131 | |
cda52fcd | 1132 | setup_done = true; |
d25a2a16 LP |
1133 | return 0; |
1134 | } | |
1135 | ||
1136 | static void __exit ipmmu_exit(void) | |
1137 | { | |
1138 | return platform_driver_unregister(&ipmmu_driver); | |
1139 | } | |
1140 | ||
1141 | subsys_initcall(ipmmu_init); | |
1142 | module_exit(ipmmu_exit); | |
1143 | ||
1144 | MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU"); | |
1145 | MODULE_AUTHOR("Laurent Pinchart <[email protected]>"); | |
1146 | MODULE_LICENSE("GPL v2"); |