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