blob: eb5008596051cd27042cd8dbd05269c7459fc28d [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
Magnus Dammdbb70692017-05-17 19:06:38 +090011#include <linux/bitmap.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020012#include <linux/delay.h>
13#include <linux/dma-mapping.h>
14#include <linux/err.h>
15#include <linux/export.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/iommu.h>
19#include <linux/module.h>
Laurent Pinchart275f5052014-03-17 01:02:46 +010020#include <linux/of.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020021#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
Laurent Pinchartf20ed392015-01-20 18:30:04 +020028#include "io-pgtable.h"
29
Magnus Dammdbb70692017-05-17 19:06:38 +090030#define IPMMU_CTX_MAX 1
31
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020032struct ipmmu_vmsa_device {
33 struct device *dev;
34 void __iomem *base;
35 struct list_head list;
36
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020037 unsigned int num_utlbs;
Magnus Dammdbb70692017-05-17 19:06:38 +090038 spinlock_t lock; /* Protects ctx and domains[] */
39 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
40 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020041
42 struct dma_iommu_mapping *mapping;
43};
44
45struct ipmmu_vmsa_domain {
46 struct ipmmu_vmsa_device *mmu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +010047 struct iommu_domain io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020048
Laurent Pinchartf20ed392015-01-20 18:30:04 +020049 struct io_pgtable_cfg cfg;
50 struct io_pgtable_ops *iop;
51
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020052 unsigned int context_id;
53 spinlock_t lock; /* Protects mappings */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020054};
55
Laurent Pinchart192d2042014-05-15 12:40:42 +020056struct ipmmu_vmsa_archdata {
57 struct ipmmu_vmsa_device *mmu;
Laurent Pincharta166d312014-07-24 01:36:43 +020058 unsigned int *utlbs;
59 unsigned int num_utlbs;
Laurent Pinchart192d2042014-05-15 12:40:42 +020060};
61
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020062static DEFINE_SPINLOCK(ipmmu_devices_lock);
63static LIST_HEAD(ipmmu_devices);
64
Joerg Roedel5914c5f2015-03-26 13:43:16 +010065static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
66{
67 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
68}
69
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020070#define TLB_LOOP_TIMEOUT 100 /* 100us */
71
72/* -----------------------------------------------------------------------------
73 * Registers Definition
74 */
75
Laurent Pinchart275f5052014-03-17 01:02:46 +010076#define IM_NS_ALIAS_OFFSET 0x800
77
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020078#define IM_CTX_SIZE 0x40
79
80#define IMCTR 0x0000
81#define IMCTR_TRE (1 << 17)
82#define IMCTR_AFE (1 << 16)
83#define IMCTR_RTSEL_MASK (3 << 4)
84#define IMCTR_RTSEL_SHIFT 4
85#define IMCTR_TREN (1 << 3)
86#define IMCTR_INTEN (1 << 2)
87#define IMCTR_FLUSH (1 << 1)
88#define IMCTR_MMUEN (1 << 0)
89
90#define IMCAAR 0x0004
91
92#define IMTTBCR 0x0008
93#define IMTTBCR_EAE (1 << 31)
94#define IMTTBCR_PMB (1 << 30)
95#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
96#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
97#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
98#define IMTTBCR_SH1_MASK (3 << 28)
99#define IMTTBCR_ORGN1_NC (0 << 26)
100#define IMTTBCR_ORGN1_WB_WA (1 << 26)
101#define IMTTBCR_ORGN1_WT (2 << 26)
102#define IMTTBCR_ORGN1_WB (3 << 26)
103#define IMTTBCR_ORGN1_MASK (3 << 26)
104#define IMTTBCR_IRGN1_NC (0 << 24)
105#define IMTTBCR_IRGN1_WB_WA (1 << 24)
106#define IMTTBCR_IRGN1_WT (2 << 24)
107#define IMTTBCR_IRGN1_WB (3 << 24)
108#define IMTTBCR_IRGN1_MASK (3 << 24)
109#define IMTTBCR_TSZ1_MASK (7 << 16)
110#define IMTTBCR_TSZ1_SHIFT 16
111#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
112#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
113#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
114#define IMTTBCR_SH0_MASK (3 << 12)
115#define IMTTBCR_ORGN0_NC (0 << 10)
116#define IMTTBCR_ORGN0_WB_WA (1 << 10)
117#define IMTTBCR_ORGN0_WT (2 << 10)
118#define IMTTBCR_ORGN0_WB (3 << 10)
119#define IMTTBCR_ORGN0_MASK (3 << 10)
120#define IMTTBCR_IRGN0_NC (0 << 8)
121#define IMTTBCR_IRGN0_WB_WA (1 << 8)
122#define IMTTBCR_IRGN0_WT (2 << 8)
123#define IMTTBCR_IRGN0_WB (3 << 8)
124#define IMTTBCR_IRGN0_MASK (3 << 8)
125#define IMTTBCR_SL0_LVL_2 (0 << 4)
126#define IMTTBCR_SL0_LVL_1 (1 << 4)
127#define IMTTBCR_TSZ0_MASK (7 << 0)
128#define IMTTBCR_TSZ0_SHIFT O
129
130#define IMBUSCR 0x000c
131#define IMBUSCR_DVM (1 << 2)
132#define IMBUSCR_BUSSEL_SYS (0 << 0)
133#define IMBUSCR_BUSSEL_CCI (1 << 0)
134#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
135#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
136#define IMBUSCR_BUSSEL_MASK (3 << 0)
137
138#define IMTTLBR0 0x0010
139#define IMTTUBR0 0x0014
140#define IMTTLBR1 0x0018
141#define IMTTUBR1 0x001c
142
143#define IMSTR 0x0020
144#define IMSTR_ERRLVL_MASK (3 << 12)
145#define IMSTR_ERRLVL_SHIFT 12
146#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
147#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
148#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
149#define IMSTR_ERRCODE_MASK (7 << 8)
150#define IMSTR_MHIT (1 << 4)
151#define IMSTR_ABORT (1 << 2)
152#define IMSTR_PF (1 << 1)
153#define IMSTR_TF (1 << 0)
154
155#define IMMAIR0 0x0028
156#define IMMAIR1 0x002c
157#define IMMAIR_ATTR_MASK 0xff
158#define IMMAIR_ATTR_DEVICE 0x04
159#define IMMAIR_ATTR_NC 0x44
160#define IMMAIR_ATTR_WBRWA 0xff
161#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
162#define IMMAIR_ATTR_IDX_NC 0
163#define IMMAIR_ATTR_IDX_WBRWA 1
164#define IMMAIR_ATTR_IDX_DEV 2
165
166#define IMEAR 0x0030
167
168#define IMPCTR 0x0200
169#define IMPSTR 0x0208
170#define IMPEAR 0x020c
171#define IMPMBA(n) (0x0280 + ((n) * 4))
172#define IMPMBD(n) (0x02c0 + ((n) * 4))
173
174#define IMUCTR(n) (0x0300 + ((n) * 16))
175#define IMUCTR_FIXADDEN (1 << 31)
176#define IMUCTR_FIXADD_MASK (0xff << 16)
177#define IMUCTR_FIXADD_SHIFT 16
178#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
179#define IMUCTR_TTSEL_PMB (8 << 4)
180#define IMUCTR_TTSEL_MASK (15 << 4)
181#define IMUCTR_FLUSH (1 << 1)
182#define IMUCTR_MMUEN (1 << 0)
183
184#define IMUASID(n) (0x0308 + ((n) * 16))
185#define IMUASID_ASID8_MASK (0xff << 8)
186#define IMUASID_ASID8_SHIFT 8
187#define IMUASID_ASID0_MASK (0xff << 0)
188#define IMUASID_ASID0_SHIFT 0
189
190/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200191 * Read/Write Access
192 */
193
194static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
195{
196 return ioread32(mmu->base + offset);
197}
198
199static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
200 u32 data)
201{
202 iowrite32(data, mmu->base + offset);
203}
204
205static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
206{
207 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
208}
209
210static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
211 u32 data)
212{
213 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
214}
215
216/* -----------------------------------------------------------------------------
217 * TLB and microTLB Management
218 */
219
220/* Wait for any pending TLB invalidations to complete */
221static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
222{
223 unsigned int count = 0;
224
225 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
226 cpu_relax();
227 if (++count == TLB_LOOP_TIMEOUT) {
228 dev_err_ratelimited(domain->mmu->dev,
229 "TLB sync timed out -- MMU may be deadlocked\n");
230 return;
231 }
232 udelay(1);
233 }
234}
235
236static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
237{
238 u32 reg;
239
240 reg = ipmmu_ctx_read(domain, IMCTR);
241 reg |= IMCTR_FLUSH;
242 ipmmu_ctx_write(domain, IMCTR, reg);
243
244 ipmmu_tlb_sync(domain);
245}
246
247/*
248 * Enable MMU translation for the microTLB.
249 */
250static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200251 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200252{
253 struct ipmmu_vmsa_device *mmu = domain->mmu;
254
Laurent Pinchart192d2042014-05-15 12:40:42 +0200255 /*
256 * TODO: Reference-count the microTLB as several bus masters can be
257 * connected to the same microTLB.
258 */
259
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200260 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200261 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200262 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200263 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200264 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
265 IMUCTR_MMUEN);
266}
267
268/*
269 * Disable MMU translation for the microTLB.
270 */
271static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200272 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200273{
274 struct ipmmu_vmsa_device *mmu = domain->mmu;
275
Laurent Pinchart192d2042014-05-15 12:40:42 +0200276 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200277}
278
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200279static void ipmmu_tlb_flush_all(void *cookie)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200280{
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200281 struct ipmmu_vmsa_domain *domain = cookie;
282
283 ipmmu_tlb_invalidate(domain);
284}
285
Robin Murphy06c610e2015-12-07 18:18:53 +0000286static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
287 size_t granule, bool leaf, void *cookie)
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200288{
289 /* The hardware doesn't support selective TLB flush. */
290}
291
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200292static struct iommu_gather_ops ipmmu_gather_ops = {
293 .tlb_flush_all = ipmmu_tlb_flush_all,
294 .tlb_add_flush = ipmmu_tlb_add_flush,
295 .tlb_sync = ipmmu_tlb_flush_all,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200296};
297
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200298/* -----------------------------------------------------------------------------
299 * Domain/Context Management
300 */
301
Magnus Dammdbb70692017-05-17 19:06:38 +0900302static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
303 struct ipmmu_vmsa_domain *domain)
304{
305 unsigned long flags;
306 int ret;
307
308 spin_lock_irqsave(&mmu->lock, flags);
309
310 ret = find_first_zero_bit(mmu->ctx, IPMMU_CTX_MAX);
311 if (ret != IPMMU_CTX_MAX) {
312 mmu->domains[ret] = domain;
313 set_bit(ret, mmu->ctx);
314 }
315
316 spin_unlock_irqrestore(&mmu->lock, flags);
317
318 return ret;
319}
320
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200321static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
322{
Geert Uytterhoevenf64232e2015-12-22 20:01:06 +0100323 u64 ttbr;
Magnus Dammdbb70692017-05-17 19:06:38 +0900324 int ret;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200325
326 /*
327 * Allocate the page table operations.
328 *
329 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
330 * access, Long-descriptor format" that the NStable bit being set in a
331 * table descriptor will result in the NStable and NS bits of all child
332 * entries being ignored and considered as being set. The IPMMU seems
333 * not to comply with this, as it generates a secure access page fault
334 * if any of the NStable and NS bits isn't set when running in
335 * non-secure mode.
336 */
337 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
338 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
339 domain->cfg.ias = 32;
340 domain->cfg.oas = 40;
341 domain->cfg.tlb = &ipmmu_gather_ops;
Geert Uytterhoeven3b6bb5b2017-01-31 12:17:07 +0100342 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
343 domain->io_domain.geometry.force_aperture = true;
Robin Murphyff2ed962015-07-29 19:46:08 +0100344 /*
345 * TODO: Add support for coherent walk through CCI with DVM and remove
346 * cache handling. For now, delegate it to the io-pgtable code.
347 */
348 domain->cfg.iommu_dev = domain->mmu->dev;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200349
350 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
351 domain);
352 if (!domain->iop)
353 return -EINVAL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200354
355 /*
Magnus Dammdbb70692017-05-17 19:06:38 +0900356 * Find an unused context.
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200357 */
Magnus Dammdbb70692017-05-17 19:06:38 +0900358 ret = ipmmu_domain_allocate_context(domain->mmu, domain);
359 if (ret == IPMMU_CTX_MAX) {
360 free_io_pgtable_ops(domain->iop);
361 return -EBUSY;
362 }
363
364 domain->context_id = ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200365
366 /* TTBR0 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200367 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200368 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
369 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
370
371 /*
372 * TTBCR
373 * We use long descriptors with inner-shareable WBWA tables and allocate
374 * the whole 32-bit VA space to TTBR0.
375 */
376 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
377 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
378 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
379
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200380 /* MAIR0 */
381 ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200382
383 /* IMBUSCR */
384 ipmmu_ctx_write(domain, IMBUSCR,
385 ipmmu_ctx_read(domain, IMBUSCR) &
386 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
387
388 /*
389 * IMSTR
390 * Clear all interrupt flags.
391 */
392 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
393
394 /*
395 * IMCTR
396 * Enable the MMU and interrupt generation. The long-descriptor
397 * translation table format doesn't use TEX remapping. Don't enable AF
398 * software management as we have no use for it. Flush the TLB as
399 * required when modifying the context registers.
400 */
401 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
402
403 return 0;
404}
405
Magnus Dammdbb70692017-05-17 19:06:38 +0900406static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
407 unsigned int context_id)
408{
409 unsigned long flags;
410
411 spin_lock_irqsave(&mmu->lock, flags);
412
413 clear_bit(context_id, mmu->ctx);
414 mmu->domains[context_id] = NULL;
415
416 spin_unlock_irqrestore(&mmu->lock, flags);
417}
418
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200419static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
420{
421 /*
422 * Disable the context. Flush the TLB as required when modifying the
423 * context registers.
424 *
425 * TODO: Is TLB flush really needed ?
426 */
427 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
428 ipmmu_tlb_sync(domain);
Magnus Dammdbb70692017-05-17 19:06:38 +0900429 ipmmu_domain_free_context(domain->mmu, domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200430}
431
432/* -----------------------------------------------------------------------------
433 * Fault Handling
434 */
435
436static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
437{
438 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
439 struct ipmmu_vmsa_device *mmu = domain->mmu;
440 u32 status;
441 u32 iova;
442
443 status = ipmmu_ctx_read(domain, IMSTR);
444 if (!(status & err_mask))
445 return IRQ_NONE;
446
447 iova = ipmmu_ctx_read(domain, IMEAR);
448
449 /*
450 * Clear the error status flags. Unlike traditional interrupt flag
451 * registers that must be cleared by writing 1, this status register
452 * seems to require 0. The error address register must be read before,
453 * otherwise its value will be 0.
454 */
455 ipmmu_ctx_write(domain, IMSTR, 0);
456
457 /* Log fatal errors. */
458 if (status & IMSTR_MHIT)
459 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
460 iova);
461 if (status & IMSTR_ABORT)
462 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
463 iova);
464
465 if (!(status & (IMSTR_PF | IMSTR_TF)))
466 return IRQ_NONE;
467
468 /*
469 * Try to handle page faults and translation faults.
470 *
471 * TODO: We need to look up the faulty device based on the I/O VA. Use
472 * the IOMMU device for now.
473 */
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100474 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200475 return IRQ_HANDLED;
476
477 dev_err_ratelimited(mmu->dev,
478 "Unhandled fault: status 0x%08x iova 0x%08x\n",
479 status, iova);
480
481 return IRQ_HANDLED;
482}
483
484static irqreturn_t ipmmu_irq(int irq, void *dev)
485{
486 struct ipmmu_vmsa_device *mmu = dev;
Magnus Dammdbb70692017-05-17 19:06:38 +0900487 irqreturn_t status = IRQ_NONE;
488 unsigned int i;
489 unsigned long flags;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200490
Magnus Dammdbb70692017-05-17 19:06:38 +0900491 spin_lock_irqsave(&mmu->lock, flags);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200492
Magnus Dammdbb70692017-05-17 19:06:38 +0900493 /*
494 * Check interrupts for all active contexts.
495 */
496 for (i = 0; i < IPMMU_CTX_MAX; i++) {
497 if (!mmu->domains[i])
498 continue;
499 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
500 status = IRQ_HANDLED;
501 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200502
Magnus Dammdbb70692017-05-17 19:06:38 +0900503 spin_unlock_irqrestore(&mmu->lock, flags);
504
505 return status;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200506}
507
508/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200509 * IOMMU Operations
510 */
511
Magnus Damm8e73bf62017-05-17 19:06:59 +0900512static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200513{
514 struct ipmmu_vmsa_domain *domain;
515
516 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
517 if (!domain)
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100518 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200519
520 spin_lock_init(&domain->lock);
521
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100522 return &domain->io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200523}
524
Magnus Damm8e73bf62017-05-17 19:06:59 +0900525static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
526{
527 if (type != IOMMU_DOMAIN_UNMANAGED)
528 return NULL;
529
530 return __ipmmu_domain_alloc(type);
531}
532
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100533static void ipmmu_domain_free(struct iommu_domain *io_domain)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200534{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100535 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200536
537 /*
538 * Free the domain resources. We assume that all devices have already
539 * been detached.
540 */
541 ipmmu_domain_destroy_context(domain);
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200542 free_io_pgtable_ops(domain->iop);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200543 kfree(domain);
544}
545
546static int ipmmu_attach_device(struct iommu_domain *io_domain,
547 struct device *dev)
548{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200549 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
550 struct ipmmu_vmsa_device *mmu = archdata->mmu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100551 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200552 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200553 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200554 int ret = 0;
555
556 if (!mmu) {
557 dev_err(dev, "Cannot attach to IPMMU\n");
558 return -ENXIO;
559 }
560
561 spin_lock_irqsave(&domain->lock, flags);
562
563 if (!domain->mmu) {
564 /* The domain hasn't been used yet, initialize it. */
565 domain->mmu = mmu;
566 ret = ipmmu_domain_init_context(domain);
567 } else if (domain->mmu != mmu) {
568 /*
569 * Something is wrong, we can't attach two devices using
570 * different IOMMUs to the same domain.
571 */
572 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
573 dev_name(mmu->dev), dev_name(domain->mmu->dev));
574 ret = -EINVAL;
575 }
576
577 spin_unlock_irqrestore(&domain->lock, flags);
578
579 if (ret < 0)
580 return ret;
581
Laurent Pincharta166d312014-07-24 01:36:43 +0200582 for (i = 0; i < archdata->num_utlbs; ++i)
583 ipmmu_utlb_enable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200584
585 return 0;
586}
587
588static void ipmmu_detach_device(struct iommu_domain *io_domain,
589 struct device *dev)
590{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200591 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100592 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pincharta166d312014-07-24 01:36:43 +0200593 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200594
Laurent Pincharta166d312014-07-24 01:36:43 +0200595 for (i = 0; i < archdata->num_utlbs; ++i)
596 ipmmu_utlb_disable(domain, archdata->utlbs[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200597
598 /*
599 * TODO: Optimize by disabling the context when no device is attached.
600 */
601}
602
603static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
604 phys_addr_t paddr, size_t size, int prot)
605{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100606 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200607
608 if (!domain)
609 return -ENODEV;
610
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200611 return domain->iop->map(domain->iop, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200612}
613
614static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
615 size_t size)
616{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100617 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200618
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200619 return domain->iop->unmap(domain->iop, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200620}
621
622static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
623 dma_addr_t iova)
624{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100625 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200626
627 /* TODO: Is locking needed ? */
628
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200629 return domain->iop->iova_to_phys(domain->iop, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200630}
631
Laurent Pincharta166d312014-07-24 01:36:43 +0200632static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev,
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200633 unsigned int *utlbs, unsigned int num_utlbs)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200634{
Laurent Pincharta166d312014-07-24 01:36:43 +0200635 unsigned int i;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200636
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200637 for (i = 0; i < num_utlbs; ++i) {
Laurent Pincharta166d312014-07-24 01:36:43 +0200638 struct of_phandle_args args;
639 int ret;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100640
Laurent Pincharta166d312014-07-24 01:36:43 +0200641 ret = of_parse_phandle_with_args(dev->of_node, "iommus",
642 "#iommu-cells", i, &args);
643 if (ret < 0)
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200644 return ret;
Laurent Pincharta166d312014-07-24 01:36:43 +0200645
646 of_node_put(args.np);
647
648 if (args.np != mmu->dev->of_node || args.args_count != 1)
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200649 return -EINVAL;
Laurent Pincharta166d312014-07-24 01:36:43 +0200650
651 utlbs[i] = args.args[0];
652 }
653
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200654 return 0;
Laurent Pinchart192d2042014-05-15 12:40:42 +0200655}
656
Magnus Damm383fef5f2017-05-17 19:06:48 +0900657static int ipmmu_init_platform_device(struct device *dev)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200658{
Laurent Pinchart192d2042014-05-15 12:40:42 +0200659 struct ipmmu_vmsa_archdata *archdata;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200660 struct ipmmu_vmsa_device *mmu;
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200661 unsigned int *utlbs;
Laurent Pincharta166d312014-07-24 01:36:43 +0200662 unsigned int i;
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200663 int num_utlbs;
664 int ret = -ENODEV;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200665
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200666 /* Find the master corresponding to the device. */
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200667
668 num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus",
669 "#iommu-cells");
670 if (num_utlbs < 0)
671 return -ENODEV;
672
673 utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL);
674 if (!utlbs)
675 return -ENOMEM;
676
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200677 spin_lock(&ipmmu_devices_lock);
678
679 list_for_each_entry(mmu, &ipmmu_devices, list) {
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200680 ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs);
681 if (!ret) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200682 /*
Laurent Pinchart192d2042014-05-15 12:40:42 +0200683 * TODO Take a reference to the MMU to protect
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200684 * against device removal.
685 */
686 break;
687 }
688 }
689
690 spin_unlock(&ipmmu_devices_lock);
691
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200692 if (ret < 0)
Shawn Linb1e2afc2016-08-24 10:23:51 +0800693 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200694
Laurent Pincharta166d312014-07-24 01:36:43 +0200695 for (i = 0; i < num_utlbs; ++i) {
696 if (utlbs[i] >= mmu->num_utlbs) {
697 ret = -EINVAL;
698 goto error;
699 }
700 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200701
Magnus Damm383fef5f2017-05-17 19:06:48 +0900702 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
703 if (!archdata) {
704 ret = -ENOMEM;
705 goto error;
706 }
707
708 archdata->mmu = mmu;
709 archdata->utlbs = utlbs;
710 archdata->num_utlbs = num_utlbs;
711 dev->archdata.iommu = archdata;
712 return 0;
713
714error:
715 kfree(utlbs);
716 return ret;
717}
718
719static int ipmmu_add_device(struct device *dev)
720{
721 struct ipmmu_vmsa_archdata *archdata;
722 struct ipmmu_vmsa_device *mmu = NULL;
723 struct iommu_group *group;
724 int ret;
725
726 if (dev->archdata.iommu) {
727 dev_warn(dev, "IOMMU driver already assigned to device %s\n",
728 dev_name(dev));
729 return -EINVAL;
730 }
731
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200732 /* Create a device group and add the device to it. */
733 group = iommu_group_alloc();
734 if (IS_ERR(group)) {
735 dev_err(dev, "Failed to allocate IOMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200736 ret = PTR_ERR(group);
737 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200738 }
739
740 ret = iommu_group_add_device(group, dev);
741 iommu_group_put(group);
742
743 if (ret < 0) {
744 dev_err(dev, "Failed to add device to IPMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200745 group = NULL;
746 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200747 }
748
Magnus Damm383fef5f2017-05-17 19:06:48 +0900749 ret = ipmmu_init_platform_device(dev);
750 if (ret < 0)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200751 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200752
753 /*
754 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
755 * VAs. This will allocate a corresponding IOMMU domain.
756 *
757 * TODO:
758 * - Create one mapping per context (TLB).
759 * - Make the mapping size configurable ? We currently use a 2GB mapping
760 * at a 1GB offset to ensure that NULL VAs will fault.
761 */
Magnus Damm383fef5f2017-05-17 19:06:48 +0900762 archdata = dev->archdata.iommu;
763 mmu = archdata->mmu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200764 if (!mmu->mapping) {
765 struct dma_iommu_mapping *mapping;
766
767 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +0200768 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200769 if (IS_ERR(mapping)) {
770 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +0100771 ret = PTR_ERR(mapping);
772 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200773 }
774
775 mmu->mapping = mapping;
776 }
777
778 /* Attach the ARM VA mapping to the device. */
779 ret = arm_iommu_attach_device(dev, mmu->mapping);
780 if (ret < 0) {
781 dev_err(dev, "Failed to attach device to VA mapping\n");
782 goto error;
783 }
784
785 return 0;
786
787error:
Magnus Damm383fef5f2017-05-17 19:06:48 +0900788 if (mmu)
789 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +0200790
791 if (!IS_ERR_OR_NULL(group))
792 iommu_group_remove_device(dev);
793
Magnus Damm383fef5f2017-05-17 19:06:48 +0900794 kfree(archdata->utlbs);
795 kfree(archdata);
796 dev->archdata.iommu = NULL;
797
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200798 return ret;
799}
800
801static void ipmmu_remove_device(struct device *dev)
802{
Laurent Pincharta166d312014-07-24 01:36:43 +0200803 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
804
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200805 arm_iommu_detach_device(dev);
806 iommu_group_remove_device(dev);
Laurent Pincharta166d312014-07-24 01:36:43 +0200807
808 kfree(archdata->utlbs);
809 kfree(archdata);
810
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200811 dev->archdata.iommu = NULL;
812}
813
Thierry Redingb22f6432014-06-27 09:03:12 +0200814static const struct iommu_ops ipmmu_ops = {
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100815 .domain_alloc = ipmmu_domain_alloc,
816 .domain_free = ipmmu_domain_free,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200817 .attach_dev = ipmmu_attach_device,
818 .detach_dev = ipmmu_detach_device,
819 .map = ipmmu_map,
820 .unmap = ipmmu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -0700821 .map_sg = default_iommu_map_sg,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200822 .iova_to_phys = ipmmu_iova_to_phys,
823 .add_device = ipmmu_add_device,
824 .remove_device = ipmmu_remove_device,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200825 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200826};
827
828/* -----------------------------------------------------------------------------
829 * Probe/remove and init
830 */
831
832static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
833{
834 unsigned int i;
835
836 /* Disable all contexts. */
837 for (i = 0; i < 4; ++i)
838 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
839}
840
841static int ipmmu_probe(struct platform_device *pdev)
842{
843 struct ipmmu_vmsa_device *mmu;
844 struct resource *res;
845 int irq;
846 int ret;
847
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200848 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
849 if (!mmu) {
850 dev_err(&pdev->dev, "cannot allocate device data\n");
851 return -ENOMEM;
852 }
853
854 mmu->dev = &pdev->dev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200855 mmu->num_utlbs = 32;
Magnus Dammdbb70692017-05-17 19:06:38 +0900856 spin_lock_init(&mmu->lock);
857 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200858
859 /* Map I/O memory and request IRQ. */
860 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
861 mmu->base = devm_ioremap_resource(&pdev->dev, res);
862 if (IS_ERR(mmu->base))
863 return PTR_ERR(mmu->base);
864
Laurent Pinchart275f5052014-03-17 01:02:46 +0100865 /*
866 * The IPMMU has two register banks, for secure and non-secure modes.
867 * The bank mapped at the beginning of the IPMMU address space
868 * corresponds to the running mode of the CPU. When running in secure
869 * mode the non-secure register bank is also available at an offset.
870 *
871 * Secure mode operation isn't clearly documented and is thus currently
872 * not implemented in the driver. Furthermore, preliminary tests of
873 * non-secure operation with the main register bank were not successful.
874 * Offset the registers base unconditionally to point to the non-secure
875 * alias space for now.
876 */
877 mmu->base += IM_NS_ALIAS_OFFSET;
878
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200879 irq = platform_get_irq(pdev, 0);
880 if (irq < 0) {
881 dev_err(&pdev->dev, "no IRQ found\n");
882 return irq;
883 }
884
885 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
886 dev_name(&pdev->dev), mmu);
887 if (ret < 0) {
888 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +0800889 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200890 }
891
892 ipmmu_device_reset(mmu);
893
894 /*
895 * We can't create the ARM mapping here as it requires the bus to have
896 * an IOMMU, which only happens when bus_set_iommu() is called in
897 * ipmmu_init() after the probe function returns.
898 */
899
900 spin_lock(&ipmmu_devices_lock);
901 list_add(&mmu->list, &ipmmu_devices);
902 spin_unlock(&ipmmu_devices_lock);
903
904 platform_set_drvdata(pdev, mmu);
905
906 return 0;
907}
908
909static int ipmmu_remove(struct platform_device *pdev)
910{
911 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
912
913 spin_lock(&ipmmu_devices_lock);
914 list_del(&mmu->list);
915 spin_unlock(&ipmmu_devices_lock);
916
917 arm_iommu_release_mapping(mmu->mapping);
918
919 ipmmu_device_reset(mmu);
920
921 return 0;
922}
923
Laurent Pinchart275f5052014-03-17 01:02:46 +0100924static const struct of_device_id ipmmu_of_ids[] = {
925 { .compatible = "renesas,ipmmu-vmsa", },
Axel Linac04f852015-03-17 08:06:45 +0800926 { }
Laurent Pinchart275f5052014-03-17 01:02:46 +0100927};
928
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200929static struct platform_driver ipmmu_driver = {
930 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200931 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +0100932 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200933 },
934 .probe = ipmmu_probe,
935 .remove = ipmmu_remove,
936};
937
938static int __init ipmmu_init(void)
939{
940 int ret;
941
942 ret = platform_driver_register(&ipmmu_driver);
943 if (ret < 0)
944 return ret;
945
946 if (!iommu_present(&platform_bus_type))
947 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
948
949 return 0;
950}
951
952static void __exit ipmmu_exit(void)
953{
954 return platform_driver_unregister(&ipmmu_driver);
955}
956
957subsys_initcall(ipmmu_init);
958module_exit(ipmmu_exit);
959
960MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
961MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
962MODULE_LICENSE("GPL v2");