blob: 3d7e7325a1e5f05c0d070de1d3ac975cadf820aa [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_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
Laurent Pinchartf20ed392015-01-20 18:30:04 +020027#include "io-pgtable.h"
28
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020029struct ipmmu_vmsa_device {
30 struct device *dev;
31 void __iomem *base;
32 struct list_head list;
33
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020034 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
Laurent Pinchartf20ed392015-01-20 18:30:04 +020043 struct io_pgtable_cfg cfg;
44 struct io_pgtable_ops *iop;
45
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020046 unsigned int context_id;
47 spinlock_t lock; /* Protects mappings */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020048};
49
Laurent Pinchart192d2042014-05-15 12:40:42 +020050struct ipmmu_vmsa_archdata {
51 struct ipmmu_vmsa_device *mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +020052 unsigned int *utlbs;
53 unsigned int num_utlbs;
Laurent Pinchart192d2042014-05-15 12:40:42 +020054};
55
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020056static DEFINE_SPINLOCK(ipmmu_devices_lock);
57static LIST_HEAD(ipmmu_devices);
58
59#define TLB_LOOP_TIMEOUT 100 /* 100us */
60
61/* -----------------------------------------------------------------------------
62 * Registers Definition
63 */
64
Laurent Pinchart275f5052014-03-17 01:02:46 +010065#define IM_NS_ALIAS_OFFSET 0x800
66
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020067#define IM_CTX_SIZE 0x40
68
69#define IMCTR 0x0000
70#define IMCTR_TRE (1 << 17)
71#define IMCTR_AFE (1 << 16)
72#define IMCTR_RTSEL_MASK (3 << 4)
73#define IMCTR_RTSEL_SHIFT 4
74#define IMCTR_TREN (1 << 3)
75#define IMCTR_INTEN (1 << 2)
76#define IMCTR_FLUSH (1 << 1)
77#define IMCTR_MMUEN (1 << 0)
78
79#define IMCAAR 0x0004
80
81#define IMTTBCR 0x0008
82#define IMTTBCR_EAE (1 << 31)
83#define IMTTBCR_PMB (1 << 30)
84#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
85#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
86#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
87#define IMTTBCR_SH1_MASK (3 << 28)
88#define IMTTBCR_ORGN1_NC (0 << 26)
89#define IMTTBCR_ORGN1_WB_WA (1 << 26)
90#define IMTTBCR_ORGN1_WT (2 << 26)
91#define IMTTBCR_ORGN1_WB (3 << 26)
92#define IMTTBCR_ORGN1_MASK (3 << 26)
93#define IMTTBCR_IRGN1_NC (0 << 24)
94#define IMTTBCR_IRGN1_WB_WA (1 << 24)
95#define IMTTBCR_IRGN1_WT (2 << 24)
96#define IMTTBCR_IRGN1_WB (3 << 24)
97#define IMTTBCR_IRGN1_MASK (3 << 24)
98#define IMTTBCR_TSZ1_MASK (7 << 16)
99#define IMTTBCR_TSZ1_SHIFT 16
100#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
101#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
102#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
103#define IMTTBCR_SH0_MASK (3 << 12)
104#define IMTTBCR_ORGN0_NC (0 << 10)
105#define IMTTBCR_ORGN0_WB_WA (1 << 10)
106#define IMTTBCR_ORGN0_WT (2 << 10)
107#define IMTTBCR_ORGN0_WB (3 << 10)
108#define IMTTBCR_ORGN0_MASK (3 << 10)
109#define IMTTBCR_IRGN0_NC (0 << 8)
110#define IMTTBCR_IRGN0_WB_WA (1 << 8)
111#define IMTTBCR_IRGN0_WT (2 << 8)
112#define IMTTBCR_IRGN0_WB (3 << 8)
113#define IMTTBCR_IRGN0_MASK (3 << 8)
114#define IMTTBCR_SL0_LVL_2 (0 << 4)
115#define IMTTBCR_SL0_LVL_1 (1 << 4)
116#define IMTTBCR_TSZ0_MASK (7 << 0)
117#define IMTTBCR_TSZ0_SHIFT O
118
119#define IMBUSCR 0x000c
120#define IMBUSCR_DVM (1 << 2)
121#define IMBUSCR_BUSSEL_SYS (0 << 0)
122#define IMBUSCR_BUSSEL_CCI (1 << 0)
123#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
124#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
125#define IMBUSCR_BUSSEL_MASK (3 << 0)
126
127#define IMTTLBR0 0x0010
128#define IMTTUBR0 0x0014
129#define IMTTLBR1 0x0018
130#define IMTTUBR1 0x001c
131
132#define IMSTR 0x0020
133#define IMSTR_ERRLVL_MASK (3 << 12)
134#define IMSTR_ERRLVL_SHIFT 12
135#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
136#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
137#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
138#define IMSTR_ERRCODE_MASK (7 << 8)
139#define IMSTR_MHIT (1 << 4)
140#define IMSTR_ABORT (1 << 2)
141#define IMSTR_PF (1 << 1)
142#define IMSTR_TF (1 << 0)
143
144#define IMMAIR0 0x0028
145#define IMMAIR1 0x002c
146#define IMMAIR_ATTR_MASK 0xff
147#define IMMAIR_ATTR_DEVICE 0x04
148#define IMMAIR_ATTR_NC 0x44
149#define IMMAIR_ATTR_WBRWA 0xff
150#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
151#define IMMAIR_ATTR_IDX_NC 0
152#define IMMAIR_ATTR_IDX_WBRWA 1
153#define IMMAIR_ATTR_IDX_DEV 2
154
155#define IMEAR 0x0030
156
157#define IMPCTR 0x0200
158#define IMPSTR 0x0208
159#define IMPEAR 0x020c
160#define IMPMBA(n) (0x0280 + ((n) * 4))
161#define IMPMBD(n) (0x02c0 + ((n) * 4))
162
163#define IMUCTR(n) (0x0300 + ((n) * 16))
164#define IMUCTR_FIXADDEN (1 << 31)
165#define IMUCTR_FIXADD_MASK (0xff << 16)
166#define IMUCTR_FIXADD_SHIFT 16
167#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
168#define IMUCTR_TTSEL_PMB (8 << 4)
169#define IMUCTR_TTSEL_MASK (15 << 4)
170#define IMUCTR_FLUSH (1 << 1)
171#define IMUCTR_MMUEN (1 << 0)
172
173#define IMUASID(n) (0x0308 + ((n) * 16))
174#define IMUASID_ASID8_MASK (0xff << 8)
175#define IMUASID_ASID8_SHIFT 8
176#define IMUASID_ASID0_MASK (0xff << 0)
177#define IMUASID_ASID0_SHIFT 0
178
179/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200180 * Read/Write Access
181 */
182
183static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
184{
185 return ioread32(mmu->base + offset);
186}
187
188static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
189 u32 data)
190{
191 iowrite32(data, mmu->base + offset);
192}
193
194static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
195{
196 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
197}
198
199static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
200 u32 data)
201{
202 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
203}
204
205/* -----------------------------------------------------------------------------
206 * TLB and microTLB Management
207 */
208
209/* Wait for any pending TLB invalidations to complete */
210static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
211{
212 unsigned int count = 0;
213
214 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
215 cpu_relax();
216 if (++count == TLB_LOOP_TIMEOUT) {
217 dev_err_ratelimited(domain->mmu->dev,
218 "TLB sync timed out -- MMU may be deadlocked\n");
219 return;
220 }
221 udelay(1);
222 }
223}
224
225static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
226{
227 u32 reg;
228
229 reg = ipmmu_ctx_read(domain, IMCTR);
230 reg |= IMCTR_FLUSH;
231 ipmmu_ctx_write(domain, IMCTR, reg);
232
233 ipmmu_tlb_sync(domain);
234}
235
236/*
237 * Enable MMU translation for the microTLB.
238 */
239static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200240 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200241{
242 struct ipmmu_vmsa_device *mmu = domain->mmu;
243
Laurent Pinchart192d2042014-05-15 12:40:42 +0200244 /*
245 * TODO: Reference-count the microTLB as several bus masters can be
246 * connected to the same microTLB.
247 */
248
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200249 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200250 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200251 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200252 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200253 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
254 IMUCTR_MMUEN);
255}
256
257/*
258 * Disable MMU translation for the microTLB.
259 */
260static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200261 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200262{
263 struct ipmmu_vmsa_device *mmu = domain->mmu;
264
Laurent Pinchart192d2042014-05-15 12:40:42 +0200265 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200266}
267
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200268static void ipmmu_tlb_flush_all(void *cookie)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200269{
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200270 struct ipmmu_vmsa_domain *domain = cookie;
271
272 ipmmu_tlb_invalidate(domain);
273}
274
275static void ipmmu_tlb_add_flush(unsigned long iova, size_t size, bool leaf,
276 void *cookie)
277{
278 /* The hardware doesn't support selective TLB flush. */
279}
280
281static void ipmmu_flush_pgtable(void *ptr, size_t size, void *cookie)
282{
283 unsigned long offset = (unsigned long)ptr & ~PAGE_MASK;
284 struct ipmmu_vmsa_domain *domain = cookie;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200285
286 /*
287 * TODO: Add support for coherent walk through CCI with DVM and remove
288 * cache handling.
289 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200290 dma_map_page(domain->mmu->dev, virt_to_page(ptr), offset, size,
291 DMA_TO_DEVICE);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200292}
293
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200294static struct iommu_gather_ops ipmmu_gather_ops = {
295 .tlb_flush_all = ipmmu_tlb_flush_all,
296 .tlb_add_flush = ipmmu_tlb_add_flush,
297 .tlb_sync = ipmmu_tlb_flush_all,
298 .flush_pgtable = ipmmu_flush_pgtable,
299};
300
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200301/* -----------------------------------------------------------------------------
302 * Domain/Context Management
303 */
304
305static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
306{
307 phys_addr_t ttbr;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200308
309 /*
310 * Allocate the page table operations.
311 *
312 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
313 * access, Long-descriptor format" that the NStable bit being set in a
314 * table descriptor will result in the NStable and NS bits of all child
315 * entries being ignored and considered as being set. The IPMMU seems
316 * not to comply with this, as it generates a secure access page fault
317 * if any of the NStable and NS bits isn't set when running in
318 * non-secure mode.
319 */
320 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
321 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
322 domain->cfg.ias = 32;
323 domain->cfg.oas = 40;
324 domain->cfg.tlb = &ipmmu_gather_ops;
325
326 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
327 domain);
328 if (!domain->iop)
329 return -EINVAL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200330
331 /*
332 * TODO: When adding support for multiple contexts, find an unused
333 * context.
334 */
335 domain->context_id = 0;
336
337 /* TTBR0 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200338 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200339 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
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200351 /* MAIR0 */
352 ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200353
354 /* IMBUSCR */
355 ipmmu_ctx_write(domain, IMBUSCR,
356 ipmmu_ctx_read(domain, IMBUSCR) &
357 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
358
359 /*
360 * IMSTR
361 * Clear all interrupt flags.
362 */
363 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
364
365 /*
366 * IMCTR
367 * Enable the MMU and interrupt generation. The long-descriptor
368 * translation table format doesn't use TEX remapping. Don't enable AF
369 * software management as we have no use for it. Flush the TLB as
370 * required when modifying the context registers.
371 */
372 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
373
374 return 0;
375}
376
377static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
378{
379 /*
380 * Disable the context. Flush the TLB as required when modifying the
381 * context registers.
382 *
383 * TODO: Is TLB flush really needed ?
384 */
385 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
386 ipmmu_tlb_sync(domain);
387}
388
389/* -----------------------------------------------------------------------------
390 * Fault Handling
391 */
392
393static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
394{
395 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
396 struct ipmmu_vmsa_device *mmu = domain->mmu;
397 u32 status;
398 u32 iova;
399
400 status = ipmmu_ctx_read(domain, IMSTR);
401 if (!(status & err_mask))
402 return IRQ_NONE;
403
404 iova = ipmmu_ctx_read(domain, IMEAR);
405
406 /*
407 * Clear the error status flags. Unlike traditional interrupt flag
408 * registers that must be cleared by writing 1, this status register
409 * seems to require 0. The error address register must be read before,
410 * otherwise its value will be 0.
411 */
412 ipmmu_ctx_write(domain, IMSTR, 0);
413
414 /* Log fatal errors. */
415 if (status & IMSTR_MHIT)
416 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
417 iova);
418 if (status & IMSTR_ABORT)
419 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
420 iova);
421
422 if (!(status & (IMSTR_PF | IMSTR_TF)))
423 return IRQ_NONE;
424
425 /*
426 * Try to handle page faults and translation faults.
427 *
428 * TODO: We need to look up the faulty device based on the I/O VA. Use
429 * the IOMMU device for now.
430 */
431 if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
432 return IRQ_HANDLED;
433
434 dev_err_ratelimited(mmu->dev,
435 "Unhandled fault: status 0x%08x iova 0x%08x\n",
436 status, iova);
437
438 return IRQ_HANDLED;
439}
440
441static irqreturn_t ipmmu_irq(int irq, void *dev)
442{
443 struct ipmmu_vmsa_device *mmu = dev;
444 struct iommu_domain *io_domain;
445 struct ipmmu_vmsa_domain *domain;
446
447 if (!mmu->mapping)
448 return IRQ_NONE;
449
450 io_domain = mmu->mapping->domain;
451 domain = io_domain->priv;
452
453 return ipmmu_domain_irq(domain);
454}
455
456/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200457 * IOMMU Operations
458 */
459
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200460static int ipmmu_domain_init(struct iommu_domain *io_domain)
461{
462 struct ipmmu_vmsa_domain *domain;
463
464 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
465 if (!domain)
466 return -ENOMEM;
467
468 spin_lock_init(&domain->lock);
469
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200470 io_domain->priv = domain;
471 domain->io_domain = io_domain;
472
473 return 0;
474}
475
476static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
477{
478 struct ipmmu_vmsa_domain *domain = io_domain->priv;
479
480 /*
481 * Free the domain resources. We assume that all devices have already
482 * been detached.
483 */
484 ipmmu_domain_destroy_context(domain);
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200485 free_io_pgtable_ops(domain->iop);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200486 kfree(domain);
487}
488
489static int ipmmu_attach_device(struct iommu_domain *io_domain,
490 struct device *dev)
491{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200492 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
493 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200494 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200495 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200496 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200497 int ret = 0;
498
499 if (!mmu) {
500 dev_err(dev, "Cannot attach to IPMMU\n");
501 return -ENXIO;
502 }
503
504 spin_lock_irqsave(&domain->lock, flags);
505
506 if (!domain->mmu) {
507 /* The domain hasn't been used yet, initialize it. */
508 domain->mmu = mmu;
509 ret = ipmmu_domain_init_context(domain);
510 } else if (domain->mmu != mmu) {
511 /*
512 * Something is wrong, we can't attach two devices using
513 * different IOMMUs to the same domain.
514 */
515 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
516 dev_name(mmu->dev), dev_name(domain->mmu->dev));
517 ret = -EINVAL;
518 }
519
520 spin_unlock_irqrestore(&domain->lock, flags);
521
522 if (ret < 0)
523 return ret;
524
Laurent Pincharta166d312014-07-24 01:36:43 +0200525 for (i = 0; i < archdata->num_utlbs; ++i)
526 ipmmu_utlb_enable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200527
528 return 0;
529}
530
531static void ipmmu_detach_device(struct iommu_domain *io_domain,
532 struct device *dev)
533{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200534 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200535 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pincharta166d312014-07-24 01:36:43 +0200536 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200537
Laurent Pincharta166d312014-07-24 01:36:43 +0200538 for (i = 0; i < archdata->num_utlbs; ++i)
539 ipmmu_utlb_disable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200540
541 /*
542 * TODO: Optimize by disabling the context when no device is attached.
543 */
544}
545
546static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
547 phys_addr_t paddr, size_t size, int prot)
548{
549 struct ipmmu_vmsa_domain *domain = io_domain->priv;
550
551 if (!domain)
552 return -ENODEV;
553
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200554 return domain->iop->map(domain->iop, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200555}
556
557static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
558 size_t size)
559{
560 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200561
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200562 return domain->iop->unmap(domain->iop, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200563}
564
565static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
566 dma_addr_t iova)
567{
568 struct ipmmu_vmsa_domain *domain = io_domain->priv;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200569
570 /* TODO: Is locking needed ? */
571
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200572 return domain->iop->iova_to_phys(domain->iop, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200573}
574
Laurent Pincharta166d312014-07-24 01:36:43 +0200575static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev,
576 unsigned int **_utlbs)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200577{
Laurent Pincharta166d312014-07-24 01:36:43 +0200578 unsigned int *utlbs;
579 unsigned int i;
580 int count;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200581
Laurent Pincharta166d312014-07-24 01:36:43 +0200582 count = of_count_phandle_with_args(dev->of_node, "iommus",
583 "#iommu-cells");
584 if (count < 0)
585 return -EINVAL;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100586
Laurent Pincharta166d312014-07-24 01:36:43 +0200587 utlbs = kcalloc(count, sizeof(*utlbs), GFP_KERNEL);
588 if (!utlbs)
589 return -ENOMEM;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100590
Laurent Pincharta166d312014-07-24 01:36:43 +0200591 for (i = 0; i < count; ++i) {
592 struct of_phandle_args args;
593 int ret;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100594
Laurent Pincharta166d312014-07-24 01:36:43 +0200595 ret = of_parse_phandle_with_args(dev->of_node, "iommus",
596 "#iommu-cells", i, &args);
597 if (ret < 0)
598 goto error;
599
600 of_node_put(args.np);
601
602 if (args.np != mmu->dev->of_node || args.args_count != 1)
603 goto error;
604
605 utlbs[i] = args.args[0];
606 }
607
608 *_utlbs = utlbs;
609
610 return count;
611
612error:
613 kfree(utlbs);
614 return -EINVAL;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200615}
616
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200617static int ipmmu_add_device(struct device *dev)
618{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200619 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200620 struct ipmmu_vmsa_device *mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +0200621 struct iommu_group *group = NULL;
622 unsigned int *utlbs = NULL;
623 unsigned int i;
624 int num_utlbs = 0;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200625 int ret;
626
627 if (dev->archdata.iommu) {
628 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
629 dev_name(dev));
630 return -EINVAL;
631 }
632
633 /* Find the master corresponding to the device. */
634 spin_lock(&ipmmu_devices_lock);
635
636 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pincharta166d312014-07-24 01:36:43 +0200637 num_utlbs = ipmmu_find_utlbs(mmu, dev, &utlbs);
638 if (num_utlbs) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200639 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +0200640 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200641 * against device removal.
642 */
643 break;
644 }
645 }
646
647 spin_unlock(&ipmmu_devices_lock);
648
Laurent Pincharta166d312014-07-24 01:36:43 +0200649 if (num_utlbs <= 0)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200650 return -ENODEV;
651
Laurent Pincharta166d312014-07-24 01:36:43 +0200652 for (i = 0; i < num_utlbs; ++i) {
653 if (utlbs[i] >= mmu->num_utlbs) {
654 ret = -EINVAL;
655 goto error;
656 }
657 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200658
659 /* Create a device group and add the device to it. */
660 group = iommu_group_alloc();
661 if (IS_ERR(group)) {
662 dev_err(dev, "Failed to allocate IOMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200663 ret = PTR_ERR(group);
664 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200665 }
666
667 ret = iommu_group_add_device(group, dev);
668 iommu_group_put(group);
669
670 if (ret < 0) {
671 dev_err(dev, "Failed to add device to IPMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200672 group = NULL;
673 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200674 }
675
Laurent Pinchart192d2042014-05-15 12:40:42 +0200676 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
677 if (!archdata) {
678 ret = -ENOMEM;
679 goto error;
680 }
681
682 archdata->mmu = mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +0200683 archdata->utlbs = utlbs;
684 archdata->num_utlbs = num_utlbs;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200685 dev->archdata.iommu = archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200686
687 /*
688 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
689 * VAs. This will allocate a corresponding IOMMU domain.
690 *
691 * TODO:
692 * - Create one mapping per context (TLB).
693 * - Make the mapping size configurable ? We currently use a 2GB mapping
694 * at a 1GB offset to ensure that NULL VAs will fault.
695 */
696 if (!mmu->mapping) {
697 struct dma_iommu_mapping *mapping;
698
699 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +0200700 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200701 if (IS_ERR(mapping)) {
702 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +0100703 ret = PTR_ERR(mapping);
704 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200705 }
706
707 mmu->mapping = mapping;
708 }
709
710 /* Attach the ARM VA mapping to the device. */
711 ret = arm_iommu_attach_device(dev, mmu->mapping);
712 if (ret < 0) {
713 dev_err(dev, "Failed to attach device to VA mapping\n");
714 goto error;
715 }
716
717 return 0;
718
719error:
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +0100720 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +0200721
Laurent Pinchart192d2042014-05-15 12:40:42 +0200722 kfree(dev->archdata.iommu);
Laurent Pincharta166d312014-07-24 01:36:43 +0200723 kfree(utlbs);
724
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200725 dev->archdata.iommu = NULL;
Laurent Pincharta166d312014-07-24 01:36:43 +0200726
727 if (!IS_ERR_OR_NULL(group))
728 iommu_group_remove_device(dev);
729
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200730 return ret;
731}
732
733static void ipmmu_remove_device(struct device *dev)
734{
Laurent Pincharta166d312014-07-24 01:36:43 +0200735 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
736
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200737 arm_iommu_detach_device(dev);
738 iommu_group_remove_device(dev);
Laurent Pincharta166d312014-07-24 01:36:43 +0200739
740 kfree(archdata->utlbs);
741 kfree(archdata);
742
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200743 dev->archdata.iommu = NULL;
744}
745
Thierry Redingb22f6432014-06-27 09:03:12 +0200746static const struct iommu_ops ipmmu_ops = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200747 .domain_init = ipmmu_domain_init,
748 .domain_destroy = ipmmu_domain_destroy,
749 .attach_dev = ipmmu_attach_device,
750 .detach_dev = ipmmu_detach_device,
751 .map = ipmmu_map,
752 .unmap = ipmmu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -0700753 .map_sg = default_iommu_map_sg,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200754 .iova_to_phys = ipmmu_iova_to_phys,
755 .add_device = ipmmu_add_device,
756 .remove_device = ipmmu_remove_device,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200757 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200758};
759
760/* -----------------------------------------------------------------------------
761 * Probe/remove and init
762 */
763
764static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
765{
766 unsigned int i;
767
768 /* Disable all contexts. */
769 for (i = 0; i < 4; ++i)
770 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
771}
772
773static int ipmmu_probe(struct platform_device *pdev)
774{
775 struct ipmmu_vmsa_device *mmu;
776 struct resource *res;
777 int irq;
778 int ret;
779
Laurent Pinchart275f5052014-03-17 01:02:46 +0100780 if (!IS_ENABLED(CONFIG_OF) && !pdev->dev.platform_data) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200781 dev_err(&pdev->dev, "missing platform data\n");
782 return -EINVAL;
783 }
784
785 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
786 if (!mmu) {
787 dev_err(&pdev->dev, "cannot allocate device data\n");
788 return -ENOMEM;
789 }
790
791 mmu->dev = &pdev->dev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200792 mmu->num_utlbs = 32;
793
794 /* Map I/O memory and request IRQ. */
795 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
796 mmu->base = devm_ioremap_resource(&pdev->dev, res);
797 if (IS_ERR(mmu->base))
798 return PTR_ERR(mmu->base);
799
Laurent Pinchart275f5052014-03-17 01:02:46 +0100800 /*
801 * The IPMMU has two register banks, for secure and non-secure modes.
802 * The bank mapped at the beginning of the IPMMU address space
803 * corresponds to the running mode of the CPU. When running in secure
804 * mode the non-secure register bank is also available at an offset.
805 *
806 * Secure mode operation isn't clearly documented and is thus currently
807 * not implemented in the driver. Furthermore, preliminary tests of
808 * non-secure operation with the main register bank were not successful.
809 * Offset the registers base unconditionally to point to the non-secure
810 * alias space for now.
811 */
812 mmu->base += IM_NS_ALIAS_OFFSET;
813
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200814 irq = platform_get_irq(pdev, 0);
815 if (irq < 0) {
816 dev_err(&pdev->dev, "no IRQ found\n");
817 return irq;
818 }
819
820 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
821 dev_name(&pdev->dev), mmu);
822 if (ret < 0) {
823 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +0800824 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200825 }
826
827 ipmmu_device_reset(mmu);
828
829 /*
830 * We can't create the ARM mapping here as it requires the bus to have
831 * an IOMMU, which only happens when bus_set_iommu() is called in
832 * ipmmu_init() after the probe function returns.
833 */
834
835 spin_lock(&ipmmu_devices_lock);
836 list_add(&mmu->list, &ipmmu_devices);
837 spin_unlock(&ipmmu_devices_lock);
838
839 platform_set_drvdata(pdev, mmu);
840
841 return 0;
842}
843
844static int ipmmu_remove(struct platform_device *pdev)
845{
846 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
847
848 spin_lock(&ipmmu_devices_lock);
849 list_del(&mmu->list);
850 spin_unlock(&ipmmu_devices_lock);
851
852 arm_iommu_release_mapping(mmu->mapping);
853
854 ipmmu_device_reset(mmu);
855
856 return 0;
857}
858
Laurent Pinchart275f5052014-03-17 01:02:46 +0100859static const struct of_device_id ipmmu_of_ids[] = {
860 { .compatible = "renesas,ipmmu-vmsa", },
861};
862
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200863static struct platform_driver ipmmu_driver = {
864 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200865 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +0100866 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200867 },
868 .probe = ipmmu_probe,
869 .remove = ipmmu_remove,
870};
871
872static int __init ipmmu_init(void)
873{
874 int ret;
875
876 ret = platform_driver_register(&ipmmu_driver);
877 if (ret < 0)
878 return ret;
879
880 if (!iommu_present(&platform_bus_type))
881 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
882
883 return 0;
884}
885
886static void __exit ipmmu_exit(void)
887{
888 return platform_driver_unregister(&ipmmu_driver);
889}
890
891subsys_initcall(ipmmu_init);
892module_exit(ipmmu_exit);
893
894MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
895MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
896MODULE_LICENSE("GPL v2");