blob: 1949f3c55679c80586e6fafd44324d72b5b710df [file] [log] [blame]
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001/*
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
11#include <linux/delay.h>
12#include <linux/dma-mapping.h>
13#include <linux/err.h>
14#include <linux/export.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/iommu.h>
18#include <linux/module.h>
19#include <linux/platform_data/ipmmu-vmsa.h>
20#include <linux/platform_device.h>
21#include <linux/sizes.h>
22#include <linux/slab.h>
23
24#include <asm/dma-iommu.h>
25#include <asm/pgalloc.h>
26
27struct ipmmu_vmsa_device {
28 struct device *dev;
29 void __iomem *base;
30 struct list_head list;
31
32 const struct ipmmu_vmsa_platform_data *pdata;
33 unsigned int num_utlbs;
34
35 struct dma_iommu_mapping *mapping;
36};
37
38struct ipmmu_vmsa_domain {
39 struct ipmmu_vmsa_device *mmu;
40 struct iommu_domain *io_domain;
41
42 unsigned int context_id;
43 spinlock_t lock; /* Protects mappings */
44 pgd_t *pgd;
45};
46
Laurent Pinchart192d2042014-05-15 12:40:42 +020047struct ipmmu_vmsa_archdata {
48 struct ipmmu_vmsa_device *mmu;
49 unsigned int utlb;
50};
51
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020052static DEFINE_SPINLOCK(ipmmu_devices_lock);
53static LIST_HEAD(ipmmu_devices);
54
55#define TLB_LOOP_TIMEOUT 100 /* 100us */
56
57/* -----------------------------------------------------------------------------
58 * Registers Definition
59 */
60
61#define IM_CTX_SIZE 0x40
62
63#define IMCTR 0x0000
64#define IMCTR_TRE (1 << 17)
65#define IMCTR_AFE (1 << 16)
66#define IMCTR_RTSEL_MASK (3 << 4)
67#define IMCTR_RTSEL_SHIFT 4
68#define IMCTR_TREN (1 << 3)
69#define IMCTR_INTEN (1 << 2)
70#define IMCTR_FLUSH (1 << 1)
71#define IMCTR_MMUEN (1 << 0)
72
73#define IMCAAR 0x0004
74
75#define IMTTBCR 0x0008
76#define IMTTBCR_EAE (1 << 31)
77#define IMTTBCR_PMB (1 << 30)
78#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
79#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
80#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
81#define IMTTBCR_SH1_MASK (3 << 28)
82#define IMTTBCR_ORGN1_NC (0 << 26)
83#define IMTTBCR_ORGN1_WB_WA (1 << 26)
84#define IMTTBCR_ORGN1_WT (2 << 26)
85#define IMTTBCR_ORGN1_WB (3 << 26)
86#define IMTTBCR_ORGN1_MASK (3 << 26)
87#define IMTTBCR_IRGN1_NC (0 << 24)
88#define IMTTBCR_IRGN1_WB_WA (1 << 24)
89#define IMTTBCR_IRGN1_WT (2 << 24)
90#define IMTTBCR_IRGN1_WB (3 << 24)
91#define IMTTBCR_IRGN1_MASK (3 << 24)
92#define IMTTBCR_TSZ1_MASK (7 << 16)
93#define IMTTBCR_TSZ1_SHIFT 16
94#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
95#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
96#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
97#define IMTTBCR_SH0_MASK (3 << 12)
98#define IMTTBCR_ORGN0_NC (0 << 10)
99#define IMTTBCR_ORGN0_WB_WA (1 << 10)
100#define IMTTBCR_ORGN0_WT (2 << 10)
101#define IMTTBCR_ORGN0_WB (3 << 10)
102#define IMTTBCR_ORGN0_MASK (3 << 10)
103#define IMTTBCR_IRGN0_NC (0 << 8)
104#define IMTTBCR_IRGN0_WB_WA (1 << 8)
105#define IMTTBCR_IRGN0_WT (2 << 8)
106#define IMTTBCR_IRGN0_WB (3 << 8)
107#define IMTTBCR_IRGN0_MASK (3 << 8)
108#define IMTTBCR_SL0_LVL_2 (0 << 4)
109#define IMTTBCR_SL0_LVL_1 (1 << 4)
110#define IMTTBCR_TSZ0_MASK (7 << 0)
111#define IMTTBCR_TSZ0_SHIFT O
112
113#define IMBUSCR 0x000c
114#define IMBUSCR_DVM (1 << 2)
115#define IMBUSCR_BUSSEL_SYS (0 << 0)
116#define IMBUSCR_BUSSEL_CCI (1 << 0)
117#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
118#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
119#define IMBUSCR_BUSSEL_MASK (3 << 0)
120
121#define IMTTLBR0 0x0010
122#define IMTTUBR0 0x0014
123#define IMTTLBR1 0x0018
124#define IMTTUBR1 0x001c
125
126#define IMSTR 0x0020
127#define IMSTR_ERRLVL_MASK (3 << 12)
128#define IMSTR_ERRLVL_SHIFT 12
129#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
130#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
131#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
132#define IMSTR_ERRCODE_MASK (7 << 8)
133#define IMSTR_MHIT (1 << 4)
134#define IMSTR_ABORT (1 << 2)
135#define IMSTR_PF (1 << 1)
136#define IMSTR_TF (1 << 0)
137
138#define IMMAIR0 0x0028
139#define IMMAIR1 0x002c
140#define IMMAIR_ATTR_MASK 0xff
141#define IMMAIR_ATTR_DEVICE 0x04
142#define IMMAIR_ATTR_NC 0x44
143#define IMMAIR_ATTR_WBRWA 0xff
144#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
145#define IMMAIR_ATTR_IDX_NC 0
146#define IMMAIR_ATTR_IDX_WBRWA 1
147#define IMMAIR_ATTR_IDX_DEV 2
148
149#define IMEAR 0x0030
150
151#define IMPCTR 0x0200
152#define IMPSTR 0x0208
153#define IMPEAR 0x020c
154#define IMPMBA(n) (0x0280 + ((n) * 4))
155#define IMPMBD(n) (0x02c0 + ((n) * 4))
156
157#define IMUCTR(n) (0x0300 + ((n) * 16))
158#define IMUCTR_FIXADDEN (1 << 31)
159#define IMUCTR_FIXADD_MASK (0xff << 16)
160#define IMUCTR_FIXADD_SHIFT 16
161#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
162#define IMUCTR_TTSEL_PMB (8 << 4)
163#define IMUCTR_TTSEL_MASK (15 << 4)
164#define IMUCTR_FLUSH (1 << 1)
165#define IMUCTR_MMUEN (1 << 0)
166
167#define IMUASID(n) (0x0308 + ((n) * 16))
168#define IMUASID_ASID8_MASK (0xff << 8)
169#define IMUASID_ASID8_SHIFT 8
170#define IMUASID_ASID0_MASK (0xff << 0)
171#define IMUASID_ASID0_SHIFT 0
172
173/* -----------------------------------------------------------------------------
174 * Page Table Bits
175 */
176
177/*
178 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access,
179 * Long-descriptor format" that the NStable bit being set in a table descriptor
180 * will result in the NStable and NS bits of all child entries being ignored and
181 * considered as being set. The IPMMU seems not to comply with this, as it
182 * generates a secure access page fault if any of the NStable and NS bits isn't
183 * set when running in non-secure mode.
184 */
185#ifndef PMD_NSTABLE
186#define PMD_NSTABLE (_AT(pmdval_t, 1) << 63)
187#endif
188
189#define ARM_VMSA_PTE_XN (((pteval_t)3) << 53)
190#define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52)
191#define ARM_VMSA_PTE_AF (((pteval_t)1) << 10)
192#define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8)
193#define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8)
194#define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8)
195#define ARM_VMSA_PTE_NS (((pteval_t)1) << 5)
196#define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0)
197
198/* Stage-1 PTE */
199#define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6)
200#define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6)
201#define ARM_VMSA_PTE_ATTRINDX_SHIFT 2
202#define ARM_VMSA_PTE_nG (((pteval_t)1) << 11)
203
204/* Stage-2 PTE */
205#define ARM_VMSA_PTE_HAP_FAULT (((pteval_t)0) << 6)
206#define ARM_VMSA_PTE_HAP_READ (((pteval_t)1) << 6)
207#define ARM_VMSA_PTE_HAP_WRITE (((pteval_t)2) << 6)
208#define ARM_VMSA_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2)
209#define ARM_VMSA_PTE_MEMATTR_NC (((pteval_t)0x5) << 2)
210#define ARM_VMSA_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2)
211
212/* -----------------------------------------------------------------------------
213 * Read/Write Access
214 */
215
216static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
217{
218 return ioread32(mmu->base + offset);
219}
220
221static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
222 u32 data)
223{
224 iowrite32(data, mmu->base + offset);
225}
226
227static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
228{
229 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
230}
231
232static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
233 u32 data)
234{
235 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
236}
237
238/* -----------------------------------------------------------------------------
239 * TLB and microTLB Management
240 */
241
242/* Wait for any pending TLB invalidations to complete */
243static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
244{
245 unsigned int count = 0;
246
247 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
248 cpu_relax();
249 if (++count == TLB_LOOP_TIMEOUT) {
250 dev_err_ratelimited(domain->mmu->dev,
251 "TLB sync timed out -- MMU may be deadlocked\n");
252 return;
253 }
254 udelay(1);
255 }
256}
257
258static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
259{
260 u32 reg;
261
262 reg = ipmmu_ctx_read(domain, IMCTR);
263 reg |= IMCTR_FLUSH;
264 ipmmu_ctx_write(domain, IMCTR, reg);
265
266 ipmmu_tlb_sync(domain);
267}
268
269/*
270 * Enable MMU translation for the microTLB.
271 */
272static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200273 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200274{
275 struct ipmmu_vmsa_device *mmu = domain->mmu;
276
Laurent Pinchart192d2042014-05-15 12:40:42 +0200277 /*
278 * TODO: Reference-count the microTLB as several bus masters can be
279 * connected to the same microTLB.
280 */
281
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200282 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200283 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200284 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200285 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200286 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
287 IMUCTR_MMUEN);
288}
289
290/*
291 * Disable MMU translation for the microTLB.
292 */
293static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200294 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200295{
296 struct ipmmu_vmsa_device *mmu = domain->mmu;
297
Laurent Pinchart192d2042014-05-15 12:40:42 +0200298 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200299}
300
301static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr,
302 size_t size)
303{
304 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
305
306 /*
307 * TODO: Add support for coherent walk through CCI with DVM and remove
308 * cache handling.
309 */
310 dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE);
311}
312
313/* -----------------------------------------------------------------------------
314 * Domain/Context Management
315 */
316
317static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
318{
319 phys_addr_t ttbr;
320 u32 reg;
321
322 /*
323 * TODO: When adding support for multiple contexts, find an unused
324 * context.
325 */
326 domain->context_id = 0;
327
328 /* TTBR0 */
329 ipmmu_flush_pgtable(domain->mmu, domain->pgd,
330 PTRS_PER_PGD * sizeof(*domain->pgd));
331 ttbr = __pa(domain->pgd);
332 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
333 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
334
335 /*
336 * TTBCR
337 * We use long descriptors with inner-shareable WBWA tables and allocate
338 * the whole 32-bit VA space to TTBR0.
339 */
340 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
341 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
342 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
343
344 /*
345 * MAIR0
346 * We need three attributes only, non-cacheable, write-back read/write
347 * allocate and device memory.
348 */
349 reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC))
350 | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA))
351 | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV));
352 ipmmu_ctx_write(domain, IMMAIR0, reg);
353
354 /* IMBUSCR */
355 ipmmu_ctx_write(domain, IMBUSCR,
356 ipmmu_ctx_read(domain, IMBUSCR) &
357 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
358
359 /*
360 * IMSTR
361 * Clear all interrupt flags.
362 */
363 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
364
365 /*
366 * IMCTR
367 * Enable the MMU and interrupt generation. The long-descriptor
368 * translation table format doesn't use TEX remapping. Don't enable AF
369 * software management as we have no use for it. Flush the TLB as
370 * required when modifying the context registers.
371 */
372 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
373
374 return 0;
375}
376
377static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
378{
379 /*
380 * Disable the context. Flush the TLB as required when modifying the
381 * context registers.
382 *
383 * TODO: Is TLB flush really needed ?
384 */
385 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
386 ipmmu_tlb_sync(domain);
387}
388
389/* -----------------------------------------------------------------------------
390 * Fault Handling
391 */
392
393static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
394{
395 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
396 struct ipmmu_vmsa_device *mmu = domain->mmu;
397 u32 status;
398 u32 iova;
399
400 status = ipmmu_ctx_read(domain, IMSTR);
401 if (!(status & err_mask))
402 return IRQ_NONE;
403
404 iova = ipmmu_ctx_read(domain, IMEAR);
405
406 /*
407 * Clear the error status flags. Unlike traditional interrupt flag
408 * registers that must be cleared by writing 1, this status register
409 * seems to require 0. The error address register must be read before,
410 * otherwise its value will be 0.
411 */
412 ipmmu_ctx_write(domain, IMSTR, 0);
413
414 /* Log fatal errors. */
415 if (status & IMSTR_MHIT)
416 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
417 iova);
418 if (status & IMSTR_ABORT)
419 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
420 iova);
421
422 if (!(status & (IMSTR_PF | IMSTR_TF)))
423 return IRQ_NONE;
424
425 /*
426 * Try to handle page faults and translation faults.
427 *
428 * TODO: We need to look up the faulty device based on the I/O VA. Use
429 * the IOMMU device for now.
430 */
431 if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
432 return IRQ_HANDLED;
433
434 dev_err_ratelimited(mmu->dev,
435 "Unhandled fault: status 0x%08x iova 0x%08x\n",
436 status, iova);
437
438 return IRQ_HANDLED;
439}
440
441static irqreturn_t ipmmu_irq(int irq, void *dev)
442{
443 struct ipmmu_vmsa_device *mmu = dev;
444 struct iommu_domain *io_domain;
445 struct ipmmu_vmsa_domain *domain;
446
447 if (!mmu->mapping)
448 return IRQ_NONE;
449
450 io_domain = mmu->mapping->domain;
451 domain = io_domain->priv;
452
453 return ipmmu_domain_irq(domain);
454}
455
456/* -----------------------------------------------------------------------------
457 * Page Table Management
458 */
459
460static void ipmmu_free_ptes(pmd_t *pmd)
461{
462 pgtable_t table = pmd_pgtable(*pmd);
463 __free_page(table);
464}
465
466static void ipmmu_free_pmds(pud_t *pud)
467{
468 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
469 unsigned int i;
470
471 pmd = pmd_base;
472 for (i = 0; i < PTRS_PER_PMD; ++i) {
473 if (pmd_none(*pmd))
474 continue;
475
476 ipmmu_free_ptes(pmd);
477 pmd++;
478 }
479
480 pmd_free(NULL, pmd_base);
481}
482
483static void ipmmu_free_puds(pgd_t *pgd)
484{
485 pud_t *pud, *pud_base = pud_offset(pgd, 0);
486 unsigned int i;
487
488 pud = pud_base;
489 for (i = 0; i < PTRS_PER_PUD; ++i) {
490 if (pud_none(*pud))
491 continue;
492
493 ipmmu_free_pmds(pud);
494 pud++;
495 }
496
497 pud_free(NULL, pud_base);
498}
499
500static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain)
501{
502 pgd_t *pgd, *pgd_base = domain->pgd;
503 unsigned int i;
504
505 /*
506 * Recursively free the page tables for this domain. We don't care about
507 * speculative TLB filling, because the TLB will be nuked next time this
508 * context bank is re-allocated and no devices currently map to these
509 * tables.
510 */
511 pgd = pgd_base;
512 for (i = 0; i < PTRS_PER_PGD; ++i) {
513 if (pgd_none(*pgd))
514 continue;
515 ipmmu_free_puds(pgd);
516 pgd++;
517 }
518
519 kfree(pgd_base);
520}
521
522/*
523 * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
524 * functions as they would flush the CPU TLB.
525 */
526
527static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
528 unsigned long addr, unsigned long end,
529 phys_addr_t phys, int prot)
530{
531 unsigned long pfn = __phys_to_pfn(phys);
532 pteval_t pteval = ARM_VMSA_PTE_PAGE | ARM_VMSA_PTE_NS | ARM_VMSA_PTE_AF
533 | ARM_VMSA_PTE_XN;
534 pte_t *pte, *start;
535
536 if (pmd_none(*pmd)) {
537 /* Allocate a new set of tables */
538 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
539 if (!pte)
540 return -ENOMEM;
541
542 ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE);
543 *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE);
544 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
545
546 pte += pte_index(addr);
547 } else
548 pte = pte_offset_kernel(pmd, addr);
549
550 pteval |= ARM_VMSA_PTE_AP_UNPRIV | ARM_VMSA_PTE_nG;
551 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
552 pteval |= ARM_VMSA_PTE_AP_RDONLY;
553
554 if (prot & IOMMU_CACHE)
555 pteval |= (IMMAIR_ATTR_IDX_WBRWA <<
556 ARM_VMSA_PTE_ATTRINDX_SHIFT);
557
558 /* If no access, create a faulting entry to avoid TLB fills */
559 if (prot & IOMMU_EXEC)
560 pteval &= ~ARM_VMSA_PTE_XN;
561 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
562 pteval &= ~ARM_VMSA_PTE_PAGE;
563
564 pteval |= ARM_VMSA_PTE_SH_IS;
565 start = pte;
566
567 /* Install the page table entries. */
568 do {
569 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
570 addr += PAGE_SIZE;
571 } while (addr != end);
572
573 ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * (pte - start));
574 return 0;
575}
576
577static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud,
578 unsigned long addr, unsigned long end,
579 phys_addr_t phys, int prot)
580{
581 unsigned long next;
582 pmd_t *pmd;
583 int ret;
584
585#ifndef __PAGETABLE_PMD_FOLDED
586 if (pud_none(*pud)) {
587 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
588 if (!pmd)
589 return -ENOMEM;
590
591 ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
592 *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
593 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
594
595 pmd += pmd_index(addr);
596 } else
597#endif
598 pmd = pmd_offset(pud, addr);
599
600 do {
601 next = pmd_addr_end(addr, end);
602 ret = ipmmu_alloc_init_pte(mmu, pmd, addr, end, phys, prot);
603 phys += next - addr;
604 } while (pmd++, addr = next, addr < end);
605
606 return ret;
607}
608
609static int ipmmu_alloc_init_pud(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
610 unsigned long addr, unsigned long end,
611 phys_addr_t phys, int prot)
612{
613 unsigned long next;
614 pud_t *pud;
615 int ret;
616
617#ifndef __PAGETABLE_PUD_FOLDED
618 if (pgd_none(*pgd)) {
619 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
620 if (!pud)
621 return -ENOMEM;
622
623 ipmmu_flush_pgtable(mmu, pud, PAGE_SIZE);
624 *pgd = __pgd(__pa(pud) | PMD_NSTABLE | PMD_TYPE_TABLE);
625 ipmmu_flush_pgtable(mmu, pgd, sizeof(*pgd));
626
627 pud += pud_index(addr);
628 } else
629#endif
630 pud = pud_offset(pgd, addr);
631
632 do {
633 next = pud_addr_end(addr, end);
634 ret = ipmmu_alloc_init_pmd(mmu, pud, addr, next, phys, prot);
635 phys += next - addr;
636 } while (pud++, addr = next, addr < end);
637
638 return ret;
639}
640
641static int ipmmu_handle_mapping(struct ipmmu_vmsa_domain *domain,
642 unsigned long iova, phys_addr_t paddr,
643 size_t size, int prot)
644{
645 struct ipmmu_vmsa_device *mmu = domain->mmu;
646 pgd_t *pgd = domain->pgd;
647 unsigned long flags;
648 unsigned long end;
649 int ret;
650
651 if (!pgd)
652 return -EINVAL;
653
654 if (size & ~PAGE_MASK)
655 return -EINVAL;
656
657 if (paddr & ~((1ULL << 40) - 1))
658 return -ERANGE;
659
660 spin_lock_irqsave(&domain->lock, flags);
661
662 pgd += pgd_index(iova);
663 end = iova + size;
664
665 do {
666 unsigned long next = pgd_addr_end(iova, end);
667
668 ret = ipmmu_alloc_init_pud(mmu, pgd, iova, next, paddr, prot);
669 if (ret)
670 break;
671
672 paddr += next - iova;
673 iova = next;
674 } while (pgd++, iova != end);
675
676 spin_unlock_irqrestore(&domain->lock, flags);
677
678 ipmmu_tlb_invalidate(domain);
679
680 return ret;
681}
682
683/* -----------------------------------------------------------------------------
684 * IOMMU Operations
685 */
686
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200687static int ipmmu_domain_init(struct iommu_domain *io_domain)
688{
689 struct ipmmu_vmsa_domain *domain;
690
691 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
692 if (!domain)
693 return -ENOMEM;
694
695 spin_lock_init(&domain->lock);
696
697 domain->pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
698 if (!domain->pgd) {
699 kfree(domain);
700 return -ENOMEM;
701 }
702
703 io_domain->priv = domain;
704 domain->io_domain = io_domain;
705
706 return 0;
707}
708
709static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
710{
711 struct ipmmu_vmsa_domain *domain = io_domain->priv;
712
713 /*
714 * Free the domain resources. We assume that all devices have already
715 * been detached.
716 */
717 ipmmu_domain_destroy_context(domain);
718 ipmmu_free_pgtables(domain);
719 kfree(domain);
720}
721
722static int ipmmu_attach_device(struct iommu_domain *io_domain,
723 struct device *dev)
724{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200725 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
726 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200727 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200728 unsigned long flags;
729 int ret = 0;
730
731 if (!mmu) {
732 dev_err(dev, "Cannot attach to IPMMU\n");
733 return -ENXIO;
734 }
735
736 spin_lock_irqsave(&domain->lock, flags);
737
738 if (!domain->mmu) {
739 /* The domain hasn't been used yet, initialize it. */
740 domain->mmu = mmu;
741 ret = ipmmu_domain_init_context(domain);
742 } else if (domain->mmu != mmu) {
743 /*
744 * Something is wrong, we can't attach two devices using
745 * different IOMMUs to the same domain.
746 */
747 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
748 dev_name(mmu->dev), dev_name(domain->mmu->dev));
749 ret = -EINVAL;
750 }
751
752 spin_unlock_irqrestore(&domain->lock, flags);
753
754 if (ret < 0)
755 return ret;
756
Laurent Pinchart192d2042014-05-15 12:40:42 +0200757 ipmmu_utlb_enable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200758
759 return 0;
760}
761
762static void ipmmu_detach_device(struct iommu_domain *io_domain,
763 struct device *dev)
764{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200765 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200766 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200767
Laurent Pinchart192d2042014-05-15 12:40:42 +0200768 ipmmu_utlb_disable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200769
770 /*
771 * TODO: Optimize by disabling the context when no device is attached.
772 */
773}
774
775static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
776 phys_addr_t paddr, size_t size, int prot)
777{
778 struct ipmmu_vmsa_domain *domain = io_domain->priv;
779
780 if (!domain)
781 return -ENODEV;
782
783 return ipmmu_handle_mapping(domain, iova, paddr, size, prot);
784}
785
786static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
787 size_t size)
788{
789 struct ipmmu_vmsa_domain *domain = io_domain->priv;
790 int ret;
791
792 ret = ipmmu_handle_mapping(domain, iova, 0, size, 0);
793 return ret ? 0 : size;
794}
795
796static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
797 dma_addr_t iova)
798{
799 struct ipmmu_vmsa_domain *domain = io_domain->priv;
800 pgd_t pgd;
801 pud_t pud;
802 pmd_t pmd;
803 pte_t pte;
804
805 /* TODO: Is locking needed ? */
806
807 if (!domain->pgd)
808 return 0;
809
810 pgd = *(domain->pgd + pgd_index(iova));
811 if (pgd_none(pgd))
812 return 0;
813
814 pud = *pud_offset(&pgd, iova);
815 if (pud_none(pud))
816 return 0;
817
818 pmd = *pmd_offset(&pud, iova);
819 if (pmd_none(pmd))
820 return 0;
821
822 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
823 if (pte_none(pte))
824 return 0;
825
826 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
827}
828
Laurent Pinchart192d2042014-05-15 12:40:42 +0200829static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev)
830{
831 const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
832 const char *devname = dev_name(dev);
833 unsigned int i;
834
835 for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
836 if (strcmp(master->name, devname) == 0)
837 return master->utlb;
838 }
839
840 return -1;
841}
842
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200843static int ipmmu_add_device(struct device *dev)
844{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200845 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200846 struct ipmmu_vmsa_device *mmu;
847 struct iommu_group *group;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200848 int utlb = -1;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200849 int ret;
850
851 if (dev->archdata.iommu) {
852 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
853 dev_name(dev));
854 return -EINVAL;
855 }
856
857 /* Find the master corresponding to the device. */
858 spin_lock(&ipmmu_devices_lock);
859
860 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pinchart192d2042014-05-15 12:40:42 +0200861 utlb = ipmmu_find_utlb(mmu, dev);
862 if (utlb >= 0) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200863 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +0200864 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200865 * against device removal.
866 */
867 break;
868 }
869 }
870
871 spin_unlock(&ipmmu_devices_lock);
872
Laurent Pinchart192d2042014-05-15 12:40:42 +0200873 if (utlb < 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200874 return -ENODEV;
875
Laurent Pinchart192d2042014-05-15 12:40:42 +0200876 if (utlb >= mmu->num_utlbs)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200877 return -EINVAL;
878
879 /* Create a device group and add the device to it. */
880 group = iommu_group_alloc();
881 if (IS_ERR(group)) {
882 dev_err(dev, "Failed to allocate IOMMU group\n");
883 return PTR_ERR(group);
884 }
885
886 ret = iommu_group_add_device(group, dev);
887 iommu_group_put(group);
888
889 if (ret < 0) {
890 dev_err(dev, "Failed to add device to IPMMU group\n");
891 return ret;
892 }
893
Laurent Pinchart192d2042014-05-15 12:40:42 +0200894 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
895 if (!archdata) {
896 ret = -ENOMEM;
897 goto error;
898 }
899
900 archdata->mmu = mmu;
901 archdata->utlb = utlb;
902 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200903
904 /*
905 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
906 * VAs. This will allocate a corresponding IOMMU domain.
907 *
908 * TODO:
909 * - Create one mapping per context (TLB).
910 * - Make the mapping size configurable ? We currently use a 2GB mapping
911 * at a 1GB offset to ensure that NULL VAs will fault.
912 */
913 if (!mmu->mapping) {
914 struct dma_iommu_mapping *mapping;
915
916 mapping = arm_iommu_create_mapping(&platform_bus_type,
917 SZ_1G, SZ_2G, 0);
918 if (IS_ERR(mapping)) {
919 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
920 return PTR_ERR(mapping);
921 }
922
923 mmu->mapping = mapping;
924 }
925
926 /* Attach the ARM VA mapping to the device. */
927 ret = arm_iommu_attach_device(dev, mmu->mapping);
928 if (ret < 0) {
929 dev_err(dev, "Failed to attach device to VA mapping\n");
930 goto error;
931 }
932
933 return 0;
934
935error:
Laurent Pinchart192d2042014-05-15 12:40:42 +0200936 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200937 dev->archdata.iommu = NULL;
938 iommu_group_remove_device(dev);
939 return ret;
940}
941
942static void ipmmu_remove_device(struct device *dev)
943{
944 arm_iommu_detach_device(dev);
945 iommu_group_remove_device(dev);
Laurent Pinchart192d2042014-05-15 12:40:42 +0200946 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200947 dev->archdata.iommu = NULL;
948}
949
950static struct iommu_ops ipmmu_ops = {
951 .domain_init = ipmmu_domain_init,
952 .domain_destroy = ipmmu_domain_destroy,
953 .attach_dev = ipmmu_attach_device,
954 .detach_dev = ipmmu_detach_device,
955 .map = ipmmu_map,
956 .unmap = ipmmu_unmap,
957 .iova_to_phys = ipmmu_iova_to_phys,
958 .add_device = ipmmu_add_device,
959 .remove_device = ipmmu_remove_device,
960 .pgsize_bitmap = SZ_1M | SZ_64K | SZ_4K,
961};
962
963/* -----------------------------------------------------------------------------
964 * Probe/remove and init
965 */
966
967static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
968{
969 unsigned int i;
970
971 /* Disable all contexts. */
972 for (i = 0; i < 4; ++i)
973 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
974}
975
976static int ipmmu_probe(struct platform_device *pdev)
977{
978 struct ipmmu_vmsa_device *mmu;
979 struct resource *res;
980 int irq;
981 int ret;
982
983 if (!pdev->dev.platform_data) {
984 dev_err(&pdev->dev, "missing platform data\n");
985 return -EINVAL;
986 }
987
988 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
989 if (!mmu) {
990 dev_err(&pdev->dev, "cannot allocate device data\n");
991 return -ENOMEM;
992 }
993
994 mmu->dev = &pdev->dev;
995 mmu->pdata = pdev->dev.platform_data;
996 mmu->num_utlbs = 32;
997
998 /* Map I/O memory and request IRQ. */
999 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1000 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1001 if (IS_ERR(mmu->base))
1002 return PTR_ERR(mmu->base);
1003
1004 irq = platform_get_irq(pdev, 0);
1005 if (irq < 0) {
1006 dev_err(&pdev->dev, "no IRQ found\n");
1007 return irq;
1008 }
1009
1010 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1011 dev_name(&pdev->dev), mmu);
1012 if (ret < 0) {
1013 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1014 return irq;
1015 }
1016
1017 ipmmu_device_reset(mmu);
1018
1019 /*
1020 * We can't create the ARM mapping here as it requires the bus to have
1021 * an IOMMU, which only happens when bus_set_iommu() is called in
1022 * ipmmu_init() after the probe function returns.
1023 */
1024
1025 spin_lock(&ipmmu_devices_lock);
1026 list_add(&mmu->list, &ipmmu_devices);
1027 spin_unlock(&ipmmu_devices_lock);
1028
1029 platform_set_drvdata(pdev, mmu);
1030
1031 return 0;
1032}
1033
1034static int ipmmu_remove(struct platform_device *pdev)
1035{
1036 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1037
1038 spin_lock(&ipmmu_devices_lock);
1039 list_del(&mmu->list);
1040 spin_unlock(&ipmmu_devices_lock);
1041
1042 arm_iommu_release_mapping(mmu->mapping);
1043
1044 ipmmu_device_reset(mmu);
1045
1046 return 0;
1047}
1048
1049static struct platform_driver ipmmu_driver = {
1050 .driver = {
1051 .owner = THIS_MODULE,
1052 .name = "ipmmu-vmsa",
1053 },
1054 .probe = ipmmu_probe,
1055 .remove = ipmmu_remove,
1056};
1057
1058static int __init ipmmu_init(void)
1059{
1060 int ret;
1061
1062 ret = platform_driver_register(&ipmmu_driver);
1063 if (ret < 0)
1064 return ret;
1065
1066 if (!iommu_present(&platform_bus_type))
1067 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1068
1069 return 0;
1070}
1071
1072static void __exit ipmmu_exit(void)
1073{
1074 return platform_driver_unregister(&ipmmu_driver);
1075}
1076
1077subsys_initcall(ipmmu_init);
1078module_exit(ipmmu_exit);
1079
1080MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1081MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1082MODULE_LICENSE("GPL v2");