blob: 368c852d9ee7a7b925f81a17fe22a76e64242bce [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;
50 unsigned int utlb;
51};
52
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020053static DEFINE_SPINLOCK(ipmmu_devices_lock);
54static LIST_HEAD(ipmmu_devices);
55
56#define TLB_LOOP_TIMEOUT 100 /* 100us */
57
58/* -----------------------------------------------------------------------------
59 * Registers Definition
60 */
61
Laurent Pinchart275f5052014-03-17 01:02:46 +010062#define IM_NS_ALIAS_OFFSET 0x800
63
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020064#define IM_CTX_SIZE 0x40
65
66#define IMCTR 0x0000
67#define IMCTR_TRE (1 << 17)
68#define IMCTR_AFE (1 << 16)
69#define IMCTR_RTSEL_MASK (3 << 4)
70#define IMCTR_RTSEL_SHIFT 4
71#define IMCTR_TREN (1 << 3)
72#define IMCTR_INTEN (1 << 2)
73#define IMCTR_FLUSH (1 << 1)
74#define IMCTR_MMUEN (1 << 0)
75
76#define IMCAAR 0x0004
77
78#define IMTTBCR 0x0008
79#define IMTTBCR_EAE (1 << 31)
80#define IMTTBCR_PMB (1 << 30)
81#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
82#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
83#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
84#define IMTTBCR_SH1_MASK (3 << 28)
85#define IMTTBCR_ORGN1_NC (0 << 26)
86#define IMTTBCR_ORGN1_WB_WA (1 << 26)
87#define IMTTBCR_ORGN1_WT (2 << 26)
88#define IMTTBCR_ORGN1_WB (3 << 26)
89#define IMTTBCR_ORGN1_MASK (3 << 26)
90#define IMTTBCR_IRGN1_NC (0 << 24)
91#define IMTTBCR_IRGN1_WB_WA (1 << 24)
92#define IMTTBCR_IRGN1_WT (2 << 24)
93#define IMTTBCR_IRGN1_WB (3 << 24)
94#define IMTTBCR_IRGN1_MASK (3 << 24)
95#define IMTTBCR_TSZ1_MASK (7 << 16)
96#define IMTTBCR_TSZ1_SHIFT 16
97#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
98#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
99#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
100#define IMTTBCR_SH0_MASK (3 << 12)
101#define IMTTBCR_ORGN0_NC (0 << 10)
102#define IMTTBCR_ORGN0_WB_WA (1 << 10)
103#define IMTTBCR_ORGN0_WT (2 << 10)
104#define IMTTBCR_ORGN0_WB (3 << 10)
105#define IMTTBCR_ORGN0_MASK (3 << 10)
106#define IMTTBCR_IRGN0_NC (0 << 8)
107#define IMTTBCR_IRGN0_WB_WA (1 << 8)
108#define IMTTBCR_IRGN0_WT (2 << 8)
109#define IMTTBCR_IRGN0_WB (3 << 8)
110#define IMTTBCR_IRGN0_MASK (3 << 8)
111#define IMTTBCR_SL0_LVL_2 (0 << 4)
112#define IMTTBCR_SL0_LVL_1 (1 << 4)
113#define IMTTBCR_TSZ0_MASK (7 << 0)
114#define IMTTBCR_TSZ0_SHIFT O
115
116#define IMBUSCR 0x000c
117#define IMBUSCR_DVM (1 << 2)
118#define IMBUSCR_BUSSEL_SYS (0 << 0)
119#define IMBUSCR_BUSSEL_CCI (1 << 0)
120#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
121#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
122#define IMBUSCR_BUSSEL_MASK (3 << 0)
123
124#define IMTTLBR0 0x0010
125#define IMTTUBR0 0x0014
126#define IMTTLBR1 0x0018
127#define IMTTUBR1 0x001c
128
129#define IMSTR 0x0020
130#define IMSTR_ERRLVL_MASK (3 << 12)
131#define IMSTR_ERRLVL_SHIFT 12
132#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
133#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
134#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
135#define IMSTR_ERRCODE_MASK (7 << 8)
136#define IMSTR_MHIT (1 << 4)
137#define IMSTR_ABORT (1 << 2)
138#define IMSTR_PF (1 << 1)
139#define IMSTR_TF (1 << 0)
140
141#define IMMAIR0 0x0028
142#define IMMAIR1 0x002c
143#define IMMAIR_ATTR_MASK 0xff
144#define IMMAIR_ATTR_DEVICE 0x04
145#define IMMAIR_ATTR_NC 0x44
146#define IMMAIR_ATTR_WBRWA 0xff
147#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
148#define IMMAIR_ATTR_IDX_NC 0
149#define IMMAIR_ATTR_IDX_WBRWA 1
150#define IMMAIR_ATTR_IDX_DEV 2
151
152#define IMEAR 0x0030
153
154#define IMPCTR 0x0200
155#define IMPSTR 0x0208
156#define IMPEAR 0x020c
157#define IMPMBA(n) (0x0280 + ((n) * 4))
158#define IMPMBD(n) (0x02c0 + ((n) * 4))
159
160#define IMUCTR(n) (0x0300 + ((n) * 16))
161#define IMUCTR_FIXADDEN (1 << 31)
162#define IMUCTR_FIXADD_MASK (0xff << 16)
163#define IMUCTR_FIXADD_SHIFT 16
164#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
165#define IMUCTR_TTSEL_PMB (8 << 4)
166#define IMUCTR_TTSEL_MASK (15 << 4)
167#define IMUCTR_FLUSH (1 << 1)
168#define IMUCTR_MMUEN (1 << 0)
169
170#define IMUASID(n) (0x0308 + ((n) * 16))
171#define IMUASID_ASID8_MASK (0xff << 8)
172#define IMUASID_ASID8_SHIFT 8
173#define IMUASID_ASID0_MASK (0xff << 0)
174#define IMUASID_ASID0_SHIFT 0
175
176/* -----------------------------------------------------------------------------
177 * Page Table Bits
178 */
179
180/*
181 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access,
182 * Long-descriptor format" that the NStable bit being set in a table descriptor
183 * will result in the NStable and NS bits of all child entries being ignored and
184 * considered as being set. The IPMMU seems not to comply with this, as it
185 * generates a secure access page fault if any of the NStable and NS bits isn't
186 * set when running in non-secure mode.
187 */
188#ifndef PMD_NSTABLE
189#define PMD_NSTABLE (_AT(pmdval_t, 1) << 63)
190#endif
191
192#define ARM_VMSA_PTE_XN (((pteval_t)3) << 53)
193#define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52)
194#define ARM_VMSA_PTE_AF (((pteval_t)1) << 10)
195#define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8)
196#define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8)
197#define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8)
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200198#define ARM_VMSA_PTE_SH_MASK (((pteval_t)3) << 8)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200199#define ARM_VMSA_PTE_NS (((pteval_t)1) << 5)
200#define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0)
201
202/* Stage-1 PTE */
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200203#define ARM_VMSA_PTE_nG (((pteval_t)1) << 11)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200204#define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6)
205#define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6)
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200206#define ARM_VMSA_PTE_AP_MASK (((pteval_t)3) << 6)
207#define ARM_VMSA_PTE_ATTRINDX_MASK (((pteval_t)3) << 2)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200208#define ARM_VMSA_PTE_ATTRINDX_SHIFT 2
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200209
210#define ARM_VMSA_PTE_ATTRS_MASK \
211 (ARM_VMSA_PTE_XN | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_nG | \
212 ARM_VMSA_PTE_AF | ARM_VMSA_PTE_SH_MASK | ARM_VMSA_PTE_AP_MASK | \
213 ARM_VMSA_PTE_NS | ARM_VMSA_PTE_ATTRINDX_MASK)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200214
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200215#define ARM_VMSA_PTE_CONT_ENTRIES 16
216#define ARM_VMSA_PTE_CONT_SIZE (PAGE_SIZE * ARM_VMSA_PTE_CONT_ENTRIES)
217
Laurent Pinchartbc281912014-05-15 12:40:45 +0200218#define IPMMU_PTRS_PER_PTE 512
219#define IPMMU_PTRS_PER_PMD 512
220#define IPMMU_PTRS_PER_PGD 4
Laurent Pinchartbc281912014-05-15 12:40:45 +0200221
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200222/* -----------------------------------------------------------------------------
223 * Read/Write Access
224 */
225
226static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
227{
228 return ioread32(mmu->base + offset);
229}
230
231static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
232 u32 data)
233{
234 iowrite32(data, mmu->base + offset);
235}
236
237static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
238{
239 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
240}
241
242static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
243 u32 data)
244{
245 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
246}
247
248/* -----------------------------------------------------------------------------
249 * TLB and microTLB Management
250 */
251
252/* Wait for any pending TLB invalidations to complete */
253static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
254{
255 unsigned int count = 0;
256
257 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
258 cpu_relax();
259 if (++count == TLB_LOOP_TIMEOUT) {
260 dev_err_ratelimited(domain->mmu->dev,
261 "TLB sync timed out -- MMU may be deadlocked\n");
262 return;
263 }
264 udelay(1);
265 }
266}
267
268static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
269{
270 u32 reg;
271
272 reg = ipmmu_ctx_read(domain, IMCTR);
273 reg |= IMCTR_FLUSH;
274 ipmmu_ctx_write(domain, IMCTR, reg);
275
276 ipmmu_tlb_sync(domain);
277}
278
279/*
280 * Enable MMU translation for the microTLB.
281 */
282static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200283 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200284{
285 struct ipmmu_vmsa_device *mmu = domain->mmu;
286
Laurent Pinchart192d2042014-05-15 12:40:42 +0200287 /*
288 * TODO: Reference-count the microTLB as several bus masters can be
289 * connected to the same microTLB.
290 */
291
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200292 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200293 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200294 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200295 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200296 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
297 IMUCTR_MMUEN);
298}
299
300/*
301 * Disable MMU translation for the microTLB.
302 */
303static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200304 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200305{
306 struct ipmmu_vmsa_device *mmu = domain->mmu;
307
Laurent Pinchart192d2042014-05-15 12:40:42 +0200308 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200309}
310
311static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr,
312 size_t size)
313{
314 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
315
316 /*
317 * TODO: Add support for coherent walk through CCI with DVM and remove
318 * cache handling.
319 */
320 dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE);
321}
322
323/* -----------------------------------------------------------------------------
324 * Domain/Context Management
325 */
326
327static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
328{
329 phys_addr_t ttbr;
330 u32 reg;
331
332 /*
333 * TODO: When adding support for multiple contexts, find an unused
334 * context.
335 */
336 domain->context_id = 0;
337
338 /* TTBR0 */
339 ipmmu_flush_pgtable(domain->mmu, domain->pgd,
Laurent Pinchartbc281912014-05-15 12:40:45 +0200340 IPMMU_PTRS_PER_PGD * sizeof(*domain->pgd));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200341 ttbr = __pa(domain->pgd);
342 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
343 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
344
345 /*
346 * TTBCR
347 * We use long descriptors with inner-shareable WBWA tables and allocate
348 * the whole 32-bit VA space to TTBR0.
349 */
350 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
351 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
352 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
353
354 /*
355 * MAIR0
356 * We need three attributes only, non-cacheable, write-back read/write
357 * allocate and device memory.
358 */
359 reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC))
360 | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA))
361 | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV));
362 ipmmu_ctx_write(domain, IMMAIR0, reg);
363
364 /* IMBUSCR */
365 ipmmu_ctx_write(domain, IMBUSCR,
366 ipmmu_ctx_read(domain, IMBUSCR) &
367 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
368
369 /*
370 * IMSTR
371 * Clear all interrupt flags.
372 */
373 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
374
375 /*
376 * IMCTR
377 * Enable the MMU and interrupt generation. The long-descriptor
378 * translation table format doesn't use TEX remapping. Don't enable AF
379 * software management as we have no use for it. Flush the TLB as
380 * required when modifying the context registers.
381 */
382 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
383
384 return 0;
385}
386
387static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
388{
389 /*
390 * Disable the context. Flush the TLB as required when modifying the
391 * context registers.
392 *
393 * TODO: Is TLB flush really needed ?
394 */
395 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
396 ipmmu_tlb_sync(domain);
397}
398
399/* -----------------------------------------------------------------------------
400 * Fault Handling
401 */
402
403static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
404{
405 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
406 struct ipmmu_vmsa_device *mmu = domain->mmu;
407 u32 status;
408 u32 iova;
409
410 status = ipmmu_ctx_read(domain, IMSTR);
411 if (!(status & err_mask))
412 return IRQ_NONE;
413
414 iova = ipmmu_ctx_read(domain, IMEAR);
415
416 /*
417 * Clear the error status flags. Unlike traditional interrupt flag
418 * registers that must be cleared by writing 1, this status register
419 * seems to require 0. The error address register must be read before,
420 * otherwise its value will be 0.
421 */
422 ipmmu_ctx_write(domain, IMSTR, 0);
423
424 /* Log fatal errors. */
425 if (status & IMSTR_MHIT)
426 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
427 iova);
428 if (status & IMSTR_ABORT)
429 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
430 iova);
431
432 if (!(status & (IMSTR_PF | IMSTR_TF)))
433 return IRQ_NONE;
434
435 /*
436 * Try to handle page faults and translation faults.
437 *
438 * TODO: We need to look up the faulty device based on the I/O VA. Use
439 * the IOMMU device for now.
440 */
441 if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
442 return IRQ_HANDLED;
443
444 dev_err_ratelimited(mmu->dev,
445 "Unhandled fault: status 0x%08x iova 0x%08x\n",
446 status, iova);
447
448 return IRQ_HANDLED;
449}
450
451static irqreturn_t ipmmu_irq(int irq, void *dev)
452{
453 struct ipmmu_vmsa_device *mmu = dev;
454 struct iommu_domain *io_domain;
455 struct ipmmu_vmsa_domain *domain;
456
457 if (!mmu->mapping)
458 return IRQ_NONE;
459
460 io_domain = mmu->mapping->domain;
461 domain = io_domain->priv;
462
463 return ipmmu_domain_irq(domain);
464}
465
466/* -----------------------------------------------------------------------------
467 * Page Table Management
468 */
469
Laurent Pinchart14e51232014-05-15 12:40:47 +0200470#define pud_pgtable(pud) pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK))
471
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200472static void ipmmu_free_ptes(pmd_t *pmd)
473{
474 pgtable_t table = pmd_pgtable(*pmd);
475 __free_page(table);
476}
477
478static void ipmmu_free_pmds(pud_t *pud)
479{
Laurent Pinchart14e51232014-05-15 12:40:47 +0200480 pmd_t *pmd = pmd_offset(pud, 0);
481 pgtable_t table;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200482 unsigned int i;
483
Laurent Pinchartbc281912014-05-15 12:40:45 +0200484 for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200485 if (!pmd_table(*pmd))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200486 continue;
487
488 ipmmu_free_ptes(pmd);
489 pmd++;
490 }
491
Laurent Pinchart14e51232014-05-15 12:40:47 +0200492 table = pud_pgtable(*pud);
493 __free_page(table);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200494}
495
496static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain)
497{
498 pgd_t *pgd, *pgd_base = domain->pgd;
499 unsigned int i;
500
501 /*
502 * Recursively free the page tables for this domain. We don't care about
503 * speculative TLB filling, because the TLB will be nuked next time this
504 * context bank is re-allocated and no devices currently map to these
505 * tables.
506 */
507 pgd = pgd_base;
Laurent Pinchartbc281912014-05-15 12:40:45 +0200508 for (i = 0; i < IPMMU_PTRS_PER_PGD; ++i) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200509 if (pgd_none(*pgd))
510 continue;
Laurent Pinchart14e51232014-05-15 12:40:47 +0200511 ipmmu_free_pmds((pud_t *)pgd);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200512 pgd++;
513 }
514
515 kfree(pgd_base);
516}
517
518/*
519 * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
520 * functions as they would flush the CPU TLB.
521 */
522
Laurent Pinchart9009f252014-05-15 12:40:48 +0200523static pte_t *ipmmu_alloc_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
524 unsigned long iova)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200525{
Laurent Pinchart9009f252014-05-15 12:40:48 +0200526 pte_t *pte;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200527
Laurent Pinchart9009f252014-05-15 12:40:48 +0200528 if (!pmd_none(*pmd))
529 return pte_offset_kernel(pmd, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200530
Laurent Pinchart9009f252014-05-15 12:40:48 +0200531 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
532 if (!pte)
533 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200534
Laurent Pinchart9009f252014-05-15 12:40:48 +0200535 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));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200538
Laurent Pinchart9009f252014-05-15 12:40:48 +0200539 return pte + pte_index(iova);
540}
541
542static pmd_t *ipmmu_alloc_pmd(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
543 unsigned long iova)
544{
545 pud_t *pud = (pud_t *)pgd;
546 pmd_t *pmd;
547
548 if (!pud_none(*pud))
549 return pmd_offset(pud, iova);
550
551 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
552 if (!pmd)
553 return NULL;
554
555 ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
556 *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
557 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
558
559 return pmd + pmd_index(iova);
560}
561
562static u64 ipmmu_page_prot(unsigned int prot, u64 type)
563{
Laurent Pinchart04561ca2014-12-15 20:41:13 +0200564 u64 pgprot = ARM_VMSA_PTE_nG | ARM_VMSA_PTE_AF
Laurent Pinchart9009f252014-05-15 12:40:48 +0200565 | ARM_VMSA_PTE_SH_IS | ARM_VMSA_PTE_AP_UNPRIV
566 | ARM_VMSA_PTE_NS | type;
567
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200568 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200569 pgprot |= ARM_VMSA_PTE_AP_RDONLY;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200570
571 if (prot & IOMMU_CACHE)
Laurent Pinchart9009f252014-05-15 12:40:48 +0200572 pgprot |= IMMAIR_ATTR_IDX_WBRWA << ARM_VMSA_PTE_ATTRINDX_SHIFT;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200573
Laurent Pinchart04561ca2014-12-15 20:41:13 +0200574 if (prot & IOMMU_NOEXEC)
575 pgprot |= ARM_VMSA_PTE_XN;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200576 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Laurent Pinchart9009f252014-05-15 12:40:48 +0200577 /* If no access create a faulting entry to avoid TLB fills. */
578 pgprot &= ~ARM_VMSA_PTE_PAGE;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200579
Laurent Pinchart9009f252014-05-15 12:40:48 +0200580 return pgprot;
581}
582
583static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
584 unsigned long iova, unsigned long pfn,
585 size_t size, int prot)
586{
587 pteval_t pteval = ipmmu_page_prot(prot, ARM_VMSA_PTE_PAGE);
588 unsigned int num_ptes = 1;
589 pte_t *pte, *start;
590 unsigned int i;
591
592 pte = ipmmu_alloc_pte(mmu, pmd, iova);
593 if (!pte)
594 return -ENOMEM;
595
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200596 start = pte;
597
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200598 /*
Laurent Pinchart9009f252014-05-15 12:40:48 +0200599 * Install the page table entries. We can be called both for a single
600 * page or for a block of 16 physically contiguous pages. In the latter
601 * case set the PTE contiguous hint.
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200602 */
Laurent Pinchart9009f252014-05-15 12:40:48 +0200603 if (size == SZ_64K) {
604 pteval |= ARM_VMSA_PTE_CONT;
605 num_ptes = ARM_VMSA_PTE_CONT_ENTRIES;
606 }
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200607
Laurent Pinchart9009f252014-05-15 12:40:48 +0200608 for (i = num_ptes; i; --i)
609 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
Laurent Pinchart4ee3cc92014-05-15 12:40:46 +0200610
Laurent Pinchart9009f252014-05-15 12:40:48 +0200611 ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * num_ptes);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200612
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200613 return 0;
614}
615
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200616static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
617 unsigned long iova, unsigned long pfn,
618 int prot)
619{
620 pmdval_t pmdval = ipmmu_page_prot(prot, PMD_TYPE_SECT);
621
622 *pmd = pfn_pmd(pfn, __pgprot(pmdval));
623 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
624
625 return 0;
626}
627
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200628static int ipmmu_create_mapping(struct ipmmu_vmsa_domain *domain,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200629 unsigned long iova, phys_addr_t paddr,
630 size_t size, int prot)
631{
632 struct ipmmu_vmsa_device *mmu = domain->mmu;
633 pgd_t *pgd = domain->pgd;
634 unsigned long flags;
Laurent Pinchart9009f252014-05-15 12:40:48 +0200635 unsigned long pfn;
636 pmd_t *pmd;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200637 int ret;
638
639 if (!pgd)
640 return -EINVAL;
641
642 if (size & ~PAGE_MASK)
643 return -EINVAL;
644
645 if (paddr & ~((1ULL << 40) - 1))
646 return -ERANGE;
647
Laurent Pinchart9009f252014-05-15 12:40:48 +0200648 pfn = __phys_to_pfn(paddr);
649 pgd += pgd_index(iova);
650
651 /* Update the page tables. */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200652 spin_lock_irqsave(&domain->lock, flags);
653
Laurent Pinchart9009f252014-05-15 12:40:48 +0200654 pmd = ipmmu_alloc_pmd(mmu, pgd, iova);
655 if (!pmd) {
656 ret = -ENOMEM;
657 goto done;
658 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200659
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200660 switch (size) {
661 case SZ_2M:
662 ret = ipmmu_alloc_init_pmd(mmu, pmd, iova, pfn, prot);
663 break;
664 case SZ_64K:
665 case SZ_4K:
666 ret = ipmmu_alloc_init_pte(mmu, pmd, iova, pfn, size, prot);
667 break;
668 default:
669 ret = -EINVAL;
670 break;
671 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200672
Laurent Pinchart9009f252014-05-15 12:40:48 +0200673done:
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200674 spin_unlock_irqrestore(&domain->lock, flags);
675
Laurent Pinchart9009f252014-05-15 12:40:48 +0200676 if (!ret)
677 ipmmu_tlb_invalidate(domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200678
679 return ret;
680}
681
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200682static void ipmmu_clear_pud(struct ipmmu_vmsa_device *mmu, pud_t *pud)
683{
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200684 pgtable_t table = pud_pgtable(*pud);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200685
686 /* Clear the PUD. */
687 *pud = __pud(0);
688 ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200689
690 /* Free the page table. */
691 __free_page(table);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200692}
693
694static void ipmmu_clear_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud,
695 pmd_t *pmd)
696{
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200697 pmd_t pmdval = *pmd;
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200698 unsigned int i;
699
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200700 /* Clear the PMD. */
701 *pmd = __pmd(0);
702 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
703
Laurent Pinchart22463ca2014-07-24 15:34:54 +0200704 /* Free the page table. */
705 if (pmd_table(pmdval)) {
706 pgtable_t table = pmd_pgtable(pmdval);
707
708 __free_page(table);
709 }
710
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200711 /* Check whether the PUD is still needed. */
712 pmd = pmd_offset(pud, 0);
713 for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
714 if (!pmd_none(pmd[i]))
715 return;
716 }
717
718 /* Clear the parent PUD. */
719 ipmmu_clear_pud(mmu, pud);
720}
721
722static void ipmmu_clear_pte(struct ipmmu_vmsa_device *mmu, pud_t *pud,
723 pmd_t *pmd, pte_t *pte, unsigned int num_ptes)
724{
725 unsigned int i;
726
727 /* Clear the PTE. */
728 for (i = num_ptes; i; --i)
729 pte[i-1] = __pte(0);
730
731 ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * num_ptes);
732
733 /* Check whether the PMD is still needed. */
734 pte = pte_offset_kernel(pmd, 0);
735 for (i = 0; i < IPMMU_PTRS_PER_PTE; ++i) {
736 if (!pte_none(pte[i]))
737 return;
738 }
739
740 /* Clear the parent PMD. */
741 ipmmu_clear_pmd(mmu, pud, pmd);
742}
743
744static int ipmmu_split_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd)
745{
746 pte_t *pte, *start;
747 pteval_t pteval;
748 unsigned long pfn;
749 unsigned int i;
750
751 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
752 if (!pte)
753 return -ENOMEM;
754
755 /* Copy the PMD attributes. */
756 pteval = (pmd_val(*pmd) & ARM_VMSA_PTE_ATTRS_MASK)
757 | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_PAGE;
758
759 pfn = pmd_pfn(*pmd);
760 start = pte;
761
762 for (i = IPMMU_PTRS_PER_PTE; i; --i)
763 *pte++ = pfn_pte(pfn++, __pgprot(pteval));
764
765 ipmmu_flush_pgtable(mmu, start, PAGE_SIZE);
766 *pmd = __pmd(__pa(start) | PMD_NSTABLE | PMD_TYPE_TABLE);
767 ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
768
769 return 0;
770}
771
772static void ipmmu_split_pte(struct ipmmu_vmsa_device *mmu, pte_t *pte)
773{
774 unsigned int i;
775
776 for (i = ARM_VMSA_PTE_CONT_ENTRIES; i; --i)
777 pte[i-1] = __pte(pte_val(*pte) & ~ARM_VMSA_PTE_CONT);
778
779 ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * ARM_VMSA_PTE_CONT_ENTRIES);
780}
781
782static int ipmmu_clear_mapping(struct ipmmu_vmsa_domain *domain,
783 unsigned long iova, size_t size)
784{
785 struct ipmmu_vmsa_device *mmu = domain->mmu;
786 unsigned long flags;
787 pgd_t *pgd = domain->pgd;
788 pud_t *pud;
789 pmd_t *pmd;
790 pte_t *pte;
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200791
792 if (!pgd)
793 return -EINVAL;
794
795 if (size & ~PAGE_MASK)
796 return -EINVAL;
797
798 pgd += pgd_index(iova);
799 pud = (pud_t *)pgd;
800
801 spin_lock_irqsave(&domain->lock, flags);
802
803 /* If there's no PUD or PMD we're done. */
804 if (pud_none(*pud))
805 goto done;
806
807 pmd = pmd_offset(pud, iova);
808 if (pmd_none(*pmd))
809 goto done;
810
811 /*
812 * When freeing a 2MB block just clear the PMD. In the unlikely case the
813 * block is mapped as individual pages this will free the corresponding
814 * PTE page table.
815 */
816 if (size == SZ_2M) {
817 ipmmu_clear_pmd(mmu, pud, pmd);
818 goto done;
819 }
820
821 /*
822 * If the PMD has been mapped as a section remap it as pages to allow
823 * freeing individual pages.
824 */
825 if (pmd_sect(*pmd))
826 ipmmu_split_pmd(mmu, pmd);
827
828 pte = pte_offset_kernel(pmd, iova);
829
830 /*
831 * When freeing a 64kB block just clear the PTE entries. We don't have
832 * to care about the contiguous hint of the surrounding entries.
833 */
834 if (size == SZ_64K) {
835 ipmmu_clear_pte(mmu, pud, pmd, pte, ARM_VMSA_PTE_CONT_ENTRIES);
836 goto done;
837 }
838
839 /*
840 * If the PTE has been mapped with the contiguous hint set remap it and
841 * its surrounding PTEs to allow unmapping a single page.
842 */
843 if (pte_val(*pte) & ARM_VMSA_PTE_CONT)
844 ipmmu_split_pte(mmu, pte);
845
846 /* Clear the PTE. */
847 ipmmu_clear_pte(mmu, pud, pmd, pte, 1);
848
849done:
850 spin_unlock_irqrestore(&domain->lock, flags);
851
Laurent Pinchart9eca0a52014-07-24 15:34:54 +0200852 ipmmu_tlb_invalidate(domain);
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200853
854 return 0;
855}
856
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200857/* -----------------------------------------------------------------------------
858 * IOMMU Operations
859 */
860
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200861static int ipmmu_domain_init(struct iommu_domain *io_domain)
862{
863 struct ipmmu_vmsa_domain *domain;
864
865 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
866 if (!domain)
867 return -ENOMEM;
868
869 spin_lock_init(&domain->lock);
870
Laurent Pinchartbc281912014-05-15 12:40:45 +0200871 domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200872 if (!domain->pgd) {
873 kfree(domain);
874 return -ENOMEM;
875 }
876
877 io_domain->priv = domain;
878 domain->io_domain = io_domain;
879
880 return 0;
881}
882
883static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
884{
885 struct ipmmu_vmsa_domain *domain = io_domain->priv;
886
887 /*
888 * Free the domain resources. We assume that all devices have already
889 * been detached.
890 */
891 ipmmu_domain_destroy_context(domain);
892 ipmmu_free_pgtables(domain);
893 kfree(domain);
894}
895
896static int ipmmu_attach_device(struct iommu_domain *io_domain,
897 struct device *dev)
898{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200899 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
900 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200901 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200902 unsigned long flags;
903 int ret = 0;
904
905 if (!mmu) {
906 dev_err(dev, "Cannot attach to IPMMU\n");
907 return -ENXIO;
908 }
909
910 spin_lock_irqsave(&domain->lock, flags);
911
912 if (!domain->mmu) {
913 /* The domain hasn't been used yet, initialize it. */
914 domain->mmu = mmu;
915 ret = ipmmu_domain_init_context(domain);
916 } else if (domain->mmu != mmu) {
917 /*
918 * Something is wrong, we can't attach two devices using
919 * different IOMMUs to the same domain.
920 */
921 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
922 dev_name(mmu->dev), dev_name(domain->mmu->dev));
923 ret = -EINVAL;
924 }
925
926 spin_unlock_irqrestore(&domain->lock, flags);
927
928 if (ret < 0)
929 return ret;
930
Laurent Pinchart192d2042014-05-15 12:40:42 +0200931 ipmmu_utlb_enable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200932
933 return 0;
934}
935
936static void ipmmu_detach_device(struct iommu_domain *io_domain,
937 struct device *dev)
938{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200939 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200940 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200941
Laurent Pinchart192d2042014-05-15 12:40:42 +0200942 ipmmu_utlb_disable(domain, archdata->utlb);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200943
944 /*
945 * TODO: Optimize by disabling the context when no device is attached.
946 */
947}
948
949static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
950 phys_addr_t paddr, size_t size, int prot)
951{
952 struct ipmmu_vmsa_domain *domain = io_domain->priv;
953
954 if (!domain)
955 return -ENODEV;
956
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200957 return ipmmu_create_mapping(domain, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200958}
959
960static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
961 size_t size)
962{
963 struct ipmmu_vmsa_domain *domain = io_domain->priv;
964 int ret;
965
Laurent Pinchart004c5b32014-05-15 12:40:51 +0200966 ret = ipmmu_clear_mapping(domain, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200967 return ret ? 0 : size;
968}
969
970static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
971 dma_addr_t iova)
972{
973 struct ipmmu_vmsa_domain *domain = io_domain->priv;
974 pgd_t pgd;
975 pud_t pud;
976 pmd_t pmd;
977 pte_t pte;
978
979 /* TODO: Is locking needed ? */
980
981 if (!domain->pgd)
982 return 0;
983
984 pgd = *(domain->pgd + pgd_index(iova));
985 if (pgd_none(pgd))
986 return 0;
987
988 pud = *pud_offset(&pgd, iova);
989 if (pud_none(pud))
990 return 0;
991
992 pmd = *pmd_offset(&pud, iova);
993 if (pmd_none(pmd))
994 return 0;
995
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +0200996 if (pmd_sect(pmd))
997 return __pfn_to_phys(pmd_pfn(pmd)) | (iova & ~PMD_MASK);
998
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200999 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
1000 if (pte_none(pte))
1001 return 0;
1002
1003 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
1004}
1005
Laurent Pinchart192d2042014-05-15 12:40:42 +02001006static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev)
1007{
Laurent Pinchart275f5052014-03-17 01:02:46 +01001008 struct of_phandle_args args;
1009 int ret;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001010
Laurent Pinchart275f5052014-03-17 01:02:46 +01001011 if (mmu->pdata) {
1012 const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
1013 const char *devname = dev_name(dev);
1014 unsigned int i;
1015
1016 for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
1017 if (strcmp(master->name, devname) == 0)
1018 return master->utlb;
1019 }
1020
1021 return -1;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001022 }
1023
Laurent Pinchart275f5052014-03-17 01:02:46 +01001024 ret = of_parse_phandle_with_args(dev->of_node, "iommus",
1025 "#iommu-cells", 0, &args);
1026 if (ret < 0)
1027 return -1;
1028
1029 of_node_put(args.np);
1030
1031 if (args.np != mmu->dev->of_node || args.args_count != 1)
1032 return -1;
1033
1034 return args.args[0];
Laurent Pinchart192d2042014-05-15 12:40:42 +02001035}
1036
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001037static int ipmmu_add_device(struct device *dev)
1038{
Laurent Pinchart192d2042014-05-15 12:40:42 +02001039 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001040 struct ipmmu_vmsa_device *mmu;
1041 struct iommu_group *group;
Laurent Pinchart192d2042014-05-15 12:40:42 +02001042 int utlb = -1;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001043 int ret;
1044
1045 if (dev->archdata.iommu) {
1046 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
1047 dev_name(dev));
1048 return -EINVAL;
1049 }
1050
1051 /* Find the master corresponding to the device. */
1052 spin_lock(&ipmmu_devices_lock);
1053
1054 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pinchart192d2042014-05-15 12:40:42 +02001055 utlb = ipmmu_find_utlb(mmu, dev);
1056 if (utlb >= 0) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001057 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +02001058 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001059 * against device removal.
1060 */
1061 break;
1062 }
1063 }
1064
1065 spin_unlock(&ipmmu_devices_lock);
1066
Laurent Pinchart192d2042014-05-15 12:40:42 +02001067 if (utlb < 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001068 return -ENODEV;
1069
Laurent Pinchart192d2042014-05-15 12:40:42 +02001070 if (utlb >= mmu->num_utlbs)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001071 return -EINVAL;
1072
1073 /* Create a device group and add the device to it. */
1074 group = iommu_group_alloc();
1075 if (IS_ERR(group)) {
1076 dev_err(dev, "Failed to allocate IOMMU group\n");
1077 return PTR_ERR(group);
1078 }
1079
1080 ret = iommu_group_add_device(group, dev);
1081 iommu_group_put(group);
1082
1083 if (ret < 0) {
1084 dev_err(dev, "Failed to add device to IPMMU group\n");
1085 return ret;
1086 }
1087
Laurent Pinchart192d2042014-05-15 12:40:42 +02001088 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
1089 if (!archdata) {
1090 ret = -ENOMEM;
1091 goto error;
1092 }
1093
1094 archdata->mmu = mmu;
1095 archdata->utlb = utlb;
1096 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001097
1098 /*
1099 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
1100 * VAs. This will allocate a corresponding IOMMU domain.
1101 *
1102 * TODO:
1103 * - Create one mapping per context (TLB).
1104 * - Make the mapping size configurable ? We currently use a 2GB mapping
1105 * at a 1GB offset to ensure that NULL VAs will fault.
1106 */
1107 if (!mmu->mapping) {
1108 struct dma_iommu_mapping *mapping;
1109
1110 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +02001111 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001112 if (IS_ERR(mapping)) {
1113 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +01001114 ret = PTR_ERR(mapping);
1115 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001116 }
1117
1118 mmu->mapping = mapping;
1119 }
1120
1121 /* Attach the ARM VA mapping to the device. */
1122 ret = arm_iommu_attach_device(dev, mmu->mapping);
1123 if (ret < 0) {
1124 dev_err(dev, "Failed to attach device to VA mapping\n");
1125 goto error;
1126 }
1127
1128 return 0;
1129
1130error:
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +01001131 arm_iommu_release_mapping(mmu->mapping);
Laurent Pinchart192d2042014-05-15 12:40:42 +02001132 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001133 dev->archdata.iommu = NULL;
1134 iommu_group_remove_device(dev);
1135 return ret;
1136}
1137
1138static void ipmmu_remove_device(struct device *dev)
1139{
1140 arm_iommu_detach_device(dev);
1141 iommu_group_remove_device(dev);
Laurent Pinchart192d2042014-05-15 12:40:42 +02001142 kfree(dev->archdata.iommu);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001143 dev->archdata.iommu = NULL;
1144}
1145
Thierry Redingb22f6432014-06-27 09:03:12 +02001146static const struct iommu_ops ipmmu_ops = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001147 .domain_init = ipmmu_domain_init,
1148 .domain_destroy = ipmmu_domain_destroy,
1149 .attach_dev = ipmmu_attach_device,
1150 .detach_dev = ipmmu_detach_device,
1151 .map = ipmmu_map,
1152 .unmap = ipmmu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07001153 .map_sg = default_iommu_map_sg,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001154 .iova_to_phys = ipmmu_iova_to_phys,
1155 .add_device = ipmmu_add_device,
1156 .remove_device = ipmmu_remove_device,
Laurent Pinchartdda7c2e42014-05-15 12:40:49 +02001157 .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001158};
1159
1160/* -----------------------------------------------------------------------------
1161 * Probe/remove and init
1162 */
1163
1164static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
1165{
1166 unsigned int i;
1167
1168 /* Disable all contexts. */
1169 for (i = 0; i < 4; ++i)
1170 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
1171}
1172
1173static int ipmmu_probe(struct platform_device *pdev)
1174{
1175 struct ipmmu_vmsa_device *mmu;
1176 struct resource *res;
1177 int irq;
1178 int ret;
1179
Laurent Pinchart275f5052014-03-17 01:02:46 +01001180 if (!IS_ENABLED(CONFIG_OF) && !pdev->dev.platform_data) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001181 dev_err(&pdev->dev, "missing platform data\n");
1182 return -EINVAL;
1183 }
1184
1185 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
1186 if (!mmu) {
1187 dev_err(&pdev->dev, "cannot allocate device data\n");
1188 return -ENOMEM;
1189 }
1190
1191 mmu->dev = &pdev->dev;
1192 mmu->pdata = pdev->dev.platform_data;
1193 mmu->num_utlbs = 32;
1194
1195 /* Map I/O memory and request IRQ. */
1196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1197 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1198 if (IS_ERR(mmu->base))
1199 return PTR_ERR(mmu->base);
1200
Laurent Pinchart275f5052014-03-17 01:02:46 +01001201 /*
1202 * The IPMMU has two register banks, for secure and non-secure modes.
1203 * The bank mapped at the beginning of the IPMMU address space
1204 * corresponds to the running mode of the CPU. When running in secure
1205 * mode the non-secure register bank is also available at an offset.
1206 *
1207 * Secure mode operation isn't clearly documented and is thus currently
1208 * not implemented in the driver. Furthermore, preliminary tests of
1209 * non-secure operation with the main register bank were not successful.
1210 * Offset the registers base unconditionally to point to the non-secure
1211 * alias space for now.
1212 */
1213 mmu->base += IM_NS_ALIAS_OFFSET;
1214
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001215 irq = platform_get_irq(pdev, 0);
1216 if (irq < 0) {
1217 dev_err(&pdev->dev, "no IRQ found\n");
1218 return irq;
1219 }
1220
1221 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1222 dev_name(&pdev->dev), mmu);
1223 if (ret < 0) {
1224 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +08001225 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001226 }
1227
1228 ipmmu_device_reset(mmu);
1229
1230 /*
1231 * We can't create the ARM mapping here as it requires the bus to have
1232 * an IOMMU, which only happens when bus_set_iommu() is called in
1233 * ipmmu_init() after the probe function returns.
1234 */
1235
1236 spin_lock(&ipmmu_devices_lock);
1237 list_add(&mmu->list, &ipmmu_devices);
1238 spin_unlock(&ipmmu_devices_lock);
1239
1240 platform_set_drvdata(pdev, mmu);
1241
1242 return 0;
1243}
1244
1245static int ipmmu_remove(struct platform_device *pdev)
1246{
1247 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1248
1249 spin_lock(&ipmmu_devices_lock);
1250 list_del(&mmu->list);
1251 spin_unlock(&ipmmu_devices_lock);
1252
1253 arm_iommu_release_mapping(mmu->mapping);
1254
1255 ipmmu_device_reset(mmu);
1256
1257 return 0;
1258}
1259
Laurent Pinchart275f5052014-03-17 01:02:46 +01001260static const struct of_device_id ipmmu_of_ids[] = {
1261 { .compatible = "renesas,ipmmu-vmsa", },
1262};
1263
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001264static struct platform_driver ipmmu_driver = {
1265 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001266 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +01001267 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001268 },
1269 .probe = ipmmu_probe,
1270 .remove = ipmmu_remove,
1271};
1272
1273static int __init ipmmu_init(void)
1274{
1275 int ret;
1276
1277 ret = platform_driver_register(&ipmmu_driver);
1278 if (ret < 0)
1279 return ret;
1280
1281 if (!iommu_present(&platform_bus_type))
1282 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1283
1284 return 0;
1285}
1286
1287static void __exit ipmmu_exit(void)
1288{
1289 return platform_driver_unregister(&ipmmu_driver);
1290}
1291
1292subsys_initcall(ipmmu_init);
1293module_exit(ipmmu_exit);
1294
1295MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1296MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1297MODULE_LICENSE("GPL v2");