blob: 7f0522048c6b714185d61f5568636fb29c258443 [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 Pinchartdda7c2e2014-05-15 12:40:49 +0200482 if (!pmd_table(*pmd))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200483 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
Laurent Pinchart9009f252014-05-15 12:40:48 +0200520static pte_t *ipmmu_alloc_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
521 unsigned long iova)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200522{
Laurent Pinchart9009f252014-05-15 12:40:48 +0200523 pte_t *pte;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200524
Laurent Pinchart9009f252014-05-15 12:40:48 +0200525 if (!pmd_none(*pmd))
526 return pte_offset_kernel(pmd, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200527
Laurent Pinchart9009f252014-05-15 12:40:48 +0200528 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
529 if (!pte)
530 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200531
Laurent Pinchart9009f252014-05-15 12:40:48 +0200532 ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE);
533 *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE);
534 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200535
Laurent Pinchart9009f252014-05-15 12:40:48 +0200536 return pte + pte_index(iova);
537}
538
539static pmd_t *ipmmu_alloc_pmd(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
540 unsigned long iova)
541{
542 pud_t *pud = (pud_t *)pgd;
543 pmd_t *pmd;
544
545 if (!pud_none(*pud))
546 return pmd_offset(pud, iova);
547
548 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
549 if (!pmd)
550 return NULL;
551
552 ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
553 *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
554 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
555
556 return pmd + pmd_index(iova);
557}
558
559static u64 ipmmu_page_prot(unsigned int prot, u64 type)
560{
561 u64 pgprot = ARM_VMSA_PTE_XN | ARM_VMSA_PTE_nG | ARM_VMSA_PTE_AF
562 | ARM_VMSA_PTE_SH_IS | ARM_VMSA_PTE_AP_UNPRIV
563 | ARM_VMSA_PTE_NS | type;
564
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200565 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200566 pgprot |= ARM_VMSA_PTE_AP_RDONLY;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200567
568 if (prot & IOMMU_CACHE)
Laurent Pinchart9009f252014-05-15 12:40:48 +0200569 pgprot |= IMMAIR_ATTR_IDX_WBRWA << ARM_VMSA_PTE_ATTRINDX_SHIFT;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200570
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200571 if (prot & IOMMU_EXEC)
Laurent Pinchart9009f252014-05-15 12:40:48 +0200572 pgprot &= ~ARM_VMSA_PTE_XN;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200573 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200574 /* If no access create a faulting entry to avoid TLB fills. */
575 pgprot &= ~ARM_VMSA_PTE_PAGE;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200576
Laurent Pinchart9009f252014-05-15 12:40:48 +0200577 return pgprot;
578}
579
580static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
581 unsigned long iova, unsigned long pfn,
582 size_t size, int prot)
583{
584 pteval_t pteval = ipmmu_page_prot(prot, ARM_VMSA_PTE_PAGE);
585 unsigned int num_ptes = 1;
586 pte_t *pte, *start;
587 unsigned int i;
588
589 pte = ipmmu_alloc_pte(mmu, pmd, iova);
590 if (!pte)
591 return -ENOMEM;
592
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200593 start = pte;
594
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200595 /*
Laurent Pinchart9009f252014-05-15 12:40:48 +0200596 * Install the page table entries. We can be called both for a single
597 * page or for a block of 16 physically contiguous pages. In the latter
598 * case set the PTE contiguous hint.
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200599 */
Laurent Pinchart9009f252014-05-15 12:40:48 +0200600 if (size == SZ_64K) {
601 pteval |= ARM_VMSA_PTE_CONT;
602 num_ptes = ARM_VMSA_PTE_CONT_ENTRIES;
603 }
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200604
Laurent Pinchart9009f252014-05-15 12:40:48 +0200605 for (i = num_ptes; i; --i)
606 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200607
Laurent Pinchart9009f252014-05-15 12:40:48 +0200608 ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * num_ptes);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200609
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200610 return 0;
611}
612
Laurent Pinchartdda7c2e2014-05-15 12:40:49 +0200613static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
614 unsigned long iova, unsigned long pfn,
615 int prot)
616{
617 pmdval_t pmdval = ipmmu_page_prot(prot, PMD_TYPE_SECT);
618
619 *pmd = pfn_pmd(pfn, __pgprot(pmdval));
620 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
621
622 return 0;
623}
624
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200625static int ipmmu_handle_mapping(struct ipmmu_vmsa_domain *domain,
626 unsigned long iova, phys_addr_t paddr,
627 size_t size, int prot)
628{
629 struct ipmmu_vmsa_device *mmu = domain->mmu;
630 pgd_t *pgd = domain->pgd;
631 unsigned long flags;
Laurent Pinchart9009f252014-05-15 12:40:48 +0200632 unsigned long pfn;
633 pmd_t *pmd;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200634 int ret;
635
636 if (!pgd)
637 return -EINVAL;
638
639 if (size & ~PAGE_MASK)
640 return -EINVAL;
641
642 if (paddr & ~((1ULL << 40) - 1))
643 return -ERANGE;
644
Laurent Pinchart9009f252014-05-15 12:40:48 +0200645 pfn = __phys_to_pfn(paddr);
646 pgd += pgd_index(iova);
647
648 /* Update the page tables. */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200649 spin_lock_irqsave(&domain->lock, flags);
650
Laurent Pinchart9009f252014-05-15 12:40:48 +0200651 pmd = ipmmu_alloc_pmd(mmu, pgd, iova);
652 if (!pmd) {
653 ret = -ENOMEM;
654 goto done;
655 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200656
Laurent Pinchartdda7c2e2014-05-15 12:40:49 +0200657 switch (size) {
658 case SZ_2M:
659 ret = ipmmu_alloc_init_pmd(mmu, pmd, iova, pfn, prot);
660 break;
661 case SZ_64K:
662 case SZ_4K:
663 ret = ipmmu_alloc_init_pte(mmu, pmd, iova, pfn, size, prot);
664 break;
665 default:
666 ret = -EINVAL;
667 break;
668 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200669
Laurent Pinchart9009f252014-05-15 12:40:48 +0200670done:
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200671 spin_unlock_irqrestore(&domain->lock, flags);
672
Laurent Pinchart9009f252014-05-15 12:40:48 +0200673 if (!ret)
674 ipmmu_tlb_invalidate(domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200675
676 return ret;
677}
678
679/* -----------------------------------------------------------------------------
680 * IOMMU Operations
681 */
682
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200683static int ipmmu_domain_init(struct iommu_domain *io_domain)
684{
685 struct ipmmu_vmsa_domain *domain;
686
687 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
688 if (!domain)
689 return -ENOMEM;
690
691 spin_lock_init(&domain->lock);
692
Laurent Pinchartbc281912014-05-15 12:40:45 +0200693 domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200694 if (!domain->pgd) {
695 kfree(domain);
696 return -ENOMEM;
697 }
698
699 io_domain->priv = domain;
700 domain->io_domain = io_domain;
701
702 return 0;
703}
704
705static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
706{
707 struct ipmmu_vmsa_domain *domain = io_domain->priv;
708
709 /*
710 * Free the domain resources. We assume that all devices have already
711 * been detached.
712 */
713 ipmmu_domain_destroy_context(domain);
714 ipmmu_free_pgtables(domain);
715 kfree(domain);
716}
717
718static int ipmmu_attach_device(struct iommu_domain *io_domain,
719 struct device *dev)
720{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200721 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
722 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200723 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200724 unsigned long flags;
725 int ret = 0;
726
727 if (!mmu) {
728 dev_err(dev, "Cannot attach to IPMMU\n");
729 return -ENXIO;
730 }
731
732 spin_lock_irqsave(&domain->lock, flags);
733
734 if (!domain->mmu) {
735 /* The domain hasn't been used yet, initialize it. */
736 domain->mmu = mmu;
737 ret = ipmmu_domain_init_context(domain);
738 } else if (domain->mmu != mmu) {
739 /*
740 * Something is wrong, we can't attach two devices using
741 * different IOMMUs to the same domain.
742 */
743 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
744 dev_name(mmu->dev), dev_name(domain->mmu->dev));
745 ret = -EINVAL;
746 }
747
748 spin_unlock_irqrestore(&domain->lock, flags);
749
750 if (ret < 0)
751 return ret;
752
Laurent Pinchart192d2042014-05-15 12:40:42 +0200753 ipmmu_utlb_enable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200754
755 return 0;
756}
757
758static void ipmmu_detach_device(struct iommu_domain *io_domain,
759 struct device *dev)
760{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200761 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200762 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200763
Laurent Pinchart192d2042014-05-15 12:40:42 +0200764 ipmmu_utlb_disable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200765
766 /*
767 * TODO: Optimize by disabling the context when no device is attached.
768 */
769}
770
771static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
772 phys_addr_t paddr, size_t size, int prot)
773{
774 struct ipmmu_vmsa_domain *domain = io_domain->priv;
775
776 if (!domain)
777 return -ENODEV;
778
779 return ipmmu_handle_mapping(domain, iova, paddr, size, prot);
780}
781
782static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
783 size_t size)
784{
785 struct ipmmu_vmsa_domain *domain = io_domain->priv;
786 int ret;
787
788 ret = ipmmu_handle_mapping(domain, iova, 0, size, 0);
789 return ret ? 0 : size;
790}
791
792static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
793 dma_addr_t iova)
794{
795 struct ipmmu_vmsa_domain *domain = io_domain->priv;
796 pgd_t pgd;
797 pud_t pud;
798 pmd_t pmd;
799 pte_t pte;
800
801 /* TODO: Is locking needed ? */
802
803 if (!domain->pgd)
804 return 0;
805
806 pgd = *(domain->pgd + pgd_index(iova));
807 if (pgd_none(pgd))
808 return 0;
809
810 pud = *pud_offset(&pgd, iova);
811 if (pud_none(pud))
812 return 0;
813
814 pmd = *pmd_offset(&pud, iova);
815 if (pmd_none(pmd))
816 return 0;
817
Laurent Pinchartdda7c2e2014-05-15 12:40:49 +0200818 if (pmd_sect(pmd))
819 return __pfn_to_phys(pmd_pfn(pmd)) | (iova & ~PMD_MASK);
820
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200821 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
822 if (pte_none(pte))
823 return 0;
824
825 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
826}
827
Laurent Pinchart192d2042014-05-15 12:40:42 +0200828static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev)
829{
830 const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
831 const char *devname = dev_name(dev);
832 unsigned int i;
833
834 for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
835 if (strcmp(master->name, devname) == 0)
836 return master->utlb;
837 }
838
839 return -1;
840}
841
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200842static int ipmmu_add_device(struct device *dev)
843{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200844 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200845 struct ipmmu_vmsa_device *mmu;
846 struct iommu_group *group;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200847 int utlb = -1;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200848 int ret;
849
850 if (dev->archdata.iommu) {
851 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
852 dev_name(dev));
853 return -EINVAL;
854 }
855
856 /* Find the master corresponding to the device. */
857 spin_lock(&ipmmu_devices_lock);
858
859 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pinchart192d2042014-05-15 12:40:42 +0200860 utlb = ipmmu_find_utlb(mmu, dev);
861 if (utlb >= 0) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200862 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +0200863 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200864 * against device removal.
865 */
866 break;
867 }
868 }
869
870 spin_unlock(&ipmmu_devices_lock);
871
Laurent Pinchart192d2042014-05-15 12:40:42 +0200872 if (utlb < 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200873 return -ENODEV;
874
Laurent Pinchart192d2042014-05-15 12:40:42 +0200875 if (utlb >= mmu->num_utlbs)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200876 return -EINVAL;
877
878 /* Create a device group and add the device to it. */
879 group = iommu_group_alloc();
880 if (IS_ERR(group)) {
881 dev_err(dev, "Failed to allocate IOMMU group\n");
882 return PTR_ERR(group);
883 }
884
885 ret = iommu_group_add_device(group, dev);
886 iommu_group_put(group);
887
888 if (ret < 0) {
889 dev_err(dev, "Failed to add device to IPMMU group\n");
890 return ret;
891 }
892
Laurent Pinchart192d2042014-05-15 12:40:42 +0200893 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
894 if (!archdata) {
895 ret = -ENOMEM;
896 goto error;
897 }
898
899 archdata->mmu = mmu;
900 archdata->utlb = utlb;
901 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200902
903 /*
904 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
905 * VAs. This will allocate a corresponding IOMMU domain.
906 *
907 * TODO:
908 * - Create one mapping per context (TLB).
909 * - Make the mapping size configurable ? We currently use a 2GB mapping
910 * at a 1GB offset to ensure that NULL VAs will fault.
911 */
912 if (!mmu->mapping) {
913 struct dma_iommu_mapping *mapping;
914
915 mapping = arm_iommu_create_mapping(&platform_bus_type,
916 SZ_1G, SZ_2G, 0);
917 if (IS_ERR(mapping)) {
918 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
919 return PTR_ERR(mapping);
920 }
921
922 mmu->mapping = mapping;
923 }
924
925 /* Attach the ARM VA mapping to the device. */
926 ret = arm_iommu_attach_device(dev, mmu->mapping);
927 if (ret < 0) {
928 dev_err(dev, "Failed to attach device to VA mapping\n");
929 goto error;
930 }
931
932 return 0;
933
934error:
Laurent Pinchart192d2042014-05-15 12:40:42 +0200935 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200936 dev->archdata.iommu = NULL;
937 iommu_group_remove_device(dev);
938 return ret;
939}
940
941static void ipmmu_remove_device(struct device *dev)
942{
943 arm_iommu_detach_device(dev);
944 iommu_group_remove_device(dev);
Laurent Pinchart192d2042014-05-15 12:40:42 +0200945 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200946 dev->archdata.iommu = NULL;
947}
948
949static struct iommu_ops ipmmu_ops = {
950 .domain_init = ipmmu_domain_init,
951 .domain_destroy = ipmmu_domain_destroy,
952 .attach_dev = ipmmu_attach_device,
953 .detach_dev = ipmmu_detach_device,
954 .map = ipmmu_map,
955 .unmap = ipmmu_unmap,
956 .iova_to_phys = ipmmu_iova_to_phys,
957 .add_device = ipmmu_add_device,
958 .remove_device = ipmmu_remove_device,
Laurent Pinchartdda7c2e2014-05-15 12:40:49 +0200959 .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200960};
961
962/* -----------------------------------------------------------------------------
963 * Probe/remove and init
964 */
965
966static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
967{
968 unsigned int i;
969
970 /* Disable all contexts. */
971 for (i = 0; i < 4; ++i)
972 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
973}
974
975static int ipmmu_probe(struct platform_device *pdev)
976{
977 struct ipmmu_vmsa_device *mmu;
978 struct resource *res;
979 int irq;
980 int ret;
981
982 if (!pdev->dev.platform_data) {
983 dev_err(&pdev->dev, "missing platform data\n");
984 return -EINVAL;
985 }
986
987 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
988 if (!mmu) {
989 dev_err(&pdev->dev, "cannot allocate device data\n");
990 return -ENOMEM;
991 }
992
993 mmu->dev = &pdev->dev;
994 mmu->pdata = pdev->dev.platform_data;
995 mmu->num_utlbs = 32;
996
997 /* Map I/O memory and request IRQ. */
998 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
999 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1000 if (IS_ERR(mmu->base))
1001 return PTR_ERR(mmu->base);
1002
1003 irq = platform_get_irq(pdev, 0);
1004 if (irq < 0) {
1005 dev_err(&pdev->dev, "no IRQ found\n");
1006 return irq;
1007 }
1008
1009 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1010 dev_name(&pdev->dev), mmu);
1011 if (ret < 0) {
1012 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1013 return irq;
1014 }
1015
1016 ipmmu_device_reset(mmu);
1017
1018 /*
1019 * We can't create the ARM mapping here as it requires the bus to have
1020 * an IOMMU, which only happens when bus_set_iommu() is called in
1021 * ipmmu_init() after the probe function returns.
1022 */
1023
1024 spin_lock(&ipmmu_devices_lock);
1025 list_add(&mmu->list, &ipmmu_devices);
1026 spin_unlock(&ipmmu_devices_lock);
1027
1028 platform_set_drvdata(pdev, mmu);
1029
1030 return 0;
1031}
1032
1033static int ipmmu_remove(struct platform_device *pdev)
1034{
1035 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1036
1037 spin_lock(&ipmmu_devices_lock);
1038 list_del(&mmu->list);
1039 spin_unlock(&ipmmu_devices_lock);
1040
1041 arm_iommu_release_mapping(mmu->mapping);
1042
1043 ipmmu_device_reset(mmu);
1044
1045 return 0;
1046}
1047
1048static struct platform_driver ipmmu_driver = {
1049 .driver = {
1050 .owner = THIS_MODULE,
1051 .name = "ipmmu-vmsa",
1052 },
1053 .probe = ipmmu_probe,
1054 .remove = ipmmu_remove,
1055};
1056
1057static int __init ipmmu_init(void)
1058{
1059 int ret;
1060
1061 ret = platform_driver_register(&ipmmu_driver);
1062 if (ret < 0)
1063 return ret;
1064
1065 if (!iommu_present(&platform_bus_type))
1066 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1067
1068 return 0;
1069}
1070
1071static void __exit ipmmu_exit(void)
1072{
1073 return platform_driver_unregister(&ipmmu_driver);
1074}
1075
1076subsys_initcall(ipmmu_init);
1077module_exit(ipmmu_exit);
1078
1079MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1080MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1081MODULE_LICENSE("GPL v2");