blob: f8f5b194a3600df916d653a450d5ff71deeacdc3 [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
Laurent Pinchartbc281912014-05-15 12:40:45 +0200212#define IPMMU_PTRS_PER_PTE 512
213#define IPMMU_PTRS_PER_PMD 512
214#define IPMMU_PTRS_PER_PGD 4
215#define IPMMU_PTRS_PER_PUD 1
216
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200217/* -----------------------------------------------------------------------------
218 * Read/Write Access
219 */
220
221static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
222{
223 return ioread32(mmu->base + offset);
224}
225
226static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
227 u32 data)
228{
229 iowrite32(data, mmu->base + offset);
230}
231
232static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
233{
234 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
235}
236
237static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
238 u32 data)
239{
240 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
241}
242
243/* -----------------------------------------------------------------------------
244 * TLB and microTLB Management
245 */
246
247/* Wait for any pending TLB invalidations to complete */
248static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
249{
250 unsigned int count = 0;
251
252 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
253 cpu_relax();
254 if (++count == TLB_LOOP_TIMEOUT) {
255 dev_err_ratelimited(domain->mmu->dev,
256 "TLB sync timed out -- MMU may be deadlocked\n");
257 return;
258 }
259 udelay(1);
260 }
261}
262
263static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
264{
265 u32 reg;
266
267 reg = ipmmu_ctx_read(domain, IMCTR);
268 reg |= IMCTR_FLUSH;
269 ipmmu_ctx_write(domain, IMCTR, reg);
270
271 ipmmu_tlb_sync(domain);
272}
273
274/*
275 * Enable MMU translation for the microTLB.
276 */
277static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200278 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200279{
280 struct ipmmu_vmsa_device *mmu = domain->mmu;
281
Laurent Pinchart192d2042014-05-15 12:40:42 +0200282 /*
283 * TODO: Reference-count the microTLB as several bus masters can be
284 * connected to the same microTLB.
285 */
286
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200287 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200288 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200289 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200290 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200291 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
292 IMUCTR_MMUEN);
293}
294
295/*
296 * Disable MMU translation for the microTLB.
297 */
298static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200299 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200300{
301 struct ipmmu_vmsa_device *mmu = domain->mmu;
302
Laurent Pinchart192d2042014-05-15 12:40:42 +0200303 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200304}
305
306static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr,
307 size_t size)
308{
309 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
310
311 /*
312 * TODO: Add support for coherent walk through CCI with DVM and remove
313 * cache handling.
314 */
315 dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE);
316}
317
318/* -----------------------------------------------------------------------------
319 * Domain/Context Management
320 */
321
322static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
323{
324 phys_addr_t ttbr;
325 u32 reg;
326
327 /*
328 * TODO: When adding support for multiple contexts, find an unused
329 * context.
330 */
331 domain->context_id = 0;
332
333 /* TTBR0 */
334 ipmmu_flush_pgtable(domain->mmu, domain->pgd,
Laurent Pinchartbc281912014-05-15 12:40:45 +0200335 IPMMU_PTRS_PER_PGD * sizeof(*domain->pgd));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200336 ttbr = __pa(domain->pgd);
337 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
338 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
339
340 /*
341 * TTBCR
342 * We use long descriptors with inner-shareable WBWA tables and allocate
343 * the whole 32-bit VA space to TTBR0.
344 */
345 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
346 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
347 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
348
349 /*
350 * MAIR0
351 * We need three attributes only, non-cacheable, write-back read/write
352 * allocate and device memory.
353 */
354 reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC))
355 | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA))
356 | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV));
357 ipmmu_ctx_write(domain, IMMAIR0, reg);
358
359 /* IMBUSCR */
360 ipmmu_ctx_write(domain, IMBUSCR,
361 ipmmu_ctx_read(domain, IMBUSCR) &
362 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
363
364 /*
365 * IMSTR
366 * Clear all interrupt flags.
367 */
368 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
369
370 /*
371 * IMCTR
372 * Enable the MMU and interrupt generation. The long-descriptor
373 * translation table format doesn't use TEX remapping. Don't enable AF
374 * software management as we have no use for it. Flush the TLB as
375 * required when modifying the context registers.
376 */
377 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
378
379 return 0;
380}
381
382static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
383{
384 /*
385 * Disable the context. Flush the TLB as required when modifying the
386 * context registers.
387 *
388 * TODO: Is TLB flush really needed ?
389 */
390 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
391 ipmmu_tlb_sync(domain);
392}
393
394/* -----------------------------------------------------------------------------
395 * Fault Handling
396 */
397
398static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
399{
400 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
401 struct ipmmu_vmsa_device *mmu = domain->mmu;
402 u32 status;
403 u32 iova;
404
405 status = ipmmu_ctx_read(domain, IMSTR);
406 if (!(status & err_mask))
407 return IRQ_NONE;
408
409 iova = ipmmu_ctx_read(domain, IMEAR);
410
411 /*
412 * Clear the error status flags. Unlike traditional interrupt flag
413 * registers that must be cleared by writing 1, this status register
414 * seems to require 0. The error address register must be read before,
415 * otherwise its value will be 0.
416 */
417 ipmmu_ctx_write(domain, IMSTR, 0);
418
419 /* Log fatal errors. */
420 if (status & IMSTR_MHIT)
421 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
422 iova);
423 if (status & IMSTR_ABORT)
424 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
425 iova);
426
427 if (!(status & (IMSTR_PF | IMSTR_TF)))
428 return IRQ_NONE;
429
430 /*
431 * Try to handle page faults and translation faults.
432 *
433 * TODO: We need to look up the faulty device based on the I/O VA. Use
434 * the IOMMU device for now.
435 */
436 if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
437 return IRQ_HANDLED;
438
439 dev_err_ratelimited(mmu->dev,
440 "Unhandled fault: status 0x%08x iova 0x%08x\n",
441 status, iova);
442
443 return IRQ_HANDLED;
444}
445
446static irqreturn_t ipmmu_irq(int irq, void *dev)
447{
448 struct ipmmu_vmsa_device *mmu = dev;
449 struct iommu_domain *io_domain;
450 struct ipmmu_vmsa_domain *domain;
451
452 if (!mmu->mapping)
453 return IRQ_NONE;
454
455 io_domain = mmu->mapping->domain;
456 domain = io_domain->priv;
457
458 return ipmmu_domain_irq(domain);
459}
460
461/* -----------------------------------------------------------------------------
462 * Page Table Management
463 */
464
465static void ipmmu_free_ptes(pmd_t *pmd)
466{
467 pgtable_t table = pmd_pgtable(*pmd);
468 __free_page(table);
469}
470
471static void ipmmu_free_pmds(pud_t *pud)
472{
473 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
474 unsigned int i;
475
476 pmd = pmd_base;
Laurent Pinchartbc281912014-05-15 12:40:45 +0200477 for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200478 if (pmd_none(*pmd))
479 continue;
480
481 ipmmu_free_ptes(pmd);
482 pmd++;
483 }
484
485 pmd_free(NULL, pmd_base);
486}
487
488static void ipmmu_free_puds(pgd_t *pgd)
489{
490 pud_t *pud, *pud_base = pud_offset(pgd, 0);
491 unsigned int i;
492
493 pud = pud_base;
Laurent Pinchartbc281912014-05-15 12:40:45 +0200494 for (i = 0; i < IPMMU_PTRS_PER_PUD; ++i) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200495 if (pud_none(*pud))
496 continue;
497
498 ipmmu_free_pmds(pud);
499 pud++;
500 }
501
502 pud_free(NULL, pud_base);
503}
504
505static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain)
506{
507 pgd_t *pgd, *pgd_base = domain->pgd;
508 unsigned int i;
509
510 /*
511 * Recursively free the page tables for this domain. We don't care about
512 * speculative TLB filling, because the TLB will be nuked next time this
513 * context bank is re-allocated and no devices currently map to these
514 * tables.
515 */
516 pgd = pgd_base;
Laurent Pinchartbc281912014-05-15 12:40:45 +0200517 for (i = 0; i < IPMMU_PTRS_PER_PGD; ++i) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200518 if (pgd_none(*pgd))
519 continue;
520 ipmmu_free_puds(pgd);
521 pgd++;
522 }
523
524 kfree(pgd_base);
525}
526
527/*
528 * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
529 * functions as they would flush the CPU TLB.
530 */
531
532static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
533 unsigned long addr, unsigned long end,
534 phys_addr_t phys, int prot)
535{
536 unsigned long pfn = __phys_to_pfn(phys);
537 pteval_t pteval = ARM_VMSA_PTE_PAGE | ARM_VMSA_PTE_NS | ARM_VMSA_PTE_AF
538 | ARM_VMSA_PTE_XN;
539 pte_t *pte, *start;
540
541 if (pmd_none(*pmd)) {
542 /* Allocate a new set of tables */
543 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
544 if (!pte)
545 return -ENOMEM;
546
547 ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE);
548 *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE);
549 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
550
551 pte += pte_index(addr);
552 } else
553 pte = pte_offset_kernel(pmd, addr);
554
555 pteval |= ARM_VMSA_PTE_AP_UNPRIV | ARM_VMSA_PTE_nG;
556 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
557 pteval |= ARM_VMSA_PTE_AP_RDONLY;
558
559 if (prot & IOMMU_CACHE)
560 pteval |= (IMMAIR_ATTR_IDX_WBRWA <<
561 ARM_VMSA_PTE_ATTRINDX_SHIFT);
562
563 /* If no access, create a faulting entry to avoid TLB fills */
564 if (prot & IOMMU_EXEC)
565 pteval &= ~ARM_VMSA_PTE_XN;
566 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
567 pteval &= ~ARM_VMSA_PTE_PAGE;
568
569 pteval |= ARM_VMSA_PTE_SH_IS;
570 start = pte;
571
572 /* Install the page table entries. */
573 do {
574 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
575 addr += PAGE_SIZE;
576 } while (addr != end);
577
578 ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * (pte - start));
579 return 0;
580}
581
582static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud,
583 unsigned long addr, unsigned long end,
584 phys_addr_t phys, int prot)
585{
586 unsigned long next;
587 pmd_t *pmd;
588 int ret;
589
590#ifndef __PAGETABLE_PMD_FOLDED
591 if (pud_none(*pud)) {
592 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
593 if (!pmd)
594 return -ENOMEM;
595
596 ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
597 *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
598 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
599
600 pmd += pmd_index(addr);
601 } else
602#endif
603 pmd = pmd_offset(pud, addr);
604
605 do {
606 next = pmd_addr_end(addr, end);
607 ret = ipmmu_alloc_init_pte(mmu, pmd, addr, end, phys, prot);
608 phys += next - addr;
609 } while (pmd++, addr = next, addr < end);
610
611 return ret;
612}
613
614static int ipmmu_alloc_init_pud(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
615 unsigned long addr, unsigned long end,
616 phys_addr_t phys, int prot)
617{
618 unsigned long next;
619 pud_t *pud;
620 int ret;
621
622#ifndef __PAGETABLE_PUD_FOLDED
623 if (pgd_none(*pgd)) {
624 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
625 if (!pud)
626 return -ENOMEM;
627
628 ipmmu_flush_pgtable(mmu, pud, PAGE_SIZE);
629 *pgd = __pgd(__pa(pud) | PMD_NSTABLE | PMD_TYPE_TABLE);
630 ipmmu_flush_pgtable(mmu, pgd, sizeof(*pgd));
631
632 pud += pud_index(addr);
633 } else
634#endif
635 pud = pud_offset(pgd, addr);
636
637 do {
638 next = pud_addr_end(addr, end);
639 ret = ipmmu_alloc_init_pmd(mmu, pud, addr, next, phys, prot);
640 phys += next - addr;
641 } while (pud++, addr = next, addr < end);
642
643 return ret;
644}
645
646static int ipmmu_handle_mapping(struct ipmmu_vmsa_domain *domain,
647 unsigned long iova, phys_addr_t paddr,
648 size_t size, int prot)
649{
650 struct ipmmu_vmsa_device *mmu = domain->mmu;
651 pgd_t *pgd = domain->pgd;
652 unsigned long flags;
653 unsigned long end;
654 int ret;
655
656 if (!pgd)
657 return -EINVAL;
658
659 if (size & ~PAGE_MASK)
660 return -EINVAL;
661
662 if (paddr & ~((1ULL << 40) - 1))
663 return -ERANGE;
664
665 spin_lock_irqsave(&domain->lock, flags);
666
667 pgd += pgd_index(iova);
668 end = iova + size;
669
670 do {
671 unsigned long next = pgd_addr_end(iova, end);
672
673 ret = ipmmu_alloc_init_pud(mmu, pgd, iova, next, paddr, prot);
674 if (ret)
675 break;
676
677 paddr += next - iova;
678 iova = next;
679 } while (pgd++, iova != end);
680
681 spin_unlock_irqrestore(&domain->lock, flags);
682
683 ipmmu_tlb_invalidate(domain);
684
685 return ret;
686}
687
688/* -----------------------------------------------------------------------------
689 * IOMMU Operations
690 */
691
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200692static int ipmmu_domain_init(struct iommu_domain *io_domain)
693{
694 struct ipmmu_vmsa_domain *domain;
695
696 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
697 if (!domain)
698 return -ENOMEM;
699
700 spin_lock_init(&domain->lock);
701
Laurent Pinchartbc281912014-05-15 12:40:45 +0200702 domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200703 if (!domain->pgd) {
704 kfree(domain);
705 return -ENOMEM;
706 }
707
708 io_domain->priv = domain;
709 domain->io_domain = io_domain;
710
711 return 0;
712}
713
714static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
715{
716 struct ipmmu_vmsa_domain *domain = io_domain->priv;
717
718 /*
719 * Free the domain resources. We assume that all devices have already
720 * been detached.
721 */
722 ipmmu_domain_destroy_context(domain);
723 ipmmu_free_pgtables(domain);
724 kfree(domain);
725}
726
727static int ipmmu_attach_device(struct iommu_domain *io_domain,
728 struct device *dev)
729{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200730 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
731 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200732 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200733 unsigned long flags;
734 int ret = 0;
735
736 if (!mmu) {
737 dev_err(dev, "Cannot attach to IPMMU\n");
738 return -ENXIO;
739 }
740
741 spin_lock_irqsave(&domain->lock, flags);
742
743 if (!domain->mmu) {
744 /* The domain hasn't been used yet, initialize it. */
745 domain->mmu = mmu;
746 ret = ipmmu_domain_init_context(domain);
747 } else if (domain->mmu != mmu) {
748 /*
749 * Something is wrong, we can't attach two devices using
750 * different IOMMUs to the same domain.
751 */
752 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
753 dev_name(mmu->dev), dev_name(domain->mmu->dev));
754 ret = -EINVAL;
755 }
756
757 spin_unlock_irqrestore(&domain->lock, flags);
758
759 if (ret < 0)
760 return ret;
761
Laurent Pinchart192d2042014-05-15 12:40:42 +0200762 ipmmu_utlb_enable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200763
764 return 0;
765}
766
767static void ipmmu_detach_device(struct iommu_domain *io_domain,
768 struct device *dev)
769{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200770 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200771 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200772
Laurent Pinchart192d2042014-05-15 12:40:42 +0200773 ipmmu_utlb_disable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200774
775 /*
776 * TODO: Optimize by disabling the context when no device is attached.
777 */
778}
779
780static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
781 phys_addr_t paddr, size_t size, int prot)
782{
783 struct ipmmu_vmsa_domain *domain = io_domain->priv;
784
785 if (!domain)
786 return -ENODEV;
787
788 return ipmmu_handle_mapping(domain, iova, paddr, size, prot);
789}
790
791static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
792 size_t size)
793{
794 struct ipmmu_vmsa_domain *domain = io_domain->priv;
795 int ret;
796
797 ret = ipmmu_handle_mapping(domain, iova, 0, size, 0);
798 return ret ? 0 : size;
799}
800
801static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
802 dma_addr_t iova)
803{
804 struct ipmmu_vmsa_domain *domain = io_domain->priv;
805 pgd_t pgd;
806 pud_t pud;
807 pmd_t pmd;
808 pte_t pte;
809
810 /* TODO: Is locking needed ? */
811
812 if (!domain->pgd)
813 return 0;
814
815 pgd = *(domain->pgd + pgd_index(iova));
816 if (pgd_none(pgd))
817 return 0;
818
819 pud = *pud_offset(&pgd, iova);
820 if (pud_none(pud))
821 return 0;
822
823 pmd = *pmd_offset(&pud, iova);
824 if (pmd_none(pmd))
825 return 0;
826
827 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
828 if (pte_none(pte))
829 return 0;
830
831 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
832}
833
Laurent Pinchart192d2042014-05-15 12:40:42 +0200834static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev)
835{
836 const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
837 const char *devname = dev_name(dev);
838 unsigned int i;
839
840 for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
841 if (strcmp(master->name, devname) == 0)
842 return master->utlb;
843 }
844
845 return -1;
846}
847
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200848static int ipmmu_add_device(struct device *dev)
849{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200850 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200851 struct ipmmu_vmsa_device *mmu;
852 struct iommu_group *group;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200853 int utlb = -1;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200854 int ret;
855
856 if (dev->archdata.iommu) {
857 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
858 dev_name(dev));
859 return -EINVAL;
860 }
861
862 /* Find the master corresponding to the device. */
863 spin_lock(&ipmmu_devices_lock);
864
865 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pinchart192d2042014-05-15 12:40:42 +0200866 utlb = ipmmu_find_utlb(mmu, dev);
867 if (utlb >= 0) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200868 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +0200869 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200870 * against device removal.
871 */
872 break;
873 }
874 }
875
876 spin_unlock(&ipmmu_devices_lock);
877
Laurent Pinchart192d2042014-05-15 12:40:42 +0200878 if (utlb < 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200879 return -ENODEV;
880
Laurent Pinchart192d2042014-05-15 12:40:42 +0200881 if (utlb >= mmu->num_utlbs)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200882 return -EINVAL;
883
884 /* Create a device group and add the device to it. */
885 group = iommu_group_alloc();
886 if (IS_ERR(group)) {
887 dev_err(dev, "Failed to allocate IOMMU group\n");
888 return PTR_ERR(group);
889 }
890
891 ret = iommu_group_add_device(group, dev);
892 iommu_group_put(group);
893
894 if (ret < 0) {
895 dev_err(dev, "Failed to add device to IPMMU group\n");
896 return ret;
897 }
898
Laurent Pinchart192d2042014-05-15 12:40:42 +0200899 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
900 if (!archdata) {
901 ret = -ENOMEM;
902 goto error;
903 }
904
905 archdata->mmu = mmu;
906 archdata->utlb = utlb;
907 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200908
909 /*
910 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
911 * VAs. This will allocate a corresponding IOMMU domain.
912 *
913 * TODO:
914 * - Create one mapping per context (TLB).
915 * - Make the mapping size configurable ? We currently use a 2GB mapping
916 * at a 1GB offset to ensure that NULL VAs will fault.
917 */
918 if (!mmu->mapping) {
919 struct dma_iommu_mapping *mapping;
920
921 mapping = arm_iommu_create_mapping(&platform_bus_type,
922 SZ_1G, SZ_2G, 0);
923 if (IS_ERR(mapping)) {
924 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
925 return PTR_ERR(mapping);
926 }
927
928 mmu->mapping = mapping;
929 }
930
931 /* Attach the ARM VA mapping to the device. */
932 ret = arm_iommu_attach_device(dev, mmu->mapping);
933 if (ret < 0) {
934 dev_err(dev, "Failed to attach device to VA mapping\n");
935 goto error;
936 }
937
938 return 0;
939
940error:
Laurent Pinchart192d2042014-05-15 12:40:42 +0200941 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200942 dev->archdata.iommu = NULL;
943 iommu_group_remove_device(dev);
944 return ret;
945}
946
947static void ipmmu_remove_device(struct device *dev)
948{
949 arm_iommu_detach_device(dev);
950 iommu_group_remove_device(dev);
Laurent Pinchart192d2042014-05-15 12:40:42 +0200951 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200952 dev->archdata.iommu = NULL;
953}
954
955static struct iommu_ops ipmmu_ops = {
956 .domain_init = ipmmu_domain_init,
957 .domain_destroy = ipmmu_domain_destroy,
958 .attach_dev = ipmmu_attach_device,
959 .detach_dev = ipmmu_detach_device,
960 .map = ipmmu_map,
961 .unmap = ipmmu_unmap,
962 .iova_to_phys = ipmmu_iova_to_phys,
963 .add_device = ipmmu_add_device,
964 .remove_device = ipmmu_remove_device,
Laurent Pinchart251dac42014-05-15 12:40:44 +0200965 .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200966};
967
968/* -----------------------------------------------------------------------------
969 * Probe/remove and init
970 */
971
972static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
973{
974 unsigned int i;
975
976 /* Disable all contexts. */
977 for (i = 0; i < 4; ++i)
978 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
979}
980
981static int ipmmu_probe(struct platform_device *pdev)
982{
983 struct ipmmu_vmsa_device *mmu;
984 struct resource *res;
985 int irq;
986 int ret;
987
988 if (!pdev->dev.platform_data) {
989 dev_err(&pdev->dev, "missing platform data\n");
990 return -EINVAL;
991 }
992
993 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
994 if (!mmu) {
995 dev_err(&pdev->dev, "cannot allocate device data\n");
996 return -ENOMEM;
997 }
998
999 mmu->dev = &pdev->dev;
1000 mmu->pdata = pdev->dev.platform_data;
1001 mmu->num_utlbs = 32;
1002
1003 /* Map I/O memory and request IRQ. */
1004 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1005 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1006 if (IS_ERR(mmu->base))
1007 return PTR_ERR(mmu->base);
1008
1009 irq = platform_get_irq(pdev, 0);
1010 if (irq < 0) {
1011 dev_err(&pdev->dev, "no IRQ found\n");
1012 return irq;
1013 }
1014
1015 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1016 dev_name(&pdev->dev), mmu);
1017 if (ret < 0) {
1018 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1019 return irq;
1020 }
1021
1022 ipmmu_device_reset(mmu);
1023
1024 /*
1025 * We can't create the ARM mapping here as it requires the bus to have
1026 * an IOMMU, which only happens when bus_set_iommu() is called in
1027 * ipmmu_init() after the probe function returns.
1028 */
1029
1030 spin_lock(&ipmmu_devices_lock);
1031 list_add(&mmu->list, &ipmmu_devices);
1032 spin_unlock(&ipmmu_devices_lock);
1033
1034 platform_set_drvdata(pdev, mmu);
1035
1036 return 0;
1037}
1038
1039static int ipmmu_remove(struct platform_device *pdev)
1040{
1041 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1042
1043 spin_lock(&ipmmu_devices_lock);
1044 list_del(&mmu->list);
1045 spin_unlock(&ipmmu_devices_lock);
1046
1047 arm_iommu_release_mapping(mmu->mapping);
1048
1049 ipmmu_device_reset(mmu);
1050
1051 return 0;
1052}
1053
1054static struct platform_driver ipmmu_driver = {
1055 .driver = {
1056 .owner = THIS_MODULE,
1057 .name = "ipmmu-vmsa",
1058 },
1059 .probe = ipmmu_probe,
1060 .remove = ipmmu_remove,
1061};
1062
1063static int __init ipmmu_init(void)
1064{
1065 int ret;
1066
1067 ret = platform_driver_register(&ipmmu_driver);
1068 if (ret < 0)
1069 return ret;
1070
1071 if (!iommu_present(&platform_bus_type))
1072 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1073
1074 return 0;
1075}
1076
1077static void __exit ipmmu_exit(void)
1078{
1079 return platform_driver_unregister(&ipmmu_driver);
1080}
1081
1082subsys_initcall(ipmmu_init);
1083module_exit(ipmmu_exit);
1084
1085MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1086MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1087MODULE_LICENSE("GPL v2");