iommu/arm-smmu: Use accessor functions for iommu private data
[linux-2.6-block.git] / drivers / iommu / ipmmu-vmsa.c
CommitLineData
57d3f11c 1// SPDX-License-Identifier: GPL-2.0
d25a2a16 2/*
8128ac3b
PG
3 * IOMMU API for Renesas VMSA-compatible IPMMU
4 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
d25a2a16
LP
5 *
6 * Copyright (C) 2014 Renesas Electronics Corporation
d25a2a16
LP
7 */
8
dbb70692 9#include <linux/bitmap.h>
d25a2a16 10#include <linux/delay.h>
3ae47292 11#include <linux/dma-iommu.h>
d25a2a16
LP
12#include <linux/dma-mapping.h>
13#include <linux/err.h>
14#include <linux/export.h>
8128ac3b 15#include <linux/init.h>
d25a2a16
LP
16#include <linux/interrupt.h>
17#include <linux/io.h>
b77cf11f 18#include <linux/io-pgtable.h>
d25a2a16 19#include <linux/iommu.h>
275f5053 20#include <linux/of.h>
33f3ac9b 21#include <linux/of_device.h>
cda52fcd 22#include <linux/of_iommu.h>
7b2d5961 23#include <linux/of_platform.h>
d25a2a16
LP
24#include <linux/platform_device.h>
25#include <linux/sizes.h>
26#include <linux/slab.h>
58b8e8bf 27#include <linux/sys_soc.h>
d25a2a16 28
3ae47292 29#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
d25a2a16
LP
30#include <asm/dma-iommu.h>
31#include <asm/pgalloc.h>
49c875f0
RM
32#else
33#define arm_iommu_create_mapping(...) NULL
34#define arm_iommu_attach_device(...) -ENODEV
35#define arm_iommu_release_mapping(...) do {} while (0)
36#define arm_iommu_detach_device(...) do {} while (0)
3ae47292 37#endif
d25a2a16 38
da38e9ec
GU
39#define IPMMU_CTX_MAX 8U
40#define IPMMU_CTX_INVALID -1
41
42#define IPMMU_UTLB_MAX 48U
dbb70692 43
33f3ac9b
MD
44struct ipmmu_features {
45 bool use_ns_alias_offset;
fd5140e2 46 bool has_cache_leaf_nodes;
5fd16341 47 unsigned int number_of_contexts;
b7f3f047 48 unsigned int num_utlbs;
f5c85891 49 bool setup_imbuscr;
c295f504 50 bool twobit_imttbcr_sl0;
2ae86955 51 bool reserved_context;
3623002f 52 bool cache_snoop;
3dc28d9f
YS
53 unsigned int ctx_offset_base;
54 unsigned int ctx_offset_stride;
1289f7f1 55 unsigned int utlb_offset_base;
33f3ac9b
MD
56};
57
d25a2a16
LP
58struct ipmmu_vmsa_device {
59 struct device *dev;
60 void __iomem *base;
01da21e5 61 struct iommu_device iommu;
fd5140e2 62 struct ipmmu_vmsa_device *root;
33f3ac9b 63 const struct ipmmu_features *features;
5fd16341 64 unsigned int num_ctx;
dbb70692
MD
65 spinlock_t lock; /* Protects ctx and domains[] */
66 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
67 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
da38e9ec 68 s8 utlb_ctx[IPMMU_UTLB_MAX];
d25a2a16 69
b354c73e 70 struct iommu_group *group;
d25a2a16
LP
71 struct dma_iommu_mapping *mapping;
72};
73
74struct ipmmu_vmsa_domain {
75 struct ipmmu_vmsa_device *mmu;
5914c5fd 76 struct iommu_domain io_domain;
d25a2a16 77
f20ed39f
LP
78 struct io_pgtable_cfg cfg;
79 struct io_pgtable_ops *iop;
80
d25a2a16 81 unsigned int context_id;
46583e8c 82 struct mutex mutex; /* Protects mappings */
d25a2a16
LP
83};
84
5914c5fd
JR
85static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
86{
87 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
88}
89
e4efe4a9 90static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
0fbc8b04 91{
df903655
JR
92 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
93
94 return fwspec ? fwspec->iommu_priv : NULL;
0fbc8b04
MD
95}
96
d25a2a16
LP
97#define TLB_LOOP_TIMEOUT 100 /* 100us */
98
99/* -----------------------------------------------------------------------------
100 * Registers Definition
101 */
102
275f5053
LP
103#define IM_NS_ALIAS_OFFSET 0x800
104
df9828aa
YS
105/* MMU "context" registers */
106#define IMCTR 0x0000 /* R-Car Gen2/3 */
107#define IMCTR_INTEN (1 << 2) /* R-Car Gen2/3 */
108#define IMCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
109#define IMCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
110
111#define IMTTBCR 0x0008 /* R-Car Gen2/3 */
112#define IMTTBCR_EAE (1 << 31) /* R-Car Gen2/3 */
3623002f 113#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) /* R-Car Gen2 only */
3623002f 114#define IMTTBCR_ORGN0_WB_WA (1 << 10) /* R-Car Gen2 only */
3623002f 115#define IMTTBCR_IRGN0_WB_WA (1 << 8) /* R-Car Gen2 only */
5ca54fdc 116#define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) /* R-Car Gen3 only */
df9828aa 117#define IMTTBCR_SL0_LVL_1 (1 << 4) /* R-Car Gen2 only */
d25a2a16 118
df9828aa
YS
119#define IMBUSCR 0x000c /* R-Car Gen2 only */
120#define IMBUSCR_DVM (1 << 2) /* R-Car Gen2 only */
121#define IMBUSCR_BUSSEL_MASK (3 << 0) /* R-Car Gen2 only */
d25a2a16 122
df9828aa
YS
123#define IMTTLBR0 0x0010 /* R-Car Gen2/3 */
124#define IMTTUBR0 0x0014 /* R-Car Gen2/3 */
d25a2a16 125
df9828aa
YS
126#define IMSTR 0x0020 /* R-Car Gen2/3 */
127#define IMSTR_MHIT (1 << 4) /* R-Car Gen2/3 */
128#define IMSTR_ABORT (1 << 2) /* R-Car Gen2/3 */
129#define IMSTR_PF (1 << 1) /* R-Car Gen2/3 */
130#define IMSTR_TF (1 << 0) /* R-Car Gen2/3 */
d25a2a16 131
df9828aa 132#define IMMAIR0 0x0028 /* R-Car Gen2/3 */
d25a2a16 133
df9828aa
YS
134#define IMELAR 0x0030 /* R-Car Gen2/3, IMEAR on R-Car Gen2 */
135#define IMEUAR 0x0034 /* R-Car Gen3 only */
d25a2a16 136
df9828aa 137/* uTLB registers */
ddbbddd7 138#define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
df9828aa
YS
139#define IMUCTR0(n) (0x0300 + ((n) * 16)) /* R-Car Gen2/3 */
140#define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) /* R-Car Gen3 only */
141#define IMUCTR_TTSEL_MMU(n) ((n) << 4) /* R-Car Gen2/3 */
142#define IMUCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
143#define IMUCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
d25a2a16 144
ddbbddd7 145#define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
df9828aa
YS
146#define IMUASID0(n) (0x0308 + ((n) * 16)) /* R-Car Gen2/3 */
147#define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) /* R-Car Gen3 only */
d25a2a16 148
fd5140e2
MD
149/* -----------------------------------------------------------------------------
150 * Root device handling
151 */
152
153static struct platform_driver ipmmu_driver;
154
155static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
156{
157 return mmu->root == mmu;
158}
159
160static int __ipmmu_check_device(struct device *dev, void *data)
161{
162 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
163 struct ipmmu_vmsa_device **rootp = data;
164
165 if (ipmmu_is_root(mmu))
166 *rootp = mmu;
167
168 return 0;
169}
170
171static struct ipmmu_vmsa_device *ipmmu_find_root(void)
172{
173 struct ipmmu_vmsa_device *root = NULL;
174
175 return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
176 __ipmmu_check_device) == 0 ? root : NULL;
177}
178
d25a2a16
LP
179/* -----------------------------------------------------------------------------
180 * 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
16d9454f
YS
194static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
195 unsigned int context_id, unsigned int reg)
196{
3dc28d9f
YS
197 return mmu->features->ctx_offset_base +
198 context_id * mmu->features->ctx_offset_stride + reg;
16d9454f
YS
199}
200
201static u32 ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
202 unsigned int context_id, unsigned int reg)
203{
204 return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg));
205}
206
207static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu,
208 unsigned int context_id, unsigned int reg, u32 data)
209{
210 ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data);
211}
212
d574893a
MD
213static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
214 unsigned int reg)
d25a2a16 215{
16d9454f 216 return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg);
d25a2a16
LP
217}
218
d574893a
MD
219static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
220 unsigned int reg, u32 data)
d25a2a16 221{
16d9454f 222 ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
d25a2a16
LP
223}
224
d574893a
MD
225static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
226 unsigned int reg, u32 data)
227{
228 if (domain->mmu != domain->mmu->root)
16d9454f 229 ipmmu_ctx_write(domain->mmu, domain->context_id, reg, data);
d574893a 230
16d9454f 231 ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
d574893a
MD
232}
233
3667c997
YS
234static u32 ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, unsigned int reg)
235{
1289f7f1 236 return mmu->features->utlb_offset_base + reg;
3667c997
YS
237}
238
239static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
240 unsigned int utlb, u32 data)
241{
242 ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data);
243}
d574893a 244
3667c997
YS
245static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu,
246 unsigned int utlb, u32 data)
247{
248 ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data);
d574893a
MD
249}
250
d25a2a16
LP
251/* -----------------------------------------------------------------------------
252 * TLB and microTLB Management
253 */
254
255/* Wait for any pending TLB invalidations to complete */
256static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
257{
258 unsigned int count = 0;
259
d574893a 260 while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {
d25a2a16
LP
261 cpu_relax();
262 if (++count == TLB_LOOP_TIMEOUT) {
263 dev_err_ratelimited(domain->mmu->dev,
264 "TLB sync timed out -- MMU may be deadlocked\n");
265 return;
266 }
267 udelay(1);
268 }
269}
270
271static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
272{
273 u32 reg;
274
d574893a 275 reg = ipmmu_ctx_read_root(domain, IMCTR);
d25a2a16 276 reg |= IMCTR_FLUSH;
d574893a 277 ipmmu_ctx_write_all(domain, IMCTR, reg);
d25a2a16
LP
278
279 ipmmu_tlb_sync(domain);
280}
281
282/*
283 * Enable MMU translation for the microTLB.
284 */
285static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
192d2045 286 unsigned int utlb)
d25a2a16
LP
287{
288 struct ipmmu_vmsa_device *mmu = domain->mmu;
289
192d2045
LP
290 /*
291 * TODO: Reference-count the microTLB as several bus masters can be
292 * connected to the same microTLB.
293 */
294
d25a2a16 295 /* TODO: What should we set the ASID to ? */
3667c997 296 ipmmu_imuasid_write(mmu, utlb, 0);
d25a2a16 297 /* TODO: Do we need to flush the microTLB ? */
3667c997
YS
298 ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) |
299 IMUCTR_FLUSH | IMUCTR_MMUEN);
da38e9ec 300 mmu->utlb_ctx[utlb] = domain->context_id;
d25a2a16
LP
301}
302
303/*
304 * Disable MMU translation for the microTLB.
305 */
306static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
192d2045 307 unsigned int utlb)
d25a2a16
LP
308{
309 struct ipmmu_vmsa_device *mmu = domain->mmu;
310
3667c997 311 ipmmu_imuctr_write(mmu, utlb, 0);
da38e9ec 312 mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
d25a2a16
LP
313}
314
f20ed39f 315static void ipmmu_tlb_flush_all(void *cookie)
d25a2a16 316{
f20ed39f
LP
317 struct ipmmu_vmsa_domain *domain = cookie;
318
319 ipmmu_tlb_invalidate(domain);
320}
321
05aed941
WD
322static void ipmmu_tlb_flush(unsigned long iova, size_t size,
323 size_t granule, void *cookie)
f20ed39f 324{
05aed941 325 ipmmu_tlb_flush_all(cookie);
f20ed39f
LP
326}
327
298f7889 328static const struct iommu_flush_ops ipmmu_flush_ops = {
f20ed39f 329 .tlb_flush_all = ipmmu_tlb_flush_all,
05aed941
WD
330 .tlb_flush_walk = ipmmu_tlb_flush,
331 .tlb_flush_leaf = ipmmu_tlb_flush,
f20ed39f
LP
332};
333
d25a2a16
LP
334/* -----------------------------------------------------------------------------
335 * Domain/Context Management
336 */
337
dbb70692
MD
338static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
339 struct ipmmu_vmsa_domain *domain)
340{
341 unsigned long flags;
342 int ret;
343
344 spin_lock_irqsave(&mmu->lock, flags);
345
5fd16341
MD
346 ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
347 if (ret != mmu->num_ctx) {
dbb70692
MD
348 mmu->domains[ret] = domain;
349 set_bit(ret, mmu->ctx);
5fd16341
MD
350 } else
351 ret = -EBUSY;
dbb70692
MD
352
353 spin_unlock_irqrestore(&mmu->lock, flags);
354
355 return ret;
356}
357
a175a67d
OT
358static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
359 unsigned int context_id)
360{
361 unsigned long flags;
362
363 spin_lock_irqsave(&mmu->lock, flags);
364
365 clear_bit(context_id, mmu->ctx);
366 mmu->domains[context_id] = NULL;
367
368 spin_unlock_irqrestore(&mmu->lock, flags);
369}
370
892db541 371static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain *domain)
d25a2a16 372{
f64232ee 373 u64 ttbr;
c295f504 374 u32 tmp;
a175a67d 375
d25a2a16 376 /* TTBR0 */
d1e5f26f 377 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr;
d574893a
MD
378 ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
379 ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
d25a2a16
LP
380
381 /*
382 * TTBCR
3623002f
HNP
383 * We use long descriptors and allocate the whole 32-bit VA space to
384 * TTBR0.
d25a2a16 385 */
c295f504
MD
386 if (domain->mmu->features->twobit_imttbcr_sl0)
387 tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
388 else
389 tmp = IMTTBCR_SL0_LVL_1;
390
3623002f
HNP
391 if (domain->mmu->features->cache_snoop)
392 tmp |= IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
393 IMTTBCR_IRGN0_WB_WA;
394
395 ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | tmp);
d25a2a16 396
f20ed39f 397 /* MAIR0 */
d574893a 398 ipmmu_ctx_write_root(domain, IMMAIR0,
205577ab 399 domain->cfg.arm_lpae_s1_cfg.mair);
d25a2a16
LP
400
401 /* IMBUSCR */
f5c85891
MD
402 if (domain->mmu->features->setup_imbuscr)
403 ipmmu_ctx_write_root(domain, IMBUSCR,
404 ipmmu_ctx_read_root(domain, IMBUSCR) &
405 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
d25a2a16
LP
406
407 /*
408 * IMSTR
409 * Clear all interrupt flags.
410 */
d574893a 411 ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
d25a2a16
LP
412
413 /*
414 * IMCTR
415 * Enable the MMU and interrupt generation. The long-descriptor
416 * translation table format doesn't use TEX remapping. Don't enable AF
417 * software management as we have no use for it. Flush the TLB as
418 * required when modifying the context registers.
419 */
d574893a
MD
420 ipmmu_ctx_write_all(domain, IMCTR,
421 IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
892db541
GU
422}
423
424static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
425{
426 int ret;
427
428 /*
429 * Allocate the page table operations.
430 *
431 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
432 * access, Long-descriptor format" that the NStable bit being set in a
433 * table descriptor will result in the NStable and NS bits of all child
434 * entries being ignored and considered as being set. The IPMMU seems
435 * not to comply with this, as it generates a secure access page fault
436 * if any of the NStable and NS bits isn't set when running in
437 * non-secure mode.
438 */
439 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
440 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
441 domain->cfg.ias = 32;
442 domain->cfg.oas = 40;
298f7889 443 domain->cfg.tlb = &ipmmu_flush_ops;
892db541
GU
444 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
445 domain->io_domain.geometry.force_aperture = true;
446 /*
447 * TODO: Add support for coherent walk through CCI with DVM and remove
448 * cache handling. For now, delegate it to the io-pgtable code.
449 */
3430abd6 450 domain->cfg.coherent_walk = false;
892db541
GU
451 domain->cfg.iommu_dev = domain->mmu->root->dev;
452
453 /*
454 * Find an unused context.
455 */
456 ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
457 if (ret < 0)
458 return ret;
459
460 domain->context_id = ret;
461
462 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
463 domain);
464 if (!domain->iop) {
465 ipmmu_domain_free_context(domain->mmu->root,
466 domain->context_id);
467 return -EINVAL;
468 }
d25a2a16 469
892db541 470 ipmmu_domain_setup_context(domain);
d25a2a16
LP
471 return 0;
472}
473
474static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
475{
e5b78f2e
GU
476 if (!domain->mmu)
477 return;
478
d25a2a16
LP
479 /*
480 * Disable the context. Flush the TLB as required when modifying the
481 * context registers.
482 *
483 * TODO: Is TLB flush really needed ?
484 */
d574893a 485 ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
d25a2a16 486 ipmmu_tlb_sync(domain);
fd5140e2 487 ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
d25a2a16
LP
488}
489
490/* -----------------------------------------------------------------------------
491 * Fault Handling
492 */
493
494static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
495{
496 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
497 struct ipmmu_vmsa_device *mmu = domain->mmu;
82576aa8 498 unsigned long iova;
d25a2a16 499 u32 status;
d25a2a16 500
d574893a 501 status = ipmmu_ctx_read_root(domain, IMSTR);
d25a2a16
LP
502 if (!(status & err_mask))
503 return IRQ_NONE;
504
82576aa8
GU
505 iova = ipmmu_ctx_read_root(domain, IMELAR);
506 if (IS_ENABLED(CONFIG_64BIT))
507 iova |= (u64)ipmmu_ctx_read_root(domain, IMEUAR) << 32;
d25a2a16
LP
508
509 /*
510 * Clear the error status flags. Unlike traditional interrupt flag
511 * registers that must be cleared by writing 1, this status register
512 * seems to require 0. The error address register must be read before,
513 * otherwise its value will be 0.
514 */
d574893a 515 ipmmu_ctx_write_root(domain, IMSTR, 0);
d25a2a16
LP
516
517 /* Log fatal errors. */
518 if (status & IMSTR_MHIT)
82576aa8 519 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%lx\n",
d25a2a16
LP
520 iova);
521 if (status & IMSTR_ABORT)
82576aa8 522 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%lx\n",
d25a2a16
LP
523 iova);
524
525 if (!(status & (IMSTR_PF | IMSTR_TF)))
526 return IRQ_NONE;
527
528 /*
529 * Try to handle page faults and translation faults.
530 *
531 * TODO: We need to look up the faulty device based on the I/O VA. Use
532 * the IOMMU device for now.
533 */
5914c5fd 534 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
d25a2a16
LP
535 return IRQ_HANDLED;
536
537 dev_err_ratelimited(mmu->dev,
82576aa8 538 "Unhandled fault: status 0x%08x iova 0x%lx\n",
d25a2a16
LP
539 status, iova);
540
541 return IRQ_HANDLED;
542}
543
544static irqreturn_t ipmmu_irq(int irq, void *dev)
545{
546 struct ipmmu_vmsa_device *mmu = dev;
dbb70692
MD
547 irqreturn_t status = IRQ_NONE;
548 unsigned int i;
549 unsigned long flags;
d25a2a16 550
dbb70692
MD
551 spin_lock_irqsave(&mmu->lock, flags);
552
553 /*
554 * Check interrupts for all active contexts.
555 */
5fd16341 556 for (i = 0; i < mmu->num_ctx; i++) {
dbb70692
MD
557 if (!mmu->domains[i])
558 continue;
559 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
560 status = IRQ_HANDLED;
561 }
d25a2a16 562
dbb70692 563 spin_unlock_irqrestore(&mmu->lock, flags);
d25a2a16 564
dbb70692 565 return status;
d25a2a16
LP
566}
567
d25a2a16
LP
568/* -----------------------------------------------------------------------------
569 * IOMMU Operations
570 */
571
8e73bf65 572static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
d25a2a16
LP
573{
574 struct ipmmu_vmsa_domain *domain;
575
576 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
577 if (!domain)
5914c5fd 578 return NULL;
d25a2a16 579
46583e8c 580 mutex_init(&domain->mutex);
d25a2a16 581
5914c5fd 582 return &domain->io_domain;
d25a2a16
LP
583}
584
1c7e7c02
RM
585static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
586{
587 struct iommu_domain *io_domain = NULL;
588
589 switch (type) {
590 case IOMMU_DOMAIN_UNMANAGED:
591 io_domain = __ipmmu_domain_alloc(type);
592 break;
593
594 case IOMMU_DOMAIN_DMA:
595 io_domain = __ipmmu_domain_alloc(type);
596 if (io_domain && iommu_get_dma_cookie(io_domain)) {
597 kfree(io_domain);
598 io_domain = NULL;
599 }
600 break;
601 }
602
603 return io_domain;
604}
605
5914c5fd 606static void ipmmu_domain_free(struct iommu_domain *io_domain)
d25a2a16 607{
5914c5fd 608 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
d25a2a16
LP
609
610 /*
611 * Free the domain resources. We assume that all devices have already
612 * been detached.
613 */
1c7e7c02 614 iommu_put_dma_cookie(io_domain);
d25a2a16 615 ipmmu_domain_destroy_context(domain);
f20ed39f 616 free_io_pgtable_ops(domain->iop);
d25a2a16
LP
617 kfree(domain);
618}
619
620static int ipmmu_attach_device(struct iommu_domain *io_domain,
621 struct device *dev)
622{
df903655 623 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
e4efe4a9 624 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
5914c5fd 625 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
a166d31e 626 unsigned int i;
d25a2a16
LP
627 int ret = 0;
628
e4efe4a9 629 if (!mmu) {
d25a2a16
LP
630 dev_err(dev, "Cannot attach to IPMMU\n");
631 return -ENXIO;
632 }
633
46583e8c 634 mutex_lock(&domain->mutex);
d25a2a16
LP
635
636 if (!domain->mmu) {
637 /* The domain hasn't been used yet, initialize it. */
638 domain->mmu = mmu;
639 ret = ipmmu_domain_init_context(domain);
5fd16341
MD
640 if (ret < 0) {
641 dev_err(dev, "Unable to initialize IPMMU context\n");
642 domain->mmu = NULL;
643 } else {
644 dev_info(dev, "Using IPMMU context %u\n",
645 domain->context_id);
646 }
d25a2a16
LP
647 } else if (domain->mmu != mmu) {
648 /*
649 * Something is wrong, we can't attach two devices using
650 * different IOMMUs to the same domain.
651 */
652 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
653 dev_name(mmu->dev), dev_name(domain->mmu->dev));
654 ret = -EINVAL;
3ae47292
MD
655 } else
656 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
d25a2a16 657
46583e8c 658 mutex_unlock(&domain->mutex);
d25a2a16
LP
659
660 if (ret < 0)
661 return ret;
662
7b2d5961
MD
663 for (i = 0; i < fwspec->num_ids; ++i)
664 ipmmu_utlb_enable(domain, fwspec->ids[i]);
d25a2a16
LP
665
666 return 0;
667}
668
669static void ipmmu_detach_device(struct iommu_domain *io_domain,
670 struct device *dev)
671{
df903655 672 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
5914c5fd 673 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
a166d31e 674 unsigned int i;
d25a2a16 675
7b2d5961
MD
676 for (i = 0; i < fwspec->num_ids; ++i)
677 ipmmu_utlb_disable(domain, fwspec->ids[i]);
d25a2a16
LP
678
679 /*
680 * TODO: Optimize by disabling the context when no device is attached.
681 */
682}
683
684static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
781ca2de 685 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
d25a2a16 686{
5914c5fd 687 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
d25a2a16
LP
688
689 if (!domain)
690 return -ENODEV;
691
f20ed39f 692 return domain->iop->map(domain->iop, iova, paddr, size, prot);
d25a2a16
LP
693}
694
695static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
56f8af5e 696 size_t size, struct iommu_iotlb_gather *gather)
d25a2a16 697{
5914c5fd 698 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
d25a2a16 699
a2d3a382 700 return domain->iop->unmap(domain->iop, iova, size, gather);
d25a2a16
LP
701}
702
56f8af5e 703static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain)
32b12449
RM
704{
705 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
706
707 if (domain->mmu)
708 ipmmu_tlb_flush_all(domain);
709}
710
56f8af5e
WD
711static void ipmmu_iotlb_sync(struct iommu_domain *io_domain,
712 struct iommu_iotlb_gather *gather)
713{
714 ipmmu_flush_iotlb_all(io_domain);
715}
716
d25a2a16
LP
717static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
718 dma_addr_t iova)
719{
5914c5fd 720 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
d25a2a16
LP
721
722 /* TODO: Is locking needed ? */
723
f20ed39f 724 return domain->iop->iova_to_phys(domain->iop, iova);
d25a2a16
LP
725}
726
7b2d5961
MD
727static int ipmmu_init_platform_device(struct device *dev,
728 struct of_phandle_args *args)
d25a2a16 729{
df903655 730 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
7b2d5961 731 struct platform_device *ipmmu_pdev;
bb590c90 732
7b2d5961
MD
733 ipmmu_pdev = of_find_device_by_node(args->np);
734 if (!ipmmu_pdev)
bb590c90
LP
735 return -ENODEV;
736
df903655 737 fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
383fef5f 738
383fef5f 739 return 0;
58b8e8bf
MD
740}
741
0b8ac140 742static const struct soc_device_attribute soc_rcar_gen3[] = {
60fb0083 743 { .soc_id = "r8a774a1", },
757f26a3 744 { .soc_id = "r8a774b1", },
b6d39cd8 745 { .soc_id = "r8a774c0", },
58b8e8bf 746 { .soc_id = "r8a7795", },
0b8ac140 747 { .soc_id = "r8a7796", },
98dbffd3 748 { .soc_id = "r8a77965", },
3701c123 749 { .soc_id = "r8a77970", },
b0c32912 750 { .soc_id = "r8a77990", },
3701c123 751 { .soc_id = "r8a77995", },
58b8e8bf
MD
752 { /* sentinel */ }
753};
754
b7ee92c6 755static const struct soc_device_attribute soc_rcar_gen3_whitelist[] = {
757f26a3 756 { .soc_id = "r8a774b1", },
b6d39cd8 757 { .soc_id = "r8a774c0", },
b7ee92c6
YS
758 { .soc_id = "r8a7795", .revision = "ES3.*" },
759 { .soc_id = "r8a77965", },
760 { .soc_id = "r8a77990", },
3701c123 761 { .soc_id = "r8a77995", },
58b8e8bf
MD
762 { /* sentinel */ }
763};
764
80759649
YS
765static const char * const rcar_gen3_slave_whitelist[] = {
766};
767
b7ee92c6
YS
768static bool ipmmu_slave_whitelist(struct device *dev)
769{
80759649
YS
770 unsigned int i;
771
b7ee92c6
YS
772 /*
773 * For R-Car Gen3 use a white list to opt-in slave devices.
774 * For Other SoCs, this returns true anyway.
775 */
776 if (!soc_device_match(soc_rcar_gen3))
777 return true;
778
779 /* Check whether this R-Car Gen3 can use the IPMMU correctly or not */
780 if (!soc_device_match(soc_rcar_gen3_whitelist))
781 return false;
782
80759649
YS
783 /* Check whether this slave device can work with the IPMMU */
784 for (i = 0; i < ARRAY_SIZE(rcar_gen3_slave_whitelist); i++) {
785 if (!strcmp(dev_name(dev), rcar_gen3_slave_whitelist[i]))
786 return true;
787 }
788
789 /* Otherwise, do not allow use of IPMMU */
b7ee92c6
YS
790 return false;
791}
792
49558da0
MD
793static int ipmmu_of_xlate(struct device *dev,
794 struct of_phandle_args *spec)
795{
b7ee92c6 796 if (!ipmmu_slave_whitelist(dev))
58b8e8bf
MD
797 return -ENODEV;
798
7b2d5961
MD
799 iommu_fwspec_add_ids(dev, spec->args, 1);
800
49558da0 801 /* Initialize once - xlate() will call multiple times */
e4efe4a9 802 if (to_ipmmu(dev))
49558da0
MD
803 return 0;
804
7b2d5961 805 return ipmmu_init_platform_device(dev, spec);
49558da0
MD
806}
807
49c875f0 808static int ipmmu_init_arm_mapping(struct device *dev)
383fef5f 809{
e4efe4a9 810 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
383fef5f
MD
811 struct iommu_group *group;
812 int ret;
813
d25a2a16
LP
814 /* Create a device group and add the device to it. */
815 group = iommu_group_alloc();
816 if (IS_ERR(group)) {
817 dev_err(dev, "Failed to allocate IOMMU group\n");
49c875f0 818 return PTR_ERR(group);
d25a2a16
LP
819 }
820
821 ret = iommu_group_add_device(group, dev);
822 iommu_group_put(group);
823
824 if (ret < 0) {
825 dev_err(dev, "Failed to add device to IPMMU group\n");
49c875f0 826 return ret;
d25a2a16
LP
827 }
828
d25a2a16
LP
829 /*
830 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
831 * VAs. This will allocate a corresponding IOMMU domain.
832 *
833 * TODO:
834 * - Create one mapping per context (TLB).
835 * - Make the mapping size configurable ? We currently use a 2GB mapping
836 * at a 1GB offset to ensure that NULL VAs will fault.
837 */
838 if (!mmu->mapping) {
839 struct dma_iommu_mapping *mapping;
840
841 mapping = arm_iommu_create_mapping(&platform_bus_type,
720b0cef 842 SZ_1G, SZ_2G);
d25a2a16
LP
843 if (IS_ERR(mapping)) {
844 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
b8f80bff
LP
845 ret = PTR_ERR(mapping);
846 goto error;
d25a2a16
LP
847 }
848
849 mmu->mapping = mapping;
850 }
851
852 /* Attach the ARM VA mapping to the device. */
853 ret = arm_iommu_attach_device(dev, mmu->mapping);
854 if (ret < 0) {
855 dev_err(dev, "Failed to attach device to VA mapping\n");
856 goto error;
857 }
858
859 return 0;
860
861error:
49c875f0
RM
862 iommu_group_remove_device(dev);
863 if (mmu->mapping)
383fef5f 864 arm_iommu_release_mapping(mmu->mapping);
a166d31e 865
d25a2a16
LP
866 return ret;
867}
868
49c875f0 869static int ipmmu_add_device(struct device *dev)
3ae47292 870{
80eaa9f5 871 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
3ae47292 872 struct iommu_group *group;
80eaa9f5 873 int ret;
3ae47292 874
0fbc8b04
MD
875 /*
876 * Only let through devices that have been verified in xlate()
0fbc8b04 877 */
80eaa9f5 878 if (!mmu)
3ae47292
MD
879 return -ENODEV;
880
80eaa9f5
GU
881 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)) {
882 ret = ipmmu_init_arm_mapping(dev);
883 if (ret)
884 return ret;
885 } else {
886 group = iommu_group_get_for_dev(dev);
887 if (IS_ERR(group))
888 return PTR_ERR(group);
49c875f0 889
80eaa9f5
GU
890 iommu_group_put(group);
891 }
3ae47292 892
80eaa9f5 893 iommu_device_link(&mmu->iommu, dev);
3ae47292
MD
894 return 0;
895}
896
49c875f0 897static void ipmmu_remove_device(struct device *dev)
3ae47292 898{
80eaa9f5
GU
899 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
900
901 iommu_device_unlink(&mmu->iommu, dev);
49c875f0 902 arm_iommu_detach_device(dev);
3ae47292
MD
903 iommu_group_remove_device(dev);
904}
905
b354c73e 906static struct iommu_group *ipmmu_find_group(struct device *dev)
3ae47292 907{
e4efe4a9 908 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
3ae47292 909 struct iommu_group *group;
3ae47292 910
e4efe4a9
RM
911 if (mmu->group)
912 return iommu_group_ref_get(mmu->group);
b354c73e
RM
913
914 group = iommu_group_alloc();
915 if (!IS_ERR(group))
e4efe4a9 916 mmu->group = group;
3ae47292
MD
917
918 return group;
919}
920
3ae47292 921static const struct iommu_ops ipmmu_ops = {
1c7e7c02
RM
922 .domain_alloc = ipmmu_domain_alloc,
923 .domain_free = ipmmu_domain_free,
3ae47292
MD
924 .attach_dev = ipmmu_attach_device,
925 .detach_dev = ipmmu_detach_device,
926 .map = ipmmu_map,
927 .unmap = ipmmu_unmap,
56f8af5e 928 .flush_iotlb_all = ipmmu_flush_iotlb_all,
32b12449 929 .iotlb_sync = ipmmu_iotlb_sync,
3ae47292 930 .iova_to_phys = ipmmu_iova_to_phys,
49c875f0
RM
931 .add_device = ipmmu_add_device,
932 .remove_device = ipmmu_remove_device,
b354c73e 933 .device_group = ipmmu_find_group,
3ae47292 934 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
49558da0 935 .of_xlate = ipmmu_of_xlate,
3ae47292
MD
936};
937
d25a2a16
LP
938/* -----------------------------------------------------------------------------
939 * Probe/remove and init
940 */
941
942static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
943{
944 unsigned int i;
945
946 /* Disable all contexts. */
5fd16341 947 for (i = 0; i < mmu->num_ctx; ++i)
16d9454f 948 ipmmu_ctx_write(mmu, i, IMCTR, 0);
d25a2a16
LP
949}
950
33f3ac9b
MD
951static const struct ipmmu_features ipmmu_features_default = {
952 .use_ns_alias_offset = true,
fd5140e2 953 .has_cache_leaf_nodes = false,
5fd16341 954 .number_of_contexts = 1, /* software only tested with one context */
b7f3f047 955 .num_utlbs = 32,
f5c85891 956 .setup_imbuscr = true,
c295f504 957 .twobit_imttbcr_sl0 = false,
2ae86955 958 .reserved_context = false,
3623002f 959 .cache_snoop = true,
3dc28d9f
YS
960 .ctx_offset_base = 0,
961 .ctx_offset_stride = 0x40,
1289f7f1 962 .utlb_offset_base = 0,
33f3ac9b
MD
963};
964
0b8ac140 965static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
58b8e8bf
MD
966 .use_ns_alias_offset = false,
967 .has_cache_leaf_nodes = true,
968 .number_of_contexts = 8,
b7f3f047 969 .num_utlbs = 48,
58b8e8bf
MD
970 .setup_imbuscr = false,
971 .twobit_imttbcr_sl0 = true,
2ae86955 972 .reserved_context = true,
3623002f 973 .cache_snoop = false,
3dc28d9f
YS
974 .ctx_offset_base = 0,
975 .ctx_offset_stride = 0x40,
1289f7f1 976 .utlb_offset_base = 0,
58b8e8bf
MD
977};
978
33f3ac9b
MD
979static const struct of_device_id ipmmu_of_ids[] = {
980 {
981 .compatible = "renesas,ipmmu-vmsa",
982 .data = &ipmmu_features_default,
60fb0083
FC
983 }, {
984 .compatible = "renesas,ipmmu-r8a774a1",
985 .data = &ipmmu_features_rcar_gen3,
757f26a3
BD
986 }, {
987 .compatible = "renesas,ipmmu-r8a774b1",
988 .data = &ipmmu_features_rcar_gen3,
b6d39cd8
FC
989 }, {
990 .compatible = "renesas,ipmmu-r8a774c0",
991 .data = &ipmmu_features_rcar_gen3,
58b8e8bf
MD
992 }, {
993 .compatible = "renesas,ipmmu-r8a7795",
0b8ac140
MD
994 .data = &ipmmu_features_rcar_gen3,
995 }, {
996 .compatible = "renesas,ipmmu-r8a7796",
997 .data = &ipmmu_features_rcar_gen3,
98dbffd3
JM
998 }, {
999 .compatible = "renesas,ipmmu-r8a77965",
1000 .data = &ipmmu_features_rcar_gen3,
3701c123
SH
1001 }, {
1002 .compatible = "renesas,ipmmu-r8a77970",
1003 .data = &ipmmu_features_rcar_gen3,
b0c32912
HNP
1004 }, {
1005 .compatible = "renesas,ipmmu-r8a77990",
1006 .data = &ipmmu_features_rcar_gen3,
3701c123
SH
1007 }, {
1008 .compatible = "renesas,ipmmu-r8a77995",
1009 .data = &ipmmu_features_rcar_gen3,
33f3ac9b
MD
1010 }, {
1011 /* Terminator */
1012 },
1013};
1014
d25a2a16
LP
1015static int ipmmu_probe(struct platform_device *pdev)
1016{
1017 struct ipmmu_vmsa_device *mmu;
1018 struct resource *res;
1019 int irq;
1020 int ret;
1021
d25a2a16
LP
1022 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
1023 if (!mmu) {
1024 dev_err(&pdev->dev, "cannot allocate device data\n");
1025 return -ENOMEM;
1026 }
1027
1028 mmu->dev = &pdev->dev;
dbb70692
MD
1029 spin_lock_init(&mmu->lock);
1030 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
33f3ac9b 1031 mmu->features = of_device_get_match_data(&pdev->dev);
da38e9ec 1032 memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs);
1c894225 1033 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
d25a2a16
LP
1034
1035 /* Map I/O memory and request IRQ. */
1036 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1037 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1038 if (IS_ERR(mmu->base))
1039 return PTR_ERR(mmu->base);
1040
275f5053
LP
1041 /*
1042 * The IPMMU has two register banks, for secure and non-secure modes.
1043 * The bank mapped at the beginning of the IPMMU address space
1044 * corresponds to the running mode of the CPU. When running in secure
1045 * mode the non-secure register bank is also available at an offset.
1046 *
1047 * Secure mode operation isn't clearly documented and is thus currently
1048 * not implemented in the driver. Furthermore, preliminary tests of
1049 * non-secure operation with the main register bank were not successful.
1050 * Offset the registers base unconditionally to point to the non-secure
1051 * alias space for now.
1052 */
33f3ac9b
MD
1053 if (mmu->features->use_ns_alias_offset)
1054 mmu->base += IM_NS_ALIAS_OFFSET;
275f5053 1055
b43e0d8a 1056 mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
5fd16341 1057
fd5140e2
MD
1058 /*
1059 * Determine if this IPMMU instance is a root device by checking for
1060 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
1061 */
1062 if (!mmu->features->has_cache_leaf_nodes ||
1063 !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
1064 mmu->root = mmu;
1065 else
1066 mmu->root = ipmmu_find_root();
d25a2a16 1067
fd5140e2
MD
1068 /*
1069 * Wait until the root device has been registered for sure.
1070 */
1071 if (!mmu->root)
1072 return -EPROBE_DEFER;
1073
1074 /* Root devices have mandatory IRQs */
1075 if (ipmmu_is_root(mmu)) {
ec37d4e9 1076 irq = platform_get_irq(pdev, 0);
565d4542 1077 if (irq < 0)
fd5140e2 1078 return irq;
fd5140e2
MD
1079
1080 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1081 dev_name(&pdev->dev), mmu);
1082 if (ret < 0) {
1083 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1084 return ret;
1085 }
1086
1087 ipmmu_device_reset(mmu);
2ae86955
YS
1088
1089 if (mmu->features->reserved_context) {
1090 dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
1091 set_bit(0, mmu->ctx);
1092 }
fd5140e2 1093 }
d25a2a16 1094
cda52fcd
MD
1095 /*
1096 * Register the IPMMU to the IOMMU subsystem in the following cases:
1097 * - R-Car Gen2 IPMMU (all devices registered)
1098 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1099 */
1100 if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
1101 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
1102 dev_name(&pdev->dev));
1103 if (ret)
1104 return ret;
7af9a5fd 1105
cda52fcd
MD
1106 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
1107 iommu_device_set_fwnode(&mmu->iommu,
1108 &pdev->dev.of_node->fwnode);
01da21e5 1109
cda52fcd
MD
1110 ret = iommu_device_register(&mmu->iommu);
1111 if (ret)
1112 return ret;
1113
1114#if defined(CONFIG_IOMMU_DMA)
1115 if (!iommu_present(&platform_bus_type))
1116 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1117#endif
1118 }
01da21e5 1119
d25a2a16
LP
1120 /*
1121 * We can't create the ARM mapping here as it requires the bus to have
1122 * an IOMMU, which only happens when bus_set_iommu() is called in
1123 * ipmmu_init() after the probe function returns.
1124 */
1125
d25a2a16
LP
1126 platform_set_drvdata(pdev, mmu);
1127
1128 return 0;
1129}
1130
1131static int ipmmu_remove(struct platform_device *pdev)
1132{
1133 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1134
7af9a5fd 1135 iommu_device_sysfs_remove(&mmu->iommu);
01da21e5
MD
1136 iommu_device_unregister(&mmu->iommu);
1137
d25a2a16
LP
1138 arm_iommu_release_mapping(mmu->mapping);
1139
1140 ipmmu_device_reset(mmu);
1141
1142 return 0;
1143}
1144
da38e9ec
GU
1145#ifdef CONFIG_PM_SLEEP
1146static int ipmmu_resume_noirq(struct device *dev)
1147{
1148 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
1149 unsigned int i;
1150
1151 /* Reset root MMU and restore contexts */
1152 if (ipmmu_is_root(mmu)) {
1153 ipmmu_device_reset(mmu);
1154
1155 for (i = 0; i < mmu->num_ctx; i++) {
1156 if (!mmu->domains[i])
1157 continue;
1158
1159 ipmmu_domain_setup_context(mmu->domains[i]);
1160 }
1161 }
1162
1163 /* Re-enable active micro-TLBs */
1164 for (i = 0; i < mmu->features->num_utlbs; i++) {
1165 if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID)
1166 continue;
1167
1168 ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i);
1169 }
1170
1171 return 0;
1172}
1173
1174static const struct dev_pm_ops ipmmu_pm = {
1175 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq)
1176};
1177#define DEV_PM_OPS &ipmmu_pm
1178#else
1179#define DEV_PM_OPS NULL
1180#endif /* CONFIG_PM_SLEEP */
1181
d25a2a16
LP
1182static struct platform_driver ipmmu_driver = {
1183 .driver = {
d25a2a16 1184 .name = "ipmmu-vmsa",
275f5053 1185 .of_match_table = of_match_ptr(ipmmu_of_ids),
da38e9ec 1186 .pm = DEV_PM_OPS,
d25a2a16
LP
1187 },
1188 .probe = ipmmu_probe,
1189 .remove = ipmmu_remove,
1190};
1191
1192static int __init ipmmu_init(void)
1193{
5c5c8741 1194 struct device_node *np;
cda52fcd 1195 static bool setup_done;
d25a2a16
LP
1196 int ret;
1197
cda52fcd
MD
1198 if (setup_done)
1199 return 0;
1200
5c5c8741
DO
1201 np = of_find_matching_node(NULL, ipmmu_of_ids);
1202 if (!np)
1203 return 0;
1204
1205 of_node_put(np);
1206
d25a2a16
LP
1207 ret = platform_driver_register(&ipmmu_driver);
1208 if (ret < 0)
1209 return ret;
1210
cda52fcd 1211#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
d25a2a16
LP
1212 if (!iommu_present(&platform_bus_type))
1213 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
cda52fcd 1214#endif
d25a2a16 1215
cda52fcd 1216 setup_done = true;
d25a2a16
LP
1217 return 0;
1218}
d25a2a16 1219subsys_initcall(ipmmu_init);