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