blob: 5d080cf11ba580b91be7b79a383637b5ac2f558d [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>
Laurent Pinchart275f5052014-03-17 01:02:46 +010019#include <linux/of.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020020#include <linux/platform_data/ipmmu-vmsa.h>
21#include <linux/platform_device.h>
22#include <linux/sizes.h>
23#include <linux/slab.h>
24
25#include <asm/dma-iommu.h>
26#include <asm/pgalloc.h>
27
28struct ipmmu_vmsa_device {
29 struct device *dev;
30 void __iomem *base;
31 struct list_head list;
32
33 const struct ipmmu_vmsa_platform_data *pdata;
34 unsigned int num_utlbs;
35
36 struct dma_iommu_mapping *mapping;
37};
38
39struct ipmmu_vmsa_domain {
40 struct ipmmu_vmsa_device *mmu;
41 struct iommu_domain *io_domain;
42
43 unsigned int context_id;
44 spinlock_t lock; /* Protects mappings */
45 pgd_t *pgd;
46};
47
Laurent Pinchart192d2042014-05-15 12:40:42 +020048struct ipmmu_vmsa_archdata {
49 struct ipmmu_vmsa_device *mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +020050 unsigned int *utlbs;
51 unsigned int num_utlbs;
Laurent Pinchart192d2042014-05-15 12:40:42 +020052};
53
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020054static DEFINE_SPINLOCK(ipmmu_devices_lock);
55static LIST_HEAD(ipmmu_devices);
56
57#define TLB_LOOP_TIMEOUT 100 /* 100us */
58
59/* -----------------------------------------------------------------------------
60 * Registers Definition
61 */
62
Laurent Pinchart275f5052014-03-17 01:02:46 +010063#define IM_NS_ALIAS_OFFSET 0x800
64
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020065#define IM_CTX_SIZE 0x40
66
67#define IMCTR 0x0000
68#define IMCTR_TRE (1 << 17)
69#define IMCTR_AFE (1 << 16)
70#define IMCTR_RTSEL_MASK (3 << 4)
71#define IMCTR_RTSEL_SHIFT 4
72#define IMCTR_TREN (1 << 3)
73#define IMCTR_INTEN (1 << 2)
74#define IMCTR_FLUSH (1 << 1)
75#define IMCTR_MMUEN (1 << 0)
76
77#define IMCAAR 0x0004
78
79#define IMTTBCR 0x0008
80#define IMTTBCR_EAE (1 << 31)
81#define IMTTBCR_PMB (1 << 30)
82#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
83#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
84#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
85#define IMTTBCR_SH1_MASK (3 << 28)
86#define IMTTBCR_ORGN1_NC (0 << 26)
87#define IMTTBCR_ORGN1_WB_WA (1 << 26)
88#define IMTTBCR_ORGN1_WT (2 << 26)
89#define IMTTBCR_ORGN1_WB (3 << 26)
90#define IMTTBCR_ORGN1_MASK (3 << 26)
91#define IMTTBCR_IRGN1_NC (0 << 24)
92#define IMTTBCR_IRGN1_WB_WA (1 << 24)
93#define IMTTBCR_IRGN1_WT (2 << 24)
94#define IMTTBCR_IRGN1_WB (3 << 24)
95#define IMTTBCR_IRGN1_MASK (3 << 24)
96#define IMTTBCR_TSZ1_MASK (7 << 16)
97#define IMTTBCR_TSZ1_SHIFT 16
98#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
99#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
100#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
101#define IMTTBCR_SH0_MASK (3 << 12)
102#define IMTTBCR_ORGN0_NC (0 << 10)
103#define IMTTBCR_ORGN0_WB_WA (1 << 10)
104#define IMTTBCR_ORGN0_WT (2 << 10)
105#define IMTTBCR_ORGN0_WB (3 << 10)
106#define IMTTBCR_ORGN0_MASK (3 << 10)
107#define IMTTBCR_IRGN0_NC (0 << 8)
108#define IMTTBCR_IRGN0_WB_WA (1 << 8)
109#define IMTTBCR_IRGN0_WT (2 << 8)
110#define IMTTBCR_IRGN0_WB (3 << 8)
111#define IMTTBCR_IRGN0_MASK (3 << 8)
112#define IMTTBCR_SL0_LVL_2 (0 << 4)
113#define IMTTBCR_SL0_LVL_1 (1 << 4)
114#define IMTTBCR_TSZ0_MASK (7 << 0)
115#define IMTTBCR_TSZ0_SHIFT O
116
117#define IMBUSCR 0x000c
118#define IMBUSCR_DVM (1 << 2)
119#define IMBUSCR_BUSSEL_SYS (0 << 0)
120#define IMBUSCR_BUSSEL_CCI (1 << 0)
121#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
122#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
123#define IMBUSCR_BUSSEL_MASK (3 << 0)
124
125#define IMTTLBR0 0x0010
126#define IMTTUBR0 0x0014
127#define IMTTLBR1 0x0018
128#define IMTTUBR1 0x001c
129
130#define IMSTR 0x0020
131#define IMSTR_ERRLVL_MASK (3 << 12)
132#define IMSTR_ERRLVL_SHIFT 12
133#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
134#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
135#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
136#define IMSTR_ERRCODE_MASK (7 << 8)
137#define IMSTR_MHIT (1 << 4)
138#define IMSTR_ABORT (1 << 2)
139#define IMSTR_PF (1 << 1)
140#define IMSTR_TF (1 << 0)
141
142#define IMMAIR0 0x0028
143#define IMMAIR1 0x002c
144#define IMMAIR_ATTR_MASK 0xff
145#define IMMAIR_ATTR_DEVICE 0x04
146#define IMMAIR_ATTR_NC 0x44
147#define IMMAIR_ATTR_WBRWA 0xff
148#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
149#define IMMAIR_ATTR_IDX_NC 0
150#define IMMAIR_ATTR_IDX_WBRWA 1
151#define IMMAIR_ATTR_IDX_DEV 2
152
153#define IMEAR 0x0030
154
155#define IMPCTR 0x0200
156#define IMPSTR 0x0208
157#define IMPEAR 0x020c
158#define IMPMBA(n) (0x0280 + ((n) * 4))
159#define IMPMBD(n) (0x02c0 + ((n) * 4))
160
161#define IMUCTR(n) (0x0300 + ((n) * 16))
162#define IMUCTR_FIXADDEN (1 << 31)
163#define IMUCTR_FIXADD_MASK (0xff << 16)
164#define IMUCTR_FIXADD_SHIFT 16
165#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
166#define IMUCTR_TTSEL_PMB (8 << 4)
167#define IMUCTR_TTSEL_MASK (15 << 4)
168#define IMUCTR_FLUSH (1 << 1)
169#define IMUCTR_MMUEN (1 << 0)
170
171#define IMUASID(n) (0x0308 + ((n) * 16))
172#define IMUASID_ASID8_MASK (0xff << 8)
173#define IMUASID_ASID8_SHIFT 8
174#define IMUASID_ASID0_MASK (0xff << 0)
175#define IMUASID_ASID0_SHIFT 0
176
177/* -----------------------------------------------------------------------------
178 * Page Table Bits
179 */
180
181/*
182 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access,
183 * Long-descriptor format" that the NStable bit being set in a table descriptor
184 * will result in the NStable and NS bits of all child entries being ignored and
185 * considered as being set. The IPMMU seems not to comply with this, as it
186 * generates a secure access page fault if any of the NStable and NS bits isn't
187 * set when running in non-secure mode.
188 */
189#ifndef PMD_NSTABLE
190#define PMD_NSTABLE (_AT(pmdval_t, 1) << 63)
191#endif
192
193#define ARM_VMSA_PTE_XN (((pteval_t)3) << 53)
194#define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52)
195#define ARM_VMSA_PTE_AF (((pteval_t)1) << 10)
196#define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8)
197#define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8)
198#define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8)
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200199#define ARM_VMSA_PTE_SH_MASK (((pteval_t)3) << 8)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200200#define ARM_VMSA_PTE_NS (((pteval_t)1) << 5)
201#define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0)
202
203/* Stage-1 PTE */
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200204#define ARM_VMSA_PTE_nG (((pteval_t)1) << 11)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200205#define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6)
206#define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6)
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200207#define ARM_VMSA_PTE_AP_MASK (((pteval_t)3) << 6)
208#define ARM_VMSA_PTE_ATTRINDX_MASK (((pteval_t)3) << 2)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200209#define ARM_VMSA_PTE_ATTRINDX_SHIFT 2
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200210
211#define ARM_VMSA_PTE_ATTRS_MASK \
212 (ARM_VMSA_PTE_XN | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_nG | \
213 ARM_VMSA_PTE_AF | ARM_VMSA_PTE_SH_MASK | ARM_VMSA_PTE_AP_MASK | \
214 ARM_VMSA_PTE_NS | ARM_VMSA_PTE_ATTRINDX_MASK)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200215
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200216#define ARM_VMSA_PTE_CONT_ENTRIES 16
217#define ARM_VMSA_PTE_CONT_SIZE (PAGE_SIZE * ARM_VMSA_PTE_CONT_ENTRIES)
218
Laurent Pinchartbc281912014-05-15 12:40:45 +0200219#define IPMMU_PTRS_PER_PTE 512
220#define IPMMU_PTRS_PER_PMD 512
221#define IPMMU_PTRS_PER_PGD 4
Laurent Pinchartbc281912014-05-15 12:40:45 +0200222
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200223/* -----------------------------------------------------------------------------
224 * Read/Write Access
225 */
226
227static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
228{
229 return ioread32(mmu->base + offset);
230}
231
232static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
233 u32 data)
234{
235 iowrite32(data, mmu->base + offset);
236}
237
238static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
239{
240 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
241}
242
243static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
244 u32 data)
245{
246 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
247}
248
249/* -----------------------------------------------------------------------------
250 * TLB and microTLB Management
251 */
252
253/* Wait for any pending TLB invalidations to complete */
254static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
255{
256 unsigned int count = 0;
257
258 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
259 cpu_relax();
260 if (++count == TLB_LOOP_TIMEOUT) {
261 dev_err_ratelimited(domain->mmu->dev,
262 "TLB sync timed out -- MMU may be deadlocked\n");
263 return;
264 }
265 udelay(1);
266 }
267}
268
269static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
270{
271 u32 reg;
272
273 reg = ipmmu_ctx_read(domain, IMCTR);
274 reg |= IMCTR_FLUSH;
275 ipmmu_ctx_write(domain, IMCTR, reg);
276
277 ipmmu_tlb_sync(domain);
278}
279
280/*
281 * Enable MMU translation for the microTLB.
282 */
283static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200284 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200285{
286 struct ipmmu_vmsa_device *mmu = domain->mmu;
287
Laurent Pinchart192d2042014-05-15 12:40:42 +0200288 /*
289 * TODO: Reference-count the microTLB as several bus masters can be
290 * connected to the same microTLB.
291 */
292
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200293 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200294 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200295 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200296 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200297 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
298 IMUCTR_MMUEN);
299}
300
301/*
302 * Disable MMU translation for the microTLB.
303 */
304static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200305 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200306{
307 struct ipmmu_vmsa_device *mmu = domain->mmu;
308
Laurent Pinchart192d2042014-05-15 12:40:42 +0200309 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200310}
311
312static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr,
313 size_t size)
314{
315 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
316
317 /*
318 * TODO: Add support for coherent walk through CCI with DVM and remove
319 * cache handling.
320 */
321 dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE);
322}
323
324/* -----------------------------------------------------------------------------
325 * Domain/Context Management
326 */
327
328static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
329{
330 phys_addr_t ttbr;
331 u32 reg;
332
333 /*
334 * TODO: When adding support for multiple contexts, find an unused
335 * context.
336 */
337 domain->context_id = 0;
338
339 /* TTBR0 */
340 ipmmu_flush_pgtable(domain->mmu, domain->pgd,
Laurent Pinchartbc281912014-05-15 12:40:45 +0200341 IPMMU_PTRS_PER_PGD * sizeof(*domain->pgd));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200342 ttbr = __pa(domain->pgd);
343 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
344 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
345
346 /*
347 * TTBCR
348 * We use long descriptors with inner-shareable WBWA tables and allocate
349 * the whole 32-bit VA space to TTBR0.
350 */
351 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
352 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
353 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
354
355 /*
356 * MAIR0
357 * We need three attributes only, non-cacheable, write-back read/write
358 * allocate and device memory.
359 */
360 reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC))
361 | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA))
362 | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV));
363 ipmmu_ctx_write(domain, IMMAIR0, reg);
364
365 /* IMBUSCR */
366 ipmmu_ctx_write(domain, IMBUSCR,
367 ipmmu_ctx_read(domain, IMBUSCR) &
368 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
369
370 /*
371 * IMSTR
372 * Clear all interrupt flags.
373 */
374 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
375
376 /*
377 * IMCTR
378 * Enable the MMU and interrupt generation. The long-descriptor
379 * translation table format doesn't use TEX remapping. Don't enable AF
380 * software management as we have no use for it. Flush the TLB as
381 * required when modifying the context registers.
382 */
383 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
384
385 return 0;
386}
387
388static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
389{
390 /*
391 * Disable the context. Flush the TLB as required when modifying the
392 * context registers.
393 *
394 * TODO: Is TLB flush really needed ?
395 */
396 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
397 ipmmu_tlb_sync(domain);
398}
399
400/* -----------------------------------------------------------------------------
401 * Fault Handling
402 */
403
404static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
405{
406 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
407 struct ipmmu_vmsa_device *mmu = domain->mmu;
408 u32 status;
409 u32 iova;
410
411 status = ipmmu_ctx_read(domain, IMSTR);
412 if (!(status & err_mask))
413 return IRQ_NONE;
414
415 iova = ipmmu_ctx_read(domain, IMEAR);
416
417 /*
418 * Clear the error status flags. Unlike traditional interrupt flag
419 * registers that must be cleared by writing 1, this status register
420 * seems to require 0. The error address register must be read before,
421 * otherwise its value will be 0.
422 */
423 ipmmu_ctx_write(domain, IMSTR, 0);
424
425 /* Log fatal errors. */
426 if (status & IMSTR_MHIT)
427 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
428 iova);
429 if (status & IMSTR_ABORT)
430 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
431 iova);
432
433 if (!(status & (IMSTR_PF | IMSTR_TF)))
434 return IRQ_NONE;
435
436 /*
437 * Try to handle page faults and translation faults.
438 *
439 * TODO: We need to look up the faulty device based on the I/O VA. Use
440 * the IOMMU device for now.
441 */
442 if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
443 return IRQ_HANDLED;
444
445 dev_err_ratelimited(mmu->dev,
446 "Unhandled fault: status 0x%08x iova 0x%08x\n",
447 status, iova);
448
449 return IRQ_HANDLED;
450}
451
452static irqreturn_t ipmmu_irq(int irq, void *dev)
453{
454 struct ipmmu_vmsa_device *mmu = dev;
455 struct iommu_domain *io_domain;
456 struct ipmmu_vmsa_domain *domain;
457
458 if (!mmu->mapping)
459 return IRQ_NONE;
460
461 io_domain = mmu->mapping->domain;
462 domain = io_domain->priv;
463
464 return ipmmu_domain_irq(domain);
465}
466
467/* -----------------------------------------------------------------------------
468 * Page Table Management
469 */
470
Laurent Pinchart14e51232014-05-15 12:40:47 +0200471#define pud_pgtable(pud) pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK))
472
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200473static void ipmmu_free_ptes(pmd_t *pmd)
474{
475 pgtable_t table = pmd_pgtable(*pmd);
476 __free_page(table);
477}
478
479static void ipmmu_free_pmds(pud_t *pud)
480{
Laurent Pinchart14e51232014-05-15 12:40:47 +0200481 pmd_t *pmd = pmd_offset(pud, 0);
482 pgtable_t table;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200483 unsigned int i;
484
Laurent Pinchartbc281912014-05-15 12:40:45 +0200485 for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200486 if (!pmd_table(*pmd))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200487 continue;
488
489 ipmmu_free_ptes(pmd);
490 pmd++;
491 }
492
Laurent Pinchart14e51232014-05-15 12:40:47 +0200493 table = pud_pgtable(*pud);
494 __free_page(table);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200495}
496
497static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain)
498{
499 pgd_t *pgd, *pgd_base = domain->pgd;
500 unsigned int i;
501
502 /*
503 * Recursively free the page tables for this domain. We don't care about
504 * speculative TLB filling, because the TLB will be nuked next time this
505 * context bank is re-allocated and no devices currently map to these
506 * tables.
507 */
508 pgd = pgd_base;
Laurent Pinchartbc281912014-05-15 12:40:45 +0200509 for (i = 0; i < IPMMU_PTRS_PER_PGD; ++i) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200510 if (pgd_none(*pgd))
511 continue;
Laurent Pinchart14e51232014-05-15 12:40:47 +0200512 ipmmu_free_pmds((pud_t *)pgd);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200513 pgd++;
514 }
515
516 kfree(pgd_base);
517}
518
519/*
520 * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
521 * functions as they would flush the CPU TLB.
522 */
523
Laurent Pinchart9009f252014-05-15 12:40:48 +0200524static pte_t *ipmmu_alloc_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
525 unsigned long iova)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200526{
Laurent Pinchart9009f252014-05-15 12:40:48 +0200527 pte_t *pte;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200528
Laurent Pinchart9009f252014-05-15 12:40:48 +0200529 if (!pmd_none(*pmd))
530 return pte_offset_kernel(pmd, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200531
Laurent Pinchart9009f252014-05-15 12:40:48 +0200532 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
533 if (!pte)
534 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200535
Laurent Pinchart9009f252014-05-15 12:40:48 +0200536 ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE);
537 *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE);
538 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200539
Laurent Pinchart9009f252014-05-15 12:40:48 +0200540 return pte + pte_index(iova);
541}
542
543static pmd_t *ipmmu_alloc_pmd(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
544 unsigned long iova)
545{
546 pud_t *pud = (pud_t *)pgd;
547 pmd_t *pmd;
548
549 if (!pud_none(*pud))
550 return pmd_offset(pud, iova);
551
552 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
553 if (!pmd)
554 return NULL;
555
556 ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
557 *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
558 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
559
560 return pmd + pmd_index(iova);
561}
562
563static u64 ipmmu_page_prot(unsigned int prot, u64 type)
564{
Laurent Pinchart04561ca2014-12-15 20:41:13 +0200565 u64 pgprot = ARM_VMSA_PTE_nG | ARM_VMSA_PTE_AF
Laurent Pinchart9009f252014-05-15 12:40:48 +0200566 | ARM_VMSA_PTE_SH_IS | ARM_VMSA_PTE_AP_UNPRIV
567 | ARM_VMSA_PTE_NS | type;
568
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200569 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200570 pgprot |= ARM_VMSA_PTE_AP_RDONLY;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200571
572 if (prot & IOMMU_CACHE)
Laurent Pinchart9009f252014-05-15 12:40:48 +0200573 pgprot |= IMMAIR_ATTR_IDX_WBRWA << ARM_VMSA_PTE_ATTRINDX_SHIFT;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200574
Laurent Pinchart04561ca2014-12-15 20:41:13 +0200575 if (prot & IOMMU_NOEXEC)
576 pgprot |= ARM_VMSA_PTE_XN;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200577 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200578 /* If no access create a faulting entry to avoid TLB fills. */
579 pgprot &= ~ARM_VMSA_PTE_PAGE;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200580
Laurent Pinchart9009f252014-05-15 12:40:48 +0200581 return pgprot;
582}
583
584static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
585 unsigned long iova, unsigned long pfn,
586 size_t size, int prot)
587{
588 pteval_t pteval = ipmmu_page_prot(prot, ARM_VMSA_PTE_PAGE);
589 unsigned int num_ptes = 1;
590 pte_t *pte, *start;
591 unsigned int i;
592
593 pte = ipmmu_alloc_pte(mmu, pmd, iova);
594 if (!pte)
595 return -ENOMEM;
596
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200597 start = pte;
598
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200599 /*
Laurent Pinchart9009f252014-05-15 12:40:48 +0200600 * Install the page table entries. We can be called both for a single
601 * page or for a block of 16 physically contiguous pages. In the latter
602 * case set the PTE contiguous hint.
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200603 */
Laurent Pinchart9009f252014-05-15 12:40:48 +0200604 if (size == SZ_64K) {
605 pteval |= ARM_VMSA_PTE_CONT;
606 num_ptes = ARM_VMSA_PTE_CONT_ENTRIES;
607 }
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200608
Laurent Pinchart9009f252014-05-15 12:40:48 +0200609 for (i = num_ptes; i; --i)
610 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200611
Laurent Pinchart9009f252014-05-15 12:40:48 +0200612 ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * num_ptes);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200613
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200614 return 0;
615}
616
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200617static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
618 unsigned long iova, unsigned long pfn,
619 int prot)
620{
621 pmdval_t pmdval = ipmmu_page_prot(prot, PMD_TYPE_SECT);
622
623 *pmd = pfn_pmd(pfn, __pgprot(pmdval));
624 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
625
626 return 0;
627}
628
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200629static int ipmmu_create_mapping(struct ipmmu_vmsa_domain *domain,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200630 unsigned long iova, phys_addr_t paddr,
631 size_t size, int prot)
632{
633 struct ipmmu_vmsa_device *mmu = domain->mmu;
634 pgd_t *pgd = domain->pgd;
635 unsigned long flags;
Laurent Pinchart9009f252014-05-15 12:40:48 +0200636 unsigned long pfn;
637 pmd_t *pmd;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200638 int ret;
639
640 if (!pgd)
641 return -EINVAL;
642
643 if (size & ~PAGE_MASK)
644 return -EINVAL;
645
646 if (paddr & ~((1ULL << 40) - 1))
647 return -ERANGE;
648
Laurent Pinchart9009f252014-05-15 12:40:48 +0200649 pfn = __phys_to_pfn(paddr);
650 pgd += pgd_index(iova);
651
652 /* Update the page tables. */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200653 spin_lock_irqsave(&domain->lock, flags);
654
Laurent Pinchart9009f252014-05-15 12:40:48 +0200655 pmd = ipmmu_alloc_pmd(mmu, pgd, iova);
656 if (!pmd) {
657 ret = -ENOMEM;
658 goto done;
659 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200660
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200661 switch (size) {
662 case SZ_2M:
663 ret = ipmmu_alloc_init_pmd(mmu, pmd, iova, pfn, prot);
664 break;
665 case SZ_64K:
666 case SZ_4K:
667 ret = ipmmu_alloc_init_pte(mmu, pmd, iova, pfn, size, prot);
668 break;
669 default:
670 ret = -EINVAL;
671 break;
672 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200673
Laurent Pinchart9009f252014-05-15 12:40:48 +0200674done:
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200675 spin_unlock_irqrestore(&domain->lock, flags);
676
Laurent Pinchart9009f252014-05-15 12:40:48 +0200677 if (!ret)
678 ipmmu_tlb_invalidate(domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200679
680 return ret;
681}
682
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200683static void ipmmu_clear_pud(struct ipmmu_vmsa_device *mmu, pud_t *pud)
684{
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200685 pgtable_t table = pud_pgtable(*pud);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200686
687 /* Clear the PUD. */
688 *pud = __pud(0);
689 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200690
691 /* Free the page table. */
692 __free_page(table);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200693}
694
695static void ipmmu_clear_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud,
696 pmd_t *pmd)
697{
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200698 pmd_t pmdval = *pmd;
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200699 unsigned int i;
700
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200701 /* Clear the PMD. */
702 *pmd = __pmd(0);
703 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
704
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200705 /* Free the page table. */
706 if (pmd_table(pmdval)) {
707 pgtable_t table = pmd_pgtable(pmdval);
708
709 __free_page(table);
710 }
711
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200712 /* Check whether the PUD is still needed. */
713 pmd = pmd_offset(pud, 0);
714 for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
715 if (!pmd_none(pmd[i]))
716 return;
717 }
718
719 /* Clear the parent PUD. */
720 ipmmu_clear_pud(mmu, pud);
721}
722
723static void ipmmu_clear_pte(struct ipmmu_vmsa_device *mmu, pud_t *pud,
724 pmd_t *pmd, pte_t *pte, unsigned int num_ptes)
725{
726 unsigned int i;
727
728 /* Clear the PTE. */
729 for (i = num_ptes; i; --i)
730 pte[i-1] = __pte(0);
731
732 ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * num_ptes);
733
734 /* Check whether the PMD is still needed. */
735 pte = pte_offset_kernel(pmd, 0);
736 for (i = 0; i < IPMMU_PTRS_PER_PTE; ++i) {
737 if (!pte_none(pte[i]))
738 return;
739 }
740
741 /* Clear the parent PMD. */
742 ipmmu_clear_pmd(mmu, pud, pmd);
743}
744
745static int ipmmu_split_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd)
746{
747 pte_t *pte, *start;
748 pteval_t pteval;
749 unsigned long pfn;
750 unsigned int i;
751
752 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
753 if (!pte)
754 return -ENOMEM;
755
756 /* Copy the PMD attributes. */
757 pteval = (pmd_val(*pmd) & ARM_VMSA_PTE_ATTRS_MASK)
758 | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_PAGE;
759
760 pfn = pmd_pfn(*pmd);
761 start = pte;
762
763 for (i = IPMMU_PTRS_PER_PTE; i; --i)
764 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
765
766 ipmmu_flush_pgtable(mmu, start, PAGE_SIZE);
767 *pmd = __pmd(__pa(start) | PMD_NSTABLE | PMD_TYPE_TABLE);
768 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
769
770 return 0;
771}
772
773static void ipmmu_split_pte(struct ipmmu_vmsa_device *mmu, pte_t *pte)
774{
775 unsigned int i;
776
777 for (i = ARM_VMSA_PTE_CONT_ENTRIES; i; --i)
778 pte[i-1] = __pte(pte_val(*pte) & ~ARM_VMSA_PTE_CONT);
779
780 ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * ARM_VMSA_PTE_CONT_ENTRIES);
781}
782
783static int ipmmu_clear_mapping(struct ipmmu_vmsa_domain *domain,
784 unsigned long iova, size_t size)
785{
786 struct ipmmu_vmsa_device *mmu = domain->mmu;
787 unsigned long flags;
788 pgd_t *pgd = domain->pgd;
789 pud_t *pud;
790 pmd_t *pmd;
791 pte_t *pte;
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200792
793 if (!pgd)
794 return -EINVAL;
795
796 if (size & ~PAGE_MASK)
797 return -EINVAL;
798
799 pgd += pgd_index(iova);
800 pud = (pud_t *)pgd;
801
802 spin_lock_irqsave(&domain->lock, flags);
803
804 /* If there's no PUD or PMD we're done. */
805 if (pud_none(*pud))
806 goto done;
807
808 pmd = pmd_offset(pud, iova);
809 if (pmd_none(*pmd))
810 goto done;
811
812 /*
813 * When freeing a 2MB block just clear the PMD. In the unlikely case the
814 * block is mapped as individual pages this will free the corresponding
815 * PTE page table.
816 */
817 if (size == SZ_2M) {
818 ipmmu_clear_pmd(mmu, pud, pmd);
819 goto done;
820 }
821
822 /*
823 * If the PMD has been mapped as a section remap it as pages to allow
824 * freeing individual pages.
825 */
826 if (pmd_sect(*pmd))
827 ipmmu_split_pmd(mmu, pmd);
828
829 pte = pte_offset_kernel(pmd, iova);
830
831 /*
832 * When freeing a 64kB block just clear the PTE entries. We don't have
833 * to care about the contiguous hint of the surrounding entries.
834 */
835 if (size == SZ_64K) {
836 ipmmu_clear_pte(mmu, pud, pmd, pte, ARM_VMSA_PTE_CONT_ENTRIES);
837 goto done;
838 }
839
840 /*
841 * If the PTE has been mapped with the contiguous hint set remap it and
842 * its surrounding PTEs to allow unmapping a single page.
843 */
844 if (pte_val(*pte) & ARM_VMSA_PTE_CONT)
845 ipmmu_split_pte(mmu, pte);
846
847 /* Clear the PTE. */
848 ipmmu_clear_pte(mmu, pud, pmd, pte, 1);
849
850done:
851 spin_unlock_irqrestore(&domain->lock, flags);
852
Laurent Pinchart9eca0a52014-07-24 15:34:54 +0200853 ipmmu_tlb_invalidate(domain);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200854
855 return 0;
856}
857
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200858/* -----------------------------------------------------------------------------
859 * IOMMU Operations
860 */
861
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200862static int ipmmu_domain_init(struct iommu_domain *io_domain)
863{
864 struct ipmmu_vmsa_domain *domain;
865
866 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
867 if (!domain)
868 return -ENOMEM;
869
870 spin_lock_init(&domain->lock);
871
Laurent Pinchartbc281912014-05-15 12:40:45 +0200872 domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200873 if (!domain->pgd) {
874 kfree(domain);
875 return -ENOMEM;
876 }
877
878 io_domain->priv = domain;
879 domain->io_domain = io_domain;
880
881 return 0;
882}
883
884static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
885{
886 struct ipmmu_vmsa_domain *domain = io_domain->priv;
887
888 /*
889 * Free the domain resources. We assume that all devices have already
890 * been detached.
891 */
892 ipmmu_domain_destroy_context(domain);
893 ipmmu_free_pgtables(domain);
894 kfree(domain);
895}
896
897static int ipmmu_attach_device(struct iommu_domain *io_domain,
898 struct device *dev)
899{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200900 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
901 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200902 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200903 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200904 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200905 int ret = 0;
906
907 if (!mmu) {
908 dev_err(dev, "Cannot attach to IPMMU\n");
909 return -ENXIO;
910 }
911
912 spin_lock_irqsave(&domain->lock, flags);
913
914 if (!domain->mmu) {
915 /* The domain hasn't been used yet, initialize it. */
916 domain->mmu = mmu;
917 ret = ipmmu_domain_init_context(domain);
918 } else if (domain->mmu != mmu) {
919 /*
920 * Something is wrong, we can't attach two devices using
921 * different IOMMUs to the same domain.
922 */
923 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
924 dev_name(mmu->dev), dev_name(domain->mmu->dev));
925 ret = -EINVAL;
926 }
927
928 spin_unlock_irqrestore(&domain->lock, flags);
929
930 if (ret < 0)
931 return ret;
932
Laurent Pincharta166d312014-07-24 01:36:43 +0200933 for (i = 0; i < archdata->num_utlbs; ++i)
934 ipmmu_utlb_enable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200935
936 return 0;
937}
938
939static void ipmmu_detach_device(struct iommu_domain *io_domain,
940 struct device *dev)
941{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200942 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200943 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pincharta166d312014-07-24 01:36:43 +0200944 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200945
Laurent Pincharta166d312014-07-24 01:36:43 +0200946 for (i = 0; i < archdata->num_utlbs; ++i)
947 ipmmu_utlb_disable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200948
949 /*
950 * TODO: Optimize by disabling the context when no device is attached.
951 */
952}
953
954static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
955 phys_addr_t paddr, size_t size, int prot)
956{
957 struct ipmmu_vmsa_domain *domain = io_domain->priv;
958
959 if (!domain)
960 return -ENODEV;
961
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200962 return ipmmu_create_mapping(domain, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200963}
964
965static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
966 size_t size)
967{
968 struct ipmmu_vmsa_domain *domain = io_domain->priv;
969 int ret;
970
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200971 ret = ipmmu_clear_mapping(domain, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200972 return ret ? 0 : size;
973}
974
975static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
976 dma_addr_t iova)
977{
978 struct ipmmu_vmsa_domain *domain = io_domain->priv;
979 pgd_t pgd;
980 pud_t pud;
981 pmd_t pmd;
982 pte_t pte;
983
984 /* TODO: Is locking needed ? */
985
986 if (!domain->pgd)
987 return 0;
988
989 pgd = *(domain->pgd + pgd_index(iova));
990 if (pgd_none(pgd))
991 return 0;
992
993 pud = *pud_offset(&pgd, iova);
994 if (pud_none(pud))
995 return 0;
996
997 pmd = *pmd_offset(&pud, iova);
998 if (pmd_none(pmd))
999 return 0;
1000
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +02001001 if (pmd_sect(pmd))
1002 return __pfn_to_phys(pmd_pfn(pmd)) | (iova & ~PMD_MASK);
1003
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001004 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
1005 if (pte_none(pte))
1006 return 0;
1007
1008 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
1009}
1010
Laurent Pincharta166d312014-07-24 01:36:43 +02001011static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev,
1012 unsigned int **_utlbs)
Laurent Pinchart192d2042014-05-15 12:40:42 +02001013{
Laurent Pincharta166d312014-07-24 01:36:43 +02001014 unsigned int *utlbs;
1015 unsigned int i;
1016 int count;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001017
Laurent Pinchart275f5052014-03-17 01:02:46 +01001018 if (mmu->pdata) {
1019 const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
1020 const char *devname = dev_name(dev);
1021 unsigned int i;
1022
1023 for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
Laurent Pincharta166d312014-07-24 01:36:43 +02001024 if (strcmp(master->name, devname) == 0) {
1025 utlbs = kmalloc(sizeof(*utlbs), GFP_KERNEL);
1026 if (!utlbs)
1027 return -ENOMEM;
1028
1029 utlbs[0] = master->utlb;
1030
1031 *_utlbs = utlbs;
1032 return 1;
1033 }
Laurent Pinchart275f5052014-03-17 01:02:46 +01001034 }
1035
Laurent Pincharta166d312014-07-24 01:36:43 +02001036 return -EINVAL;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001037 }
1038
Laurent Pincharta166d312014-07-24 01:36:43 +02001039 count = of_count_phandle_with_args(dev->of_node, "iommus",
1040 "#iommu-cells");
1041 if (count < 0)
1042 return -EINVAL;
Laurent Pinchart275f5052014-03-17 01:02:46 +01001043
Laurent Pincharta166d312014-07-24 01:36:43 +02001044 utlbs = kcalloc(count, sizeof(*utlbs), GFP_KERNEL);
1045 if (!utlbs)
1046 return -ENOMEM;
Laurent Pinchart275f5052014-03-17 01:02:46 +01001047
Laurent Pincharta166d312014-07-24 01:36:43 +02001048 for (i = 0; i < count; ++i) {
1049 struct of_phandle_args args;
1050 int ret;
Laurent Pinchart275f5052014-03-17 01:02:46 +01001051
Laurent Pincharta166d312014-07-24 01:36:43 +02001052 ret = of_parse_phandle_with_args(dev->of_node, "iommus",
1053 "#iommu-cells", i, &args);
1054 if (ret < 0)
1055 goto error;
1056
1057 of_node_put(args.np);
1058
1059 if (args.np != mmu->dev->of_node || args.args_count != 1)
1060 goto error;
1061
1062 utlbs[i] = args.args[0];
1063 }
1064
1065 *_utlbs = utlbs;
1066
1067 return count;
1068
1069error:
1070 kfree(utlbs);
1071 return -EINVAL;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001072}
1073
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001074static int ipmmu_add_device(struct device *dev)
1075{
Laurent Pinchart192d2042014-05-15 12:40:42 +02001076 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001077 struct ipmmu_vmsa_device *mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +02001078 struct iommu_group *group = NULL;
1079 unsigned int *utlbs = NULL;
1080 unsigned int i;
1081 int num_utlbs = 0;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001082 int ret;
1083
1084 if (dev->archdata.iommu) {
1085 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
1086 dev_name(dev));
1087 return -EINVAL;
1088 }
1089
1090 /* Find the master corresponding to the device. */
1091 spin_lock(&ipmmu_devices_lock);
1092
1093 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pincharta166d312014-07-24 01:36:43 +02001094 num_utlbs = ipmmu_find_utlbs(mmu, dev, &utlbs);
1095 if (num_utlbs) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001096 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +02001097 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001098 * against device removal.
1099 */
1100 break;
1101 }
1102 }
1103
1104 spin_unlock(&ipmmu_devices_lock);
1105
Laurent Pincharta166d312014-07-24 01:36:43 +02001106 if (num_utlbs <= 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001107 return -ENODEV;
1108
Laurent Pincharta166d312014-07-24 01:36:43 +02001109 for (i = 0; i < num_utlbs; ++i) {
1110 if (utlbs[i] >= mmu->num_utlbs) {
1111 ret = -EINVAL;
1112 goto error;
1113 }
1114 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001115
1116 /* Create a device group and add the device to it. */
1117 group = iommu_group_alloc();
1118 if (IS_ERR(group)) {
1119 dev_err(dev, "Failed to allocate IOMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +02001120 ret = PTR_ERR(group);
1121 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001122 }
1123
1124 ret = iommu_group_add_device(group, dev);
1125 iommu_group_put(group);
1126
1127 if (ret < 0) {
1128 dev_err(dev, "Failed to add device to IPMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +02001129 group = NULL;
1130 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001131 }
1132
Laurent Pinchart192d2042014-05-15 12:40:42 +02001133 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
1134 if (!archdata) {
1135 ret = -ENOMEM;
1136 goto error;
1137 }
1138
1139 archdata->mmu = mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +02001140 archdata->utlbs = utlbs;
1141 archdata->num_utlbs = num_utlbs;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001142 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001143
1144 /*
1145 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
1146 * VAs. This will allocate a corresponding IOMMU domain.
1147 *
1148 * TODO:
1149 * - Create one mapping per context (TLB).
1150 * - Make the mapping size configurable ? We currently use a 2GB mapping
1151 * at a 1GB offset to ensure that NULL VAs will fault.
1152 */
1153 if (!mmu->mapping) {
1154 struct dma_iommu_mapping *mapping;
1155
1156 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +02001157 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001158 if (IS_ERR(mapping)) {
1159 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +01001160 ret = PTR_ERR(mapping);
1161 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001162 }
1163
1164 mmu->mapping = mapping;
1165 }
1166
1167 /* Attach the ARM VA mapping to the device. */
1168 ret = arm_iommu_attach_device(dev, mmu->mapping);
1169 if (ret < 0) {
1170 dev_err(dev, "Failed to attach device to VA mapping\n");
1171 goto error;
1172 }
1173
1174 return 0;
1175
1176error:
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +01001177 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +02001178
Laurent Pinchart192d2042014-05-15 12:40:42 +02001179 kfree(dev->archdata.iommu);
Laurent Pincharta166d312014-07-24 01:36:43 +02001180 kfree(utlbs);
1181
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001182 dev->archdata.iommu = NULL;
Laurent Pincharta166d312014-07-24 01:36:43 +02001183
1184 if (!IS_ERR_OR_NULL(group))
1185 iommu_group_remove_device(dev);
1186
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001187 return ret;
1188}
1189
1190static void ipmmu_remove_device(struct device *dev)
1191{
Laurent Pincharta166d312014-07-24 01:36:43 +02001192 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
1193
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001194 arm_iommu_detach_device(dev);
1195 iommu_group_remove_device(dev);
Laurent Pincharta166d312014-07-24 01:36:43 +02001196
1197 kfree(archdata->utlbs);
1198 kfree(archdata);
1199
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001200 dev->archdata.iommu = NULL;
1201}
1202
Thierry Redingb22f6432014-06-27 09:03:12 +02001203static const struct iommu_ops ipmmu_ops = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001204 .domain_init = ipmmu_domain_init,
1205 .domain_destroy = ipmmu_domain_destroy,
1206 .attach_dev = ipmmu_attach_device,
1207 .detach_dev = ipmmu_detach_device,
1208 .map = ipmmu_map,
1209 .unmap = ipmmu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001210 .map_sg = default_iommu_map_sg,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001211 .iova_to_phys = ipmmu_iova_to_phys,
1212 .add_device = ipmmu_add_device,
1213 .remove_device = ipmmu_remove_device,
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +02001214 .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001215};
1216
1217/* -----------------------------------------------------------------------------
1218 * Probe/remove and init
1219 */
1220
1221static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
1222{
1223 unsigned int i;
1224
1225 /* Disable all contexts. */
1226 for (i = 0; i < 4; ++i)
1227 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
1228}
1229
1230static int ipmmu_probe(struct platform_device *pdev)
1231{
1232 struct ipmmu_vmsa_device *mmu;
1233 struct resource *res;
1234 int irq;
1235 int ret;
1236
Laurent Pinchart275f5052014-03-17 01:02:46 +01001237 if (!IS_ENABLED(CONFIG_OF) && !pdev->dev.platform_data) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001238 dev_err(&pdev->dev, "missing platform data\n");
1239 return -EINVAL;
1240 }
1241
1242 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
1243 if (!mmu) {
1244 dev_err(&pdev->dev, "cannot allocate device data\n");
1245 return -ENOMEM;
1246 }
1247
1248 mmu->dev = &pdev->dev;
1249 mmu->pdata = pdev->dev.platform_data;
1250 mmu->num_utlbs = 32;
1251
1252 /* Map I/O memory and request IRQ. */
1253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1254 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1255 if (IS_ERR(mmu->base))
1256 return PTR_ERR(mmu->base);
1257
Laurent Pinchart275f5052014-03-17 01:02:46 +01001258 /*
1259 * The IPMMU has two register banks, for secure and non-secure modes.
1260 * The bank mapped at the beginning of the IPMMU address space
1261 * corresponds to the running mode of the CPU. When running in secure
1262 * mode the non-secure register bank is also available at an offset.
1263 *
1264 * Secure mode operation isn't clearly documented and is thus currently
1265 * not implemented in the driver. Furthermore, preliminary tests of
1266 * non-secure operation with the main register bank were not successful.
1267 * Offset the registers base unconditionally to point to the non-secure
1268 * alias space for now.
1269 */
1270 mmu->base += IM_NS_ALIAS_OFFSET;
1271
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001272 irq = platform_get_irq(pdev, 0);
1273 if (irq < 0) {
1274 dev_err(&pdev->dev, "no IRQ found\n");
1275 return irq;
1276 }
1277
1278 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1279 dev_name(&pdev->dev), mmu);
1280 if (ret < 0) {
1281 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +08001282 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001283 }
1284
1285 ipmmu_device_reset(mmu);
1286
1287 /*
1288 * We can't create the ARM mapping here as it requires the bus to have
1289 * an IOMMU, which only happens when bus_set_iommu() is called in
1290 * ipmmu_init() after the probe function returns.
1291 */
1292
1293 spin_lock(&ipmmu_devices_lock);
1294 list_add(&mmu->list, &ipmmu_devices);
1295 spin_unlock(&ipmmu_devices_lock);
1296
1297 platform_set_drvdata(pdev, mmu);
1298
1299 return 0;
1300}
1301
1302static int ipmmu_remove(struct platform_device *pdev)
1303{
1304 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1305
1306 spin_lock(&ipmmu_devices_lock);
1307 list_del(&mmu->list);
1308 spin_unlock(&ipmmu_devices_lock);
1309
1310 arm_iommu_release_mapping(mmu->mapping);
1311
1312 ipmmu_device_reset(mmu);
1313
1314 return 0;
1315}
1316
Laurent Pinchart275f5052014-03-17 01:02:46 +01001317static const struct of_device_id ipmmu_of_ids[] = {
1318 { .compatible = "renesas,ipmmu-vmsa", },
1319};
1320
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001321static struct platform_driver ipmmu_driver = {
1322 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001323 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +01001324 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001325 },
1326 .probe = ipmmu_probe,
1327 .remove = ipmmu_remove,
1328};
1329
1330static int __init ipmmu_init(void)
1331{
1332 int ret;
1333
1334 ret = platform_driver_register(&ipmmu_driver);
1335 if (ret < 0)
1336 return ret;
1337
1338 if (!iommu_present(&platform_bus_type))
1339 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1340
1341 return 0;
1342}
1343
1344static void __exit ipmmu_exit(void)
1345{
1346 return platform_driver_unregister(&ipmmu_driver);
1347}
1348
1349subsys_initcall(ipmmu_init);
1350module_exit(ipmmu_exit);
1351
1352MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1353MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1354MODULE_LICENSE("GPL v2");