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