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