blob: 5ce7879cb58d7f150e95f52a71e66f61077c714a [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>
Magnus Damm3ae47292017-05-17 19:07:10 +090013#include <linux/dma-iommu.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020014#include <linux/dma-mapping.h>
15#include <linux/err.h>
16#include <linux/export.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/iommu.h>
20#include <linux/module.h>
Laurent Pinchart275f5052014-03-17 01:02:46 +010021#include <linux/of.h>
Magnus Damm33f3ac92017-10-16 21:29:25 +090022#include <linux/of_device.h>
Magnus Damm7b2d5962017-07-17 22:05:41 +090023#include <linux/of_platform.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020024#include <linux/platform_device.h>
25#include <linux/sizes.h>
26#include <linux/slab.h>
27
Magnus Damm3ae47292017-05-17 19:07:10 +090028#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020029#include <asm/dma-iommu.h>
30#include <asm/pgalloc.h>
Robin Murphy49c875f2017-10-13 19:23:42 +010031#else
32#define arm_iommu_create_mapping(...) NULL
33#define arm_iommu_attach_device(...) -ENODEV
34#define arm_iommu_release_mapping(...) do {} while (0)
35#define arm_iommu_detach_device(...) do {} while (0)
Magnus Damm3ae47292017-05-17 19:07:10 +090036#endif
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020037
Laurent Pinchartf20ed392015-01-20 18:30:04 +020038#include "io-pgtable.h"
39
Magnus Dammdbb70692017-05-17 19:06:38 +090040#define IPMMU_CTX_MAX 1
41
Magnus Damm33f3ac92017-10-16 21:29:25 +090042struct ipmmu_features {
43 bool use_ns_alias_offset;
44};
45
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020046struct ipmmu_vmsa_device {
47 struct device *dev;
48 void __iomem *base;
Magnus Damm01da21e2017-07-17 22:05:10 +090049 struct iommu_device iommu;
Magnus Damm33f3ac92017-10-16 21:29:25 +090050 const struct ipmmu_features *features;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020051 unsigned int num_utlbs;
Magnus Dammdbb70692017-05-17 19:06:38 +090052 spinlock_t lock; /* Protects ctx and domains[] */
53 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
54 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020055
Robin Murphyb354c732017-10-13 19:23:40 +010056 struct iommu_group *group;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020057 struct dma_iommu_mapping *mapping;
58};
59
60struct ipmmu_vmsa_domain {
61 struct ipmmu_vmsa_device *mmu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +010062 struct iommu_domain io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020063
Laurent Pinchartf20ed392015-01-20 18:30:04 +020064 struct io_pgtable_cfg cfg;
65 struct io_pgtable_ops *iop;
66
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020067 unsigned int context_id;
68 spinlock_t lock; /* Protects mappings */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020069};
70
Joerg Roedel5914c5f2015-03-26 13:43:16 +010071static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
72{
73 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
74}
75
Robin Murphye4efe4a2017-10-13 19:23:41 +010076static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
Magnus Damm0fbc8b02017-05-17 19:07:20 +090077{
Robin Murphy3c49ed32017-07-17 22:05:31 +090078 return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL;
Magnus Damm0fbc8b02017-05-17 19:07:20 +090079}
80
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020081#define TLB_LOOP_TIMEOUT 100 /* 100us */
82
83/* -----------------------------------------------------------------------------
84 * Registers Definition
85 */
86
Laurent Pinchart275f5052014-03-17 01:02:46 +010087#define IM_NS_ALIAS_OFFSET 0x800
88
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020089#define IM_CTX_SIZE 0x40
90
91#define IMCTR 0x0000
92#define IMCTR_TRE (1 << 17)
93#define IMCTR_AFE (1 << 16)
94#define IMCTR_RTSEL_MASK (3 << 4)
95#define IMCTR_RTSEL_SHIFT 4
96#define IMCTR_TREN (1 << 3)
97#define IMCTR_INTEN (1 << 2)
98#define IMCTR_FLUSH (1 << 1)
99#define IMCTR_MMUEN (1 << 0)
100
101#define IMCAAR 0x0004
102
103#define IMTTBCR 0x0008
104#define IMTTBCR_EAE (1 << 31)
105#define IMTTBCR_PMB (1 << 30)
106#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
107#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
108#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
109#define IMTTBCR_SH1_MASK (3 << 28)
110#define IMTTBCR_ORGN1_NC (0 << 26)
111#define IMTTBCR_ORGN1_WB_WA (1 << 26)
112#define IMTTBCR_ORGN1_WT (2 << 26)
113#define IMTTBCR_ORGN1_WB (3 << 26)
114#define IMTTBCR_ORGN1_MASK (3 << 26)
115#define IMTTBCR_IRGN1_NC (0 << 24)
116#define IMTTBCR_IRGN1_WB_WA (1 << 24)
117#define IMTTBCR_IRGN1_WT (2 << 24)
118#define IMTTBCR_IRGN1_WB (3 << 24)
119#define IMTTBCR_IRGN1_MASK (3 << 24)
120#define IMTTBCR_TSZ1_MASK (7 << 16)
121#define IMTTBCR_TSZ1_SHIFT 16
122#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
123#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
124#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
125#define IMTTBCR_SH0_MASK (3 << 12)
126#define IMTTBCR_ORGN0_NC (0 << 10)
127#define IMTTBCR_ORGN0_WB_WA (1 << 10)
128#define IMTTBCR_ORGN0_WT (2 << 10)
129#define IMTTBCR_ORGN0_WB (3 << 10)
130#define IMTTBCR_ORGN0_MASK (3 << 10)
131#define IMTTBCR_IRGN0_NC (0 << 8)
132#define IMTTBCR_IRGN0_WB_WA (1 << 8)
133#define IMTTBCR_IRGN0_WT (2 << 8)
134#define IMTTBCR_IRGN0_WB (3 << 8)
135#define IMTTBCR_IRGN0_MASK (3 << 8)
136#define IMTTBCR_SL0_LVL_2 (0 << 4)
137#define IMTTBCR_SL0_LVL_1 (1 << 4)
138#define IMTTBCR_TSZ0_MASK (7 << 0)
139#define IMTTBCR_TSZ0_SHIFT O
140
141#define IMBUSCR 0x000c
142#define IMBUSCR_DVM (1 << 2)
143#define IMBUSCR_BUSSEL_SYS (0 << 0)
144#define IMBUSCR_BUSSEL_CCI (1 << 0)
145#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
146#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
147#define IMBUSCR_BUSSEL_MASK (3 << 0)
148
149#define IMTTLBR0 0x0010
150#define IMTTUBR0 0x0014
151#define IMTTLBR1 0x0018
152#define IMTTUBR1 0x001c
153
154#define IMSTR 0x0020
155#define IMSTR_ERRLVL_MASK (3 << 12)
156#define IMSTR_ERRLVL_SHIFT 12
157#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
158#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
159#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
160#define IMSTR_ERRCODE_MASK (7 << 8)
161#define IMSTR_MHIT (1 << 4)
162#define IMSTR_ABORT (1 << 2)
163#define IMSTR_PF (1 << 1)
164#define IMSTR_TF (1 << 0)
165
166#define IMMAIR0 0x0028
167#define IMMAIR1 0x002c
168#define IMMAIR_ATTR_MASK 0xff
169#define IMMAIR_ATTR_DEVICE 0x04
170#define IMMAIR_ATTR_NC 0x44
171#define IMMAIR_ATTR_WBRWA 0xff
172#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
173#define IMMAIR_ATTR_IDX_NC 0
174#define IMMAIR_ATTR_IDX_WBRWA 1
175#define IMMAIR_ATTR_IDX_DEV 2
176
177#define IMEAR 0x0030
178
179#define IMPCTR 0x0200
180#define IMPSTR 0x0208
181#define IMPEAR 0x020c
182#define IMPMBA(n) (0x0280 + ((n) * 4))
183#define IMPMBD(n) (0x02c0 + ((n) * 4))
184
185#define IMUCTR(n) (0x0300 + ((n) * 16))
186#define IMUCTR_FIXADDEN (1 << 31)
187#define IMUCTR_FIXADD_MASK (0xff << 16)
188#define IMUCTR_FIXADD_SHIFT 16
189#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
190#define IMUCTR_TTSEL_PMB (8 << 4)
191#define IMUCTR_TTSEL_MASK (15 << 4)
192#define IMUCTR_FLUSH (1 << 1)
193#define IMUCTR_MMUEN (1 << 0)
194
195#define IMUASID(n) (0x0308 + ((n) * 16))
196#define IMUASID_ASID8_MASK (0xff << 8)
197#define IMUASID_ASID8_SHIFT 8
198#define IMUASID_ASID0_MASK (0xff << 0)
199#define IMUASID_ASID0_SHIFT 0
200
201/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200202 * Read/Write Access
203 */
204
205static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
206{
207 return ioread32(mmu->base + offset);
208}
209
210static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
211 u32 data)
212{
213 iowrite32(data, mmu->base + offset);
214}
215
216static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
217{
218 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
219}
220
221static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
222 u32 data)
223{
224 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
225}
226
227/* -----------------------------------------------------------------------------
228 * TLB and microTLB Management
229 */
230
231/* Wait for any pending TLB invalidations to complete */
232static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
233{
234 unsigned int count = 0;
235
236 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
237 cpu_relax();
238 if (++count == TLB_LOOP_TIMEOUT) {
239 dev_err_ratelimited(domain->mmu->dev,
240 "TLB sync timed out -- MMU may be deadlocked\n");
241 return;
242 }
243 udelay(1);
244 }
245}
246
247static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
248{
249 u32 reg;
250
251 reg = ipmmu_ctx_read(domain, IMCTR);
252 reg |= IMCTR_FLUSH;
253 ipmmu_ctx_write(domain, IMCTR, reg);
254
255 ipmmu_tlb_sync(domain);
256}
257
258/*
259 * Enable MMU translation for the microTLB.
260 */
261static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200262 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200263{
264 struct ipmmu_vmsa_device *mmu = domain->mmu;
265
Laurent Pinchart192d2042014-05-15 12:40:42 +0200266 /*
267 * TODO: Reference-count the microTLB as several bus masters can be
268 * connected to the same microTLB.
269 */
270
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200271 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200272 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200273 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200274 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200275 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
276 IMUCTR_MMUEN);
277}
278
279/*
280 * Disable MMU translation for the microTLB.
281 */
282static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200283 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200284{
285 struct ipmmu_vmsa_device *mmu = domain->mmu;
286
Laurent Pinchart192d2042014-05-15 12:40:42 +0200287 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200288}
289
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200290static void ipmmu_tlb_flush_all(void *cookie)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200291{
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200292 struct ipmmu_vmsa_domain *domain = cookie;
293
294 ipmmu_tlb_invalidate(domain);
295}
296
Robin Murphy06c610e2015-12-07 18:18:53 +0000297static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
298 size_t granule, bool leaf, void *cookie)
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200299{
300 /* The hardware doesn't support selective TLB flush. */
301}
302
Bhumika Goyal8da4af92017-08-28 23:47:27 +0530303static const struct iommu_gather_ops ipmmu_gather_ops = {
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200304 .tlb_flush_all = ipmmu_tlb_flush_all,
305 .tlb_add_flush = ipmmu_tlb_add_flush,
306 .tlb_sync = ipmmu_tlb_flush_all,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200307};
308
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200309/* -----------------------------------------------------------------------------
310 * Domain/Context Management
311 */
312
Magnus Dammdbb70692017-05-17 19:06:38 +0900313static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
314 struct ipmmu_vmsa_domain *domain)
315{
316 unsigned long flags;
317 int ret;
318
319 spin_lock_irqsave(&mmu->lock, flags);
320
321 ret = find_first_zero_bit(mmu->ctx, IPMMU_CTX_MAX);
322 if (ret != IPMMU_CTX_MAX) {
323 mmu->domains[ret] = domain;
324 set_bit(ret, mmu->ctx);
325 }
326
327 spin_unlock_irqrestore(&mmu->lock, flags);
328
329 return ret;
330}
331
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300332static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
333 unsigned int context_id)
334{
335 unsigned long flags;
336
337 spin_lock_irqsave(&mmu->lock, flags);
338
339 clear_bit(context_id, mmu->ctx);
340 mmu->domains[context_id] = NULL;
341
342 spin_unlock_irqrestore(&mmu->lock, flags);
343}
344
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200345static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
346{
Geert Uytterhoevenf64232e2015-12-22 20:01:06 +0100347 u64 ttbr;
Magnus Dammdbb70692017-05-17 19:06:38 +0900348 int ret;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200349
350 /*
351 * Allocate the page table operations.
352 *
353 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
354 * access, Long-descriptor format" that the NStable bit being set in a
355 * table descriptor will result in the NStable and NS bits of all child
356 * entries being ignored and considered as being set. The IPMMU seems
357 * not to comply with this, as it generates a secure access page fault
358 * if any of the NStable and NS bits isn't set when running in
359 * non-secure mode.
360 */
361 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
Magnus Damm26b6aec2017-05-17 19:07:41 +0900362 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200363 domain->cfg.ias = 32;
364 domain->cfg.oas = 40;
365 domain->cfg.tlb = &ipmmu_gather_ops;
Geert Uytterhoeven3b6bb5b2017-01-31 12:17:07 +0100366 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
367 domain->io_domain.geometry.force_aperture = true;
Robin Murphyff2ed962015-07-29 19:46:08 +0100368 /*
369 * TODO: Add support for coherent walk through CCI with DVM and remove
370 * cache handling. For now, delegate it to the io-pgtable code.
371 */
372 domain->cfg.iommu_dev = domain->mmu->dev;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200373
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200374 /*
Magnus Dammdbb70692017-05-17 19:06:38 +0900375 * Find an unused context.
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200376 */
Magnus Dammdbb70692017-05-17 19:06:38 +0900377 ret = ipmmu_domain_allocate_context(domain->mmu, domain);
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300378 if (ret == IPMMU_CTX_MAX)
Magnus Dammdbb70692017-05-17 19:06:38 +0900379 return -EBUSY;
Magnus Dammdbb70692017-05-17 19:06:38 +0900380
381 domain->context_id = ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200382
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300383 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
384 domain);
385 if (!domain->iop) {
386 ipmmu_domain_free_context(domain->mmu, domain->context_id);
387 return -EINVAL;
388 }
389
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200390 /* TTBR0 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200391 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200392 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
393 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
394
395 /*
396 * TTBCR
397 * We use long descriptors with inner-shareable WBWA tables and allocate
398 * the whole 32-bit VA space to TTBR0.
399 */
400 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
401 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
402 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
403
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200404 /* MAIR0 */
405 ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200406
407 /* IMBUSCR */
408 ipmmu_ctx_write(domain, IMBUSCR,
409 ipmmu_ctx_read(domain, IMBUSCR) &
410 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
411
412 /*
413 * IMSTR
414 * Clear all interrupt flags.
415 */
416 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
417
418 /*
419 * IMCTR
420 * Enable the MMU and interrupt generation. The long-descriptor
421 * translation table format doesn't use TEX remapping. Don't enable AF
422 * software management as we have no use for it. Flush the TLB as
423 * required when modifying the context registers.
424 */
425 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
426
427 return 0;
428}
429
430static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
431{
432 /*
433 * Disable the context. Flush the TLB as required when modifying the
434 * context registers.
435 *
436 * TODO: Is TLB flush really needed ?
437 */
438 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
439 ipmmu_tlb_sync(domain);
Magnus Dammdbb70692017-05-17 19:06:38 +0900440 ipmmu_domain_free_context(domain->mmu, domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200441}
442
443/* -----------------------------------------------------------------------------
444 * Fault Handling
445 */
446
447static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
448{
449 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
450 struct ipmmu_vmsa_device *mmu = domain->mmu;
451 u32 status;
452 u32 iova;
453
454 status = ipmmu_ctx_read(domain, IMSTR);
455 if (!(status & err_mask))
456 return IRQ_NONE;
457
458 iova = ipmmu_ctx_read(domain, IMEAR);
459
460 /*
461 * Clear the error status flags. Unlike traditional interrupt flag
462 * registers that must be cleared by writing 1, this status register
463 * seems to require 0. The error address register must be read before,
464 * otherwise its value will be 0.
465 */
466 ipmmu_ctx_write(domain, IMSTR, 0);
467
468 /* Log fatal errors. */
469 if (status & IMSTR_MHIT)
470 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
471 iova);
472 if (status & IMSTR_ABORT)
473 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
474 iova);
475
476 if (!(status & (IMSTR_PF | IMSTR_TF)))
477 return IRQ_NONE;
478
479 /*
480 * Try to handle page faults and translation faults.
481 *
482 * TODO: We need to look up the faulty device based on the I/O VA. Use
483 * the IOMMU device for now.
484 */
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100485 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200486 return IRQ_HANDLED;
487
488 dev_err_ratelimited(mmu->dev,
489 "Unhandled fault: status 0x%08x iova 0x%08x\n",
490 status, iova);
491
492 return IRQ_HANDLED;
493}
494
495static irqreturn_t ipmmu_irq(int irq, void *dev)
496{
497 struct ipmmu_vmsa_device *mmu = dev;
Magnus Dammdbb70692017-05-17 19:06:38 +0900498 irqreturn_t status = IRQ_NONE;
499 unsigned int i;
500 unsigned long flags;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200501
Magnus Dammdbb70692017-05-17 19:06:38 +0900502 spin_lock_irqsave(&mmu->lock, flags);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200503
Magnus Dammdbb70692017-05-17 19:06:38 +0900504 /*
505 * Check interrupts for all active contexts.
506 */
507 for (i = 0; i < IPMMU_CTX_MAX; i++) {
508 if (!mmu->domains[i])
509 continue;
510 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
511 status = IRQ_HANDLED;
512 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200513
Magnus Dammdbb70692017-05-17 19:06:38 +0900514 spin_unlock_irqrestore(&mmu->lock, flags);
515
516 return status;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200517}
518
519/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200520 * IOMMU Operations
521 */
522
Magnus Damm8e73bf62017-05-17 19:06:59 +0900523static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200524{
525 struct ipmmu_vmsa_domain *domain;
526
527 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
528 if (!domain)
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100529 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200530
531 spin_lock_init(&domain->lock);
532
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100533 return &domain->io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200534}
535
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100536static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
537{
538 struct iommu_domain *io_domain = NULL;
539
540 switch (type) {
541 case IOMMU_DOMAIN_UNMANAGED:
542 io_domain = __ipmmu_domain_alloc(type);
543 break;
544
545 case IOMMU_DOMAIN_DMA:
546 io_domain = __ipmmu_domain_alloc(type);
547 if (io_domain && iommu_get_dma_cookie(io_domain)) {
548 kfree(io_domain);
549 io_domain = NULL;
550 }
551 break;
552 }
553
554 return io_domain;
555}
556
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100557static void ipmmu_domain_free(struct iommu_domain *io_domain)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200558{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100559 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200560
561 /*
562 * Free the domain resources. We assume that all devices have already
563 * been detached.
564 */
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100565 iommu_put_dma_cookie(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200566 ipmmu_domain_destroy_context(domain);
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200567 free_io_pgtable_ops(domain->iop);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200568 kfree(domain);
569}
570
571static int ipmmu_attach_device(struct iommu_domain *io_domain,
572 struct device *dev)
573{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900574 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Robin Murphye4efe4a2017-10-13 19:23:41 +0100575 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100576 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200577 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200578 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200579 int ret = 0;
580
Robin Murphye4efe4a2017-10-13 19:23:41 +0100581 if (!mmu) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200582 dev_err(dev, "Cannot attach to IPMMU\n");
583 return -ENXIO;
584 }
585
586 spin_lock_irqsave(&domain->lock, flags);
587
588 if (!domain->mmu) {
589 /* The domain hasn't been used yet, initialize it. */
590 domain->mmu = mmu;
591 ret = ipmmu_domain_init_context(domain);
592 } else if (domain->mmu != mmu) {
593 /*
594 * Something is wrong, we can't attach two devices using
595 * different IOMMUs to the same domain.
596 */
597 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
598 dev_name(mmu->dev), dev_name(domain->mmu->dev));
599 ret = -EINVAL;
Magnus Damm3ae47292017-05-17 19:07:10 +0900600 } else
601 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200602
603 spin_unlock_irqrestore(&domain->lock, flags);
604
605 if (ret < 0)
606 return ret;
607
Magnus Damm7b2d5962017-07-17 22:05:41 +0900608 for (i = 0; i < fwspec->num_ids; ++i)
609 ipmmu_utlb_enable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200610
611 return 0;
612}
613
614static void ipmmu_detach_device(struct iommu_domain *io_domain,
615 struct device *dev)
616{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900617 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100618 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pincharta166d312014-07-24 01:36:43 +0200619 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200620
Magnus Damm7b2d5962017-07-17 22:05:41 +0900621 for (i = 0; i < fwspec->num_ids; ++i)
622 ipmmu_utlb_disable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200623
624 /*
625 * TODO: Optimize by disabling the context when no device is attached.
626 */
627}
628
629static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
630 phys_addr_t paddr, size_t size, int prot)
631{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100632 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200633
634 if (!domain)
635 return -ENODEV;
636
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200637 return domain->iop->map(domain->iop, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200638}
639
640static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
641 size_t size)
642{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100643 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200644
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200645 return domain->iop->unmap(domain->iop, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200646}
647
Robin Murphy32b12442017-09-28 15:55:01 +0100648static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
649{
650 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
651
652 if (domain->mmu)
653 ipmmu_tlb_flush_all(domain);
654}
655
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200656static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
657 dma_addr_t iova)
658{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100659 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200660
661 /* TODO: Is locking needed ? */
662
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200663 return domain->iop->iova_to_phys(domain->iop, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200664}
665
Magnus Damm7b2d5962017-07-17 22:05:41 +0900666static int ipmmu_init_platform_device(struct device *dev,
667 struct of_phandle_args *args)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200668{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900669 struct platform_device *ipmmu_pdev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200670
Magnus Damm7b2d5962017-07-17 22:05:41 +0900671 ipmmu_pdev = of_find_device_by_node(args->np);
672 if (!ipmmu_pdev)
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200673 return -ENODEV;
674
Robin Murphye4efe4a2017-10-13 19:23:41 +0100675 dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900676 return 0;
Magnus Damm383fef5f2017-05-17 19:06:48 +0900677}
678
Magnus Damm49558da2017-07-17 22:05:20 +0900679static int ipmmu_of_xlate(struct device *dev,
680 struct of_phandle_args *spec)
681{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900682 iommu_fwspec_add_ids(dev, spec->args, 1);
683
Magnus Damm49558da2017-07-17 22:05:20 +0900684 /* Initialize once - xlate() will call multiple times */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100685 if (to_ipmmu(dev))
Magnus Damm49558da2017-07-17 22:05:20 +0900686 return 0;
687
Magnus Damm7b2d5962017-07-17 22:05:41 +0900688 return ipmmu_init_platform_device(dev, spec);
Magnus Damm49558da2017-07-17 22:05:20 +0900689}
690
Robin Murphy49c875f2017-10-13 19:23:42 +0100691static int ipmmu_init_arm_mapping(struct device *dev)
Magnus Damm383fef5f2017-05-17 19:06:48 +0900692{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100693 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900694 struct iommu_group *group;
695 int ret;
696
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200697 /* Create a device group and add the device to it. */
698 group = iommu_group_alloc();
699 if (IS_ERR(group)) {
700 dev_err(dev, "Failed to allocate IOMMU group\n");
Robin Murphy49c875f2017-10-13 19:23:42 +0100701 return PTR_ERR(group);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200702 }
703
704 ret = iommu_group_add_device(group, dev);
705 iommu_group_put(group);
706
707 if (ret < 0) {
708 dev_err(dev, "Failed to add device to IPMMU group\n");
Robin Murphy49c875f2017-10-13 19:23:42 +0100709 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200710 }
711
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200712 /*
713 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
714 * VAs. This will allocate a corresponding IOMMU domain.
715 *
716 * TODO:
717 * - Create one mapping per context (TLB).
718 * - Make the mapping size configurable ? We currently use a 2GB mapping
719 * at a 1GB offset to ensure that NULL VAs will fault.
720 */
721 if (!mmu->mapping) {
722 struct dma_iommu_mapping *mapping;
723
724 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +0200725 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200726 if (IS_ERR(mapping)) {
727 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +0100728 ret = PTR_ERR(mapping);
729 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200730 }
731
732 mmu->mapping = mapping;
733 }
734
735 /* Attach the ARM VA mapping to the device. */
736 ret = arm_iommu_attach_device(dev, mmu->mapping);
737 if (ret < 0) {
738 dev_err(dev, "Failed to attach device to VA mapping\n");
739 goto error;
740 }
741
742 return 0;
743
744error:
Robin Murphy49c875f2017-10-13 19:23:42 +0100745 iommu_group_remove_device(dev);
746 if (mmu->mapping)
Magnus Damm383fef5f2017-05-17 19:06:48 +0900747 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +0200748
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200749 return ret;
750}
751
Robin Murphy49c875f2017-10-13 19:23:42 +0100752static int ipmmu_add_device(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900753{
Magnus Damm3ae47292017-05-17 19:07:10 +0900754 struct iommu_group *group;
755
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900756 /*
757 * Only let through devices that have been verified in xlate()
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900758 */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100759 if (!to_ipmmu(dev))
Magnus Damm3ae47292017-05-17 19:07:10 +0900760 return -ENODEV;
761
Robin Murphy49c875f2017-10-13 19:23:42 +0100762 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
763 return ipmmu_init_arm_mapping(dev);
764
Magnus Damm3ae47292017-05-17 19:07:10 +0900765 group = iommu_group_get_for_dev(dev);
766 if (IS_ERR(group))
767 return PTR_ERR(group);
768
Robin Murphy49c875f2017-10-13 19:23:42 +0100769 iommu_group_put(group);
Magnus Damm3ae47292017-05-17 19:07:10 +0900770 return 0;
771}
772
Robin Murphy49c875f2017-10-13 19:23:42 +0100773static void ipmmu_remove_device(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900774{
Robin Murphy49c875f2017-10-13 19:23:42 +0100775 arm_iommu_detach_device(dev);
Magnus Damm3ae47292017-05-17 19:07:10 +0900776 iommu_group_remove_device(dev);
777}
778
Robin Murphyb354c732017-10-13 19:23:40 +0100779static struct iommu_group *ipmmu_find_group(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900780{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100781 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm3ae47292017-05-17 19:07:10 +0900782 struct iommu_group *group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900783
Robin Murphye4efe4a2017-10-13 19:23:41 +0100784 if (mmu->group)
785 return iommu_group_ref_get(mmu->group);
Robin Murphyb354c732017-10-13 19:23:40 +0100786
787 group = iommu_group_alloc();
788 if (!IS_ERR(group))
Robin Murphye4efe4a2017-10-13 19:23:41 +0100789 mmu->group = group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900790
791 return group;
792}
793
Magnus Damm3ae47292017-05-17 19:07:10 +0900794static const struct iommu_ops ipmmu_ops = {
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100795 .domain_alloc = ipmmu_domain_alloc,
796 .domain_free = ipmmu_domain_free,
Magnus Damm3ae47292017-05-17 19:07:10 +0900797 .attach_dev = ipmmu_attach_device,
798 .detach_dev = ipmmu_detach_device,
799 .map = ipmmu_map,
800 .unmap = ipmmu_unmap,
Robin Murphy32b12442017-09-28 15:55:01 +0100801 .flush_iotlb_all = ipmmu_iotlb_sync,
802 .iotlb_sync = ipmmu_iotlb_sync,
Magnus Damm3ae47292017-05-17 19:07:10 +0900803 .map_sg = default_iommu_map_sg,
804 .iova_to_phys = ipmmu_iova_to_phys,
Robin Murphy49c875f2017-10-13 19:23:42 +0100805 .add_device = ipmmu_add_device,
806 .remove_device = ipmmu_remove_device,
Robin Murphyb354c732017-10-13 19:23:40 +0100807 .device_group = ipmmu_find_group,
Magnus Damm3ae47292017-05-17 19:07:10 +0900808 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Magnus Damm49558da2017-07-17 22:05:20 +0900809 .of_xlate = ipmmu_of_xlate,
Magnus Damm3ae47292017-05-17 19:07:10 +0900810};
811
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200812/* -----------------------------------------------------------------------------
813 * Probe/remove and init
814 */
815
816static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
817{
818 unsigned int i;
819
820 /* Disable all contexts. */
821 for (i = 0; i < 4; ++i)
822 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
823}
824
Magnus Damm33f3ac92017-10-16 21:29:25 +0900825static const struct ipmmu_features ipmmu_features_default = {
826 .use_ns_alias_offset = true,
827};
828
829static const struct of_device_id ipmmu_of_ids[] = {
830 {
831 .compatible = "renesas,ipmmu-vmsa",
832 .data = &ipmmu_features_default,
833 }, {
834 /* Terminator */
835 },
836};
837
838MODULE_DEVICE_TABLE(of, ipmmu_of_ids);
839
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200840static int ipmmu_probe(struct platform_device *pdev)
841{
842 struct ipmmu_vmsa_device *mmu;
843 struct resource *res;
844 int irq;
845 int ret;
846
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200847 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
848 if (!mmu) {
849 dev_err(&pdev->dev, "cannot allocate device data\n");
850 return -ENOMEM;
851 }
852
853 mmu->dev = &pdev->dev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200854 mmu->num_utlbs = 32;
Magnus Dammdbb70692017-05-17 19:06:38 +0900855 spin_lock_init(&mmu->lock);
856 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
Magnus Damm33f3ac92017-10-16 21:29:25 +0900857 mmu->features = of_device_get_match_data(&pdev->dev);
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 */
Magnus Damm33f3ac92017-10-16 21:29:25 +0900877 if (mmu->features->use_ns_alias_offset)
878 mmu->base += IM_NS_ALIAS_OFFSET;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100879
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200880 irq = platform_get_irq(pdev, 0);
881 if (irq < 0) {
882 dev_err(&pdev->dev, "no IRQ found\n");
883 return irq;
884 }
885
886 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
887 dev_name(&pdev->dev), mmu);
888 if (ret < 0) {
889 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +0800890 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200891 }
892
893 ipmmu_device_reset(mmu);
894
Magnus Damm7af9a5f2017-08-21 14:53:35 +0900895 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
896 dev_name(&pdev->dev));
897 if (ret)
898 return ret;
899
Magnus Damm01da21e2017-07-17 22:05:10 +0900900 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
901 iommu_device_set_fwnode(&mmu->iommu, &pdev->dev.of_node->fwnode);
902
903 ret = iommu_device_register(&mmu->iommu);
904 if (ret)
905 return ret;
906
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200907 /*
908 * We can't create the ARM mapping here as it requires the bus to have
909 * an IOMMU, which only happens when bus_set_iommu() is called in
910 * ipmmu_init() after the probe function returns.
911 */
912
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200913 platform_set_drvdata(pdev, mmu);
914
915 return 0;
916}
917
918static int ipmmu_remove(struct platform_device *pdev)
919{
920 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
921
Magnus Damm7af9a5f2017-08-21 14:53:35 +0900922 iommu_device_sysfs_remove(&mmu->iommu);
Magnus Damm01da21e2017-07-17 22:05:10 +0900923 iommu_device_unregister(&mmu->iommu);
924
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200925 arm_iommu_release_mapping(mmu->mapping);
926
927 ipmmu_device_reset(mmu);
928
929 return 0;
930}
931
932static struct platform_driver ipmmu_driver = {
933 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200934 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +0100935 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200936 },
937 .probe = ipmmu_probe,
938 .remove = ipmmu_remove,
939};
940
941static int __init ipmmu_init(void)
942{
943 int ret;
944
945 ret = platform_driver_register(&ipmmu_driver);
946 if (ret < 0)
947 return ret;
948
949 if (!iommu_present(&platform_bus_type))
950 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
951
952 return 0;
953}
954
955static void __exit ipmmu_exit(void)
956{
957 return platform_driver_unregister(&ipmmu_driver);
958}
959
960subsys_initcall(ipmmu_init);
961module_exit(ipmmu_exit);
962
963MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
964MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
965MODULE_LICENSE("GPL v2");