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