blob: fd05a5f5a47e986f3a5de33bf66327a9e394606e [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 Damm7b2d5962017-07-17 22:05:41 +090022#include <linux/of_platform.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020023#include <linux/platform_device.h>
24#include <linux/sizes.h>
25#include <linux/slab.h>
26
Magnus Damm3ae47292017-05-17 19:07:10 +090027#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020028#include <asm/dma-iommu.h>
29#include <asm/pgalloc.h>
Magnus Damm3ae47292017-05-17 19:07:10 +090030#endif
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020031
Laurent Pinchartf20ed392015-01-20 18:30:04 +020032#include "io-pgtable.h"
33
Magnus Dammdbb70692017-05-17 19:06:38 +090034#define IPMMU_CTX_MAX 1
35
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020036struct ipmmu_vmsa_device {
37 struct device *dev;
38 void __iomem *base;
Magnus Damm01da21e2017-07-17 22:05:10 +090039 struct iommu_device iommu;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020040
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020041 unsigned int num_utlbs;
Magnus Dammdbb70692017-05-17 19:06:38 +090042 spinlock_t lock; /* Protects ctx and domains[] */
43 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
44 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020045
Robin Murphyb354c732017-10-13 19:23:40 +010046 struct iommu_group *group;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020047 struct dma_iommu_mapping *mapping;
48};
49
50struct ipmmu_vmsa_domain {
51 struct ipmmu_vmsa_device *mmu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +010052 struct iommu_domain io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020053
Laurent Pinchartf20ed392015-01-20 18:30:04 +020054 struct io_pgtable_cfg cfg;
55 struct io_pgtable_ops *iop;
56
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020057 unsigned int context_id;
58 spinlock_t lock; /* Protects mappings */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020059};
60
Joerg Roedel5914c5f2015-03-26 13:43:16 +010061static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
62{
63 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
64}
65
Robin Murphye4efe4a2017-10-13 19:23:41 +010066static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
Magnus Damm0fbc8b02017-05-17 19:07:20 +090067{
Robin Murphy3c49ed32017-07-17 22:05:31 +090068 return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL;
Magnus Damm0fbc8b02017-05-17 19:07:20 +090069}
70
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020071#define TLB_LOOP_TIMEOUT 100 /* 100us */
72
73/* -----------------------------------------------------------------------------
74 * Registers Definition
75 */
76
Laurent Pinchart275f5052014-03-17 01:02:46 +010077#define IM_NS_ALIAS_OFFSET 0x800
78
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020079#define IM_CTX_SIZE 0x40
80
81#define IMCTR 0x0000
82#define IMCTR_TRE (1 << 17)
83#define IMCTR_AFE (1 << 16)
84#define IMCTR_RTSEL_MASK (3 << 4)
85#define IMCTR_RTSEL_SHIFT 4
86#define IMCTR_TREN (1 << 3)
87#define IMCTR_INTEN (1 << 2)
88#define IMCTR_FLUSH (1 << 1)
89#define IMCTR_MMUEN (1 << 0)
90
91#define IMCAAR 0x0004
92
93#define IMTTBCR 0x0008
94#define IMTTBCR_EAE (1 << 31)
95#define IMTTBCR_PMB (1 << 30)
96#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
97#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
98#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
99#define IMTTBCR_SH1_MASK (3 << 28)
100#define IMTTBCR_ORGN1_NC (0 << 26)
101#define IMTTBCR_ORGN1_WB_WA (1 << 26)
102#define IMTTBCR_ORGN1_WT (2 << 26)
103#define IMTTBCR_ORGN1_WB (3 << 26)
104#define IMTTBCR_ORGN1_MASK (3 << 26)
105#define IMTTBCR_IRGN1_NC (0 << 24)
106#define IMTTBCR_IRGN1_WB_WA (1 << 24)
107#define IMTTBCR_IRGN1_WT (2 << 24)
108#define IMTTBCR_IRGN1_WB (3 << 24)
109#define IMTTBCR_IRGN1_MASK (3 << 24)
110#define IMTTBCR_TSZ1_MASK (7 << 16)
111#define IMTTBCR_TSZ1_SHIFT 16
112#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
113#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
114#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
115#define IMTTBCR_SH0_MASK (3 << 12)
116#define IMTTBCR_ORGN0_NC (0 << 10)
117#define IMTTBCR_ORGN0_WB_WA (1 << 10)
118#define IMTTBCR_ORGN0_WT (2 << 10)
119#define IMTTBCR_ORGN0_WB (3 << 10)
120#define IMTTBCR_ORGN0_MASK (3 << 10)
121#define IMTTBCR_IRGN0_NC (0 << 8)
122#define IMTTBCR_IRGN0_WB_WA (1 << 8)
123#define IMTTBCR_IRGN0_WT (2 << 8)
124#define IMTTBCR_IRGN0_WB (3 << 8)
125#define IMTTBCR_IRGN0_MASK (3 << 8)
126#define IMTTBCR_SL0_LVL_2 (0 << 4)
127#define IMTTBCR_SL0_LVL_1 (1 << 4)
128#define IMTTBCR_TSZ0_MASK (7 << 0)
129#define IMTTBCR_TSZ0_SHIFT O
130
131#define IMBUSCR 0x000c
132#define IMBUSCR_DVM (1 << 2)
133#define IMBUSCR_BUSSEL_SYS (0 << 0)
134#define IMBUSCR_BUSSEL_CCI (1 << 0)
135#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
136#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
137#define IMBUSCR_BUSSEL_MASK (3 << 0)
138
139#define IMTTLBR0 0x0010
140#define IMTTUBR0 0x0014
141#define IMTTLBR1 0x0018
142#define IMTTUBR1 0x001c
143
144#define IMSTR 0x0020
145#define IMSTR_ERRLVL_MASK (3 << 12)
146#define IMSTR_ERRLVL_SHIFT 12
147#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
148#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
149#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
150#define IMSTR_ERRCODE_MASK (7 << 8)
151#define IMSTR_MHIT (1 << 4)
152#define IMSTR_ABORT (1 << 2)
153#define IMSTR_PF (1 << 1)
154#define IMSTR_TF (1 << 0)
155
156#define IMMAIR0 0x0028
157#define IMMAIR1 0x002c
158#define IMMAIR_ATTR_MASK 0xff
159#define IMMAIR_ATTR_DEVICE 0x04
160#define IMMAIR_ATTR_NC 0x44
161#define IMMAIR_ATTR_WBRWA 0xff
162#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
163#define IMMAIR_ATTR_IDX_NC 0
164#define IMMAIR_ATTR_IDX_WBRWA 1
165#define IMMAIR_ATTR_IDX_DEV 2
166
167#define IMEAR 0x0030
168
169#define IMPCTR 0x0200
170#define IMPSTR 0x0208
171#define IMPEAR 0x020c
172#define IMPMBA(n) (0x0280 + ((n) * 4))
173#define IMPMBD(n) (0x02c0 + ((n) * 4))
174
175#define IMUCTR(n) (0x0300 + ((n) * 16))
176#define IMUCTR_FIXADDEN (1 << 31)
177#define IMUCTR_FIXADD_MASK (0xff << 16)
178#define IMUCTR_FIXADD_SHIFT 16
179#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
180#define IMUCTR_TTSEL_PMB (8 << 4)
181#define IMUCTR_TTSEL_MASK (15 << 4)
182#define IMUCTR_FLUSH (1 << 1)
183#define IMUCTR_MMUEN (1 << 0)
184
185#define IMUASID(n) (0x0308 + ((n) * 16))
186#define IMUASID_ASID8_MASK (0xff << 8)
187#define IMUASID_ASID8_SHIFT 8
188#define IMUASID_ASID0_MASK (0xff << 0)
189#define IMUASID_ASID0_SHIFT 0
190
191/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200192 * Read/Write Access
193 */
194
195static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
196{
197 return ioread32(mmu->base + offset);
198}
199
200static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
201 u32 data)
202{
203 iowrite32(data, mmu->base + offset);
204}
205
206static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
207{
208 return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
209}
210
211static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
212 u32 data)
213{
214 ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
215}
216
217/* -----------------------------------------------------------------------------
218 * TLB and microTLB Management
219 */
220
221/* Wait for any pending TLB invalidations to complete */
222static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
223{
224 unsigned int count = 0;
225
226 while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
227 cpu_relax();
228 if (++count == TLB_LOOP_TIMEOUT) {
229 dev_err_ratelimited(domain->mmu->dev,
230 "TLB sync timed out -- MMU may be deadlocked\n");
231 return;
232 }
233 udelay(1);
234 }
235}
236
237static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
238{
239 u32 reg;
240
241 reg = ipmmu_ctx_read(domain, IMCTR);
242 reg |= IMCTR_FLUSH;
243 ipmmu_ctx_write(domain, IMCTR, reg);
244
245 ipmmu_tlb_sync(domain);
246}
247
248/*
249 * Enable MMU translation for the microTLB.
250 */
251static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200252 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200253{
254 struct ipmmu_vmsa_device *mmu = domain->mmu;
255
Laurent Pinchart192d2042014-05-15 12:40:42 +0200256 /*
257 * TODO: Reference-count the microTLB as several bus masters can be
258 * connected to the same microTLB.
259 */
260
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200261 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200262 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200263 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200264 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200265 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
266 IMUCTR_MMUEN);
267}
268
269/*
270 * Disable MMU translation for the microTLB.
271 */
272static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200273 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200274{
275 struct ipmmu_vmsa_device *mmu = domain->mmu;
276
Laurent Pinchart192d2042014-05-15 12:40:42 +0200277 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200278}
279
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200280static void ipmmu_tlb_flush_all(void *cookie)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200281{
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200282 struct ipmmu_vmsa_domain *domain = cookie;
283
284 ipmmu_tlb_invalidate(domain);
285}
286
Robin Murphy06c610e2015-12-07 18:18:53 +0000287static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
288 size_t granule, bool leaf, void *cookie)
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200289{
290 /* The hardware doesn't support selective TLB flush. */
291}
292
Bhumika Goyal8da4af92017-08-28 23:47:27 +0530293static const struct iommu_gather_ops ipmmu_gather_ops = {
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200294 .tlb_flush_all = ipmmu_tlb_flush_all,
295 .tlb_add_flush = ipmmu_tlb_add_flush,
296 .tlb_sync = ipmmu_tlb_flush_all,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200297};
298
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200299/* -----------------------------------------------------------------------------
300 * Domain/Context Management
301 */
302
Magnus Dammdbb70692017-05-17 19:06:38 +0900303static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
304 struct ipmmu_vmsa_domain *domain)
305{
306 unsigned long flags;
307 int ret;
308
309 spin_lock_irqsave(&mmu->lock, flags);
310
311 ret = find_first_zero_bit(mmu->ctx, IPMMU_CTX_MAX);
312 if (ret != IPMMU_CTX_MAX) {
313 mmu->domains[ret] = domain;
314 set_bit(ret, mmu->ctx);
315 }
316
317 spin_unlock_irqrestore(&mmu->lock, flags);
318
319 return ret;
320}
321
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300322static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
323 unsigned int context_id)
324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&mmu->lock, flags);
328
329 clear_bit(context_id, mmu->ctx);
330 mmu->domains[context_id] = NULL;
331
332 spin_unlock_irqrestore(&mmu->lock, flags);
333}
334
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200335static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
336{
Geert Uytterhoevenf64232e2015-12-22 20:01:06 +0100337 u64 ttbr;
Magnus Dammdbb70692017-05-17 19:06:38 +0900338 int ret;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200339
340 /*
341 * Allocate the page table operations.
342 *
343 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
344 * access, Long-descriptor format" that the NStable bit being set in a
345 * table descriptor will result in the NStable and NS bits of all child
346 * entries being ignored and considered as being set. The IPMMU seems
347 * not to comply with this, as it generates a secure access page fault
348 * if any of the NStable and NS bits isn't set when running in
349 * non-secure mode.
350 */
351 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
Magnus Damm26b6aec2017-05-17 19:07:41 +0900352 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200353 domain->cfg.ias = 32;
354 domain->cfg.oas = 40;
355 domain->cfg.tlb = &ipmmu_gather_ops;
Geert Uytterhoeven3b6bb5b2017-01-31 12:17:07 +0100356 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
357 domain->io_domain.geometry.force_aperture = true;
Robin Murphyff2ed962015-07-29 19:46:08 +0100358 /*
359 * TODO: Add support for coherent walk through CCI with DVM and remove
360 * cache handling. For now, delegate it to the io-pgtable code.
361 */
362 domain->cfg.iommu_dev = domain->mmu->dev;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200363
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200364 /*
Magnus Dammdbb70692017-05-17 19:06:38 +0900365 * Find an unused context.
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200366 */
Magnus Dammdbb70692017-05-17 19:06:38 +0900367 ret = ipmmu_domain_allocate_context(domain->mmu, domain);
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300368 if (ret == IPMMU_CTX_MAX)
Magnus Dammdbb70692017-05-17 19:06:38 +0900369 return -EBUSY;
Magnus Dammdbb70692017-05-17 19:06:38 +0900370
371 domain->context_id = ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200372
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300373 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
374 domain);
375 if (!domain->iop) {
376 ipmmu_domain_free_context(domain->mmu, domain->context_id);
377 return -EINVAL;
378 }
379
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200380 /* TTBR0 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200381 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200382 ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
383 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
384
385 /*
386 * TTBCR
387 * We use long descriptors with inner-shareable WBWA tables and allocate
388 * the whole 32-bit VA space to TTBR0.
389 */
390 ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
391 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
392 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
393
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200394 /* MAIR0 */
395 ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200396
397 /* IMBUSCR */
398 ipmmu_ctx_write(domain, IMBUSCR,
399 ipmmu_ctx_read(domain, IMBUSCR) &
400 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
401
402 /*
403 * IMSTR
404 * Clear all interrupt flags.
405 */
406 ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
407
408 /*
409 * IMCTR
410 * Enable the MMU and interrupt generation. The long-descriptor
411 * translation table format doesn't use TEX remapping. Don't enable AF
412 * software management as we have no use for it. Flush the TLB as
413 * required when modifying the context registers.
414 */
415 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
416
417 return 0;
418}
419
420static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
421{
422 /*
423 * Disable the context. Flush the TLB as required when modifying the
424 * context registers.
425 *
426 * TODO: Is TLB flush really needed ?
427 */
428 ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
429 ipmmu_tlb_sync(domain);
Magnus Dammdbb70692017-05-17 19:06:38 +0900430 ipmmu_domain_free_context(domain->mmu, domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200431}
432
433/* -----------------------------------------------------------------------------
434 * Fault Handling
435 */
436
437static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
438{
439 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
440 struct ipmmu_vmsa_device *mmu = domain->mmu;
441 u32 status;
442 u32 iova;
443
444 status = ipmmu_ctx_read(domain, IMSTR);
445 if (!(status & err_mask))
446 return IRQ_NONE;
447
448 iova = ipmmu_ctx_read(domain, IMEAR);
449
450 /*
451 * Clear the error status flags. Unlike traditional interrupt flag
452 * registers that must be cleared by writing 1, this status register
453 * seems to require 0. The error address register must be read before,
454 * otherwise its value will be 0.
455 */
456 ipmmu_ctx_write(domain, IMSTR, 0);
457
458 /* Log fatal errors. */
459 if (status & IMSTR_MHIT)
460 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
461 iova);
462 if (status & IMSTR_ABORT)
463 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
464 iova);
465
466 if (!(status & (IMSTR_PF | IMSTR_TF)))
467 return IRQ_NONE;
468
469 /*
470 * Try to handle page faults and translation faults.
471 *
472 * TODO: We need to look up the faulty device based on the I/O VA. Use
473 * the IOMMU device for now.
474 */
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100475 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200476 return IRQ_HANDLED;
477
478 dev_err_ratelimited(mmu->dev,
479 "Unhandled fault: status 0x%08x iova 0x%08x\n",
480 status, iova);
481
482 return IRQ_HANDLED;
483}
484
485static irqreturn_t ipmmu_irq(int irq, void *dev)
486{
487 struct ipmmu_vmsa_device *mmu = dev;
Magnus Dammdbb70692017-05-17 19:06:38 +0900488 irqreturn_t status = IRQ_NONE;
489 unsigned int i;
490 unsigned long flags;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200491
Magnus Dammdbb70692017-05-17 19:06:38 +0900492 spin_lock_irqsave(&mmu->lock, flags);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200493
Magnus Dammdbb70692017-05-17 19:06:38 +0900494 /*
495 * Check interrupts for all active contexts.
496 */
497 for (i = 0; i < IPMMU_CTX_MAX; i++) {
498 if (!mmu->domains[i])
499 continue;
500 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
501 status = IRQ_HANDLED;
502 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200503
Magnus Dammdbb70692017-05-17 19:06:38 +0900504 spin_unlock_irqrestore(&mmu->lock, flags);
505
506 return status;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200507}
508
509/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200510 * IOMMU Operations
511 */
512
Magnus Damm8e73bf62017-05-17 19:06:59 +0900513static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200514{
515 struct ipmmu_vmsa_domain *domain;
516
517 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
518 if (!domain)
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100519 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200520
521 spin_lock_init(&domain->lock);
522
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100523 return &domain->io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200524}
525
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100526static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
527{
528 struct iommu_domain *io_domain = NULL;
529
530 switch (type) {
531 case IOMMU_DOMAIN_UNMANAGED:
532 io_domain = __ipmmu_domain_alloc(type);
533 break;
534
535 case IOMMU_DOMAIN_DMA:
536 io_domain = __ipmmu_domain_alloc(type);
537 if (io_domain && iommu_get_dma_cookie(io_domain)) {
538 kfree(io_domain);
539 io_domain = NULL;
540 }
541 break;
542 }
543
544 return io_domain;
545}
546
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100547static void ipmmu_domain_free(struct iommu_domain *io_domain)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200548{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100549 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200550
551 /*
552 * Free the domain resources. We assume that all devices have already
553 * been detached.
554 */
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100555 iommu_put_dma_cookie(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200556 ipmmu_domain_destroy_context(domain);
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200557 free_io_pgtable_ops(domain->iop);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200558 kfree(domain);
559}
560
561static int ipmmu_attach_device(struct iommu_domain *io_domain,
562 struct device *dev)
563{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900564 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Robin Murphye4efe4a2017-10-13 19:23:41 +0100565 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100566 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200567 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200568 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200569 int ret = 0;
570
Robin Murphye4efe4a2017-10-13 19:23:41 +0100571 if (!mmu) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200572 dev_err(dev, "Cannot attach to IPMMU\n");
573 return -ENXIO;
574 }
575
576 spin_lock_irqsave(&domain->lock, flags);
577
578 if (!domain->mmu) {
579 /* The domain hasn't been used yet, initialize it. */
580 domain->mmu = mmu;
581 ret = ipmmu_domain_init_context(domain);
582 } else if (domain->mmu != mmu) {
583 /*
584 * Something is wrong, we can't attach two devices using
585 * different IOMMUs to the same domain.
586 */
587 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
588 dev_name(mmu->dev), dev_name(domain->mmu->dev));
589 ret = -EINVAL;
Magnus Damm3ae47292017-05-17 19:07:10 +0900590 } else
591 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200592
593 spin_unlock_irqrestore(&domain->lock, flags);
594
595 if (ret < 0)
596 return ret;
597
Magnus Damm7b2d5962017-07-17 22:05:41 +0900598 for (i = 0; i < fwspec->num_ids; ++i)
599 ipmmu_utlb_enable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200600
601 return 0;
602}
603
604static void ipmmu_detach_device(struct iommu_domain *io_domain,
605 struct device *dev)
606{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900607 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100608 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pincharta166d312014-07-24 01:36:43 +0200609 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200610
Magnus Damm7b2d5962017-07-17 22:05:41 +0900611 for (i = 0; i < fwspec->num_ids; ++i)
612 ipmmu_utlb_disable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200613
614 /*
615 * TODO: Optimize by disabling the context when no device is attached.
616 */
617}
618
619static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
620 phys_addr_t paddr, size_t size, int prot)
621{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100622 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200623
624 if (!domain)
625 return -ENODEV;
626
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200627 return domain->iop->map(domain->iop, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200628}
629
630static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
631 size_t size)
632{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100633 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200634
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200635 return domain->iop->unmap(domain->iop, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200636}
637
Robin Murphy32b12442017-09-28 15:55:01 +0100638static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
639{
640 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
641
642 if (domain->mmu)
643 ipmmu_tlb_flush_all(domain);
644}
645
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200646static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
647 dma_addr_t iova)
648{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100649 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200650
651 /* TODO: Is locking needed ? */
652
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200653 return domain->iop->iova_to_phys(domain->iop, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200654}
655
Magnus Damm7b2d5962017-07-17 22:05:41 +0900656static int ipmmu_init_platform_device(struct device *dev,
657 struct of_phandle_args *args)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200658{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900659 struct platform_device *ipmmu_pdev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200660
Magnus Damm7b2d5962017-07-17 22:05:41 +0900661 ipmmu_pdev = of_find_device_by_node(args->np);
662 if (!ipmmu_pdev)
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200663 return -ENODEV;
664
Robin Murphye4efe4a2017-10-13 19:23:41 +0100665 dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900666 return 0;
Magnus Damm383fef5f2017-05-17 19:06:48 +0900667}
668
Magnus Damm49558da2017-07-17 22:05:20 +0900669static int ipmmu_of_xlate(struct device *dev,
670 struct of_phandle_args *spec)
671{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900672 iommu_fwspec_add_ids(dev, spec->args, 1);
673
Magnus Damm49558da2017-07-17 22:05:20 +0900674 /* Initialize once - xlate() will call multiple times */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100675 if (to_ipmmu(dev))
Magnus Damm49558da2017-07-17 22:05:20 +0900676 return 0;
677
Magnus Damm7b2d5962017-07-17 22:05:41 +0900678 return ipmmu_init_platform_device(dev, spec);
Magnus Damm49558da2017-07-17 22:05:20 +0900679}
680
Magnus Damm3ae47292017-05-17 19:07:10 +0900681#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
682
Magnus Damm383fef5f2017-05-17 19:06:48 +0900683static int ipmmu_add_device(struct device *dev)
684{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100685 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900686 struct iommu_group *group;
687 int ret;
688
Magnus Damm49558da2017-07-17 22:05:20 +0900689 /*
690 * Only let through devices that have been verified in xlate()
691 */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100692 if (!mmu)
Magnus Damm49558da2017-07-17 22:05:20 +0900693 return -ENODEV;
Magnus Damm383fef5f2017-05-17 19:06:48 +0900694
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200695 /* Create a device group and add the device to it. */
696 group = iommu_group_alloc();
697 if (IS_ERR(group)) {
698 dev_err(dev, "Failed to allocate IOMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200699 ret = PTR_ERR(group);
700 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200701 }
702
703 ret = iommu_group_add_device(group, dev);
704 iommu_group_put(group);
705
706 if (ret < 0) {
707 dev_err(dev, "Failed to add device to IPMMU group\n");
Laurent Pincharta166d312014-07-24 01:36:43 +0200708 group = NULL;
709 goto error;
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:
Magnus Damm383fef5f2017-05-17 19:06:48 +0900745 if (mmu)
746 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +0200747
748 if (!IS_ERR_OR_NULL(group))
749 iommu_group_remove_device(dev);
750
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200751 return ret;
752}
753
754static void ipmmu_remove_device(struct device *dev)
755{
756 arm_iommu_detach_device(dev);
757 iommu_group_remove_device(dev);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200758}
759
Thierry Redingb22f6432014-06-27 09:03:12 +0200760static const struct iommu_ops ipmmu_ops = {
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100761 .domain_alloc = ipmmu_domain_alloc,
762 .domain_free = ipmmu_domain_free,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200763 .attach_dev = ipmmu_attach_device,
764 .detach_dev = ipmmu_detach_device,
765 .map = ipmmu_map,
766 .unmap = ipmmu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -0700767 .map_sg = default_iommu_map_sg,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200768 .iova_to_phys = ipmmu_iova_to_phys,
769 .add_device = ipmmu_add_device,
770 .remove_device = ipmmu_remove_device,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200771 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Magnus Damm49558da2017-07-17 22:05:20 +0900772 .of_xlate = ipmmu_of_xlate,
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200773};
774
Magnus Damm3ae47292017-05-17 19:07:10 +0900775#endif /* !CONFIG_ARM && CONFIG_IOMMU_DMA */
776
777#ifdef CONFIG_IOMMU_DMA
778
Magnus Damm3ae47292017-05-17 19:07:10 +0900779static int ipmmu_add_device_dma(struct device *dev)
780{
Magnus Damm3ae47292017-05-17 19:07:10 +0900781 struct iommu_group *group;
782
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900783 /*
784 * Only let through devices that have been verified in xlate()
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900785 */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100786 if (!to_ipmmu(dev))
Magnus Damm3ae47292017-05-17 19:07:10 +0900787 return -ENODEV;
788
789 group = iommu_group_get_for_dev(dev);
790 if (IS_ERR(group))
791 return PTR_ERR(group);
792
Magnus Damm3ae47292017-05-17 19:07:10 +0900793 return 0;
794}
795
796static void ipmmu_remove_device_dma(struct device *dev)
797{
Magnus Damm3ae47292017-05-17 19:07:10 +0900798 iommu_group_remove_device(dev);
799}
800
Robin Murphyb354c732017-10-13 19:23:40 +0100801static struct iommu_group *ipmmu_find_group(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900802{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100803 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm3ae47292017-05-17 19:07:10 +0900804 struct iommu_group *group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900805
Robin Murphye4efe4a2017-10-13 19:23:41 +0100806 if (mmu->group)
807 return iommu_group_ref_get(mmu->group);
Robin Murphyb354c732017-10-13 19:23:40 +0100808
809 group = iommu_group_alloc();
810 if (!IS_ERR(group))
Robin Murphye4efe4a2017-10-13 19:23:41 +0100811 mmu->group = group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900812
813 return group;
814}
815
Magnus Damm3ae47292017-05-17 19:07:10 +0900816static const struct iommu_ops ipmmu_ops = {
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100817 .domain_alloc = ipmmu_domain_alloc,
818 .domain_free = ipmmu_domain_free,
Magnus Damm3ae47292017-05-17 19:07:10 +0900819 .attach_dev = ipmmu_attach_device,
820 .detach_dev = ipmmu_detach_device,
821 .map = ipmmu_map,
822 .unmap = ipmmu_unmap,
Robin Murphy32b12442017-09-28 15:55:01 +0100823 .flush_iotlb_all = ipmmu_iotlb_sync,
824 .iotlb_sync = ipmmu_iotlb_sync,
Magnus Damm3ae47292017-05-17 19:07:10 +0900825 .map_sg = default_iommu_map_sg,
826 .iova_to_phys = ipmmu_iova_to_phys,
827 .add_device = ipmmu_add_device_dma,
828 .remove_device = ipmmu_remove_device_dma,
Robin Murphyb354c732017-10-13 19:23:40 +0100829 .device_group = ipmmu_find_group,
Magnus Damm3ae47292017-05-17 19:07:10 +0900830 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Magnus Damm49558da2017-07-17 22:05:20 +0900831 .of_xlate = ipmmu_of_xlate,
Magnus Damm3ae47292017-05-17 19:07:10 +0900832};
833
834#endif /* CONFIG_IOMMU_DMA */
835
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200836/* -----------------------------------------------------------------------------
837 * Probe/remove and init
838 */
839
840static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
841{
842 unsigned int i;
843
844 /* Disable all contexts. */
845 for (i = 0; i < 4; ++i)
846 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
847}
848
849static int ipmmu_probe(struct platform_device *pdev)
850{
851 struct ipmmu_vmsa_device *mmu;
852 struct resource *res;
853 int irq;
854 int ret;
855
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200856 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
857 if (!mmu) {
858 dev_err(&pdev->dev, "cannot allocate device data\n");
859 return -ENOMEM;
860 }
861
862 mmu->dev = &pdev->dev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200863 mmu->num_utlbs = 32;
Magnus Dammdbb70692017-05-17 19:06:38 +0900864 spin_lock_init(&mmu->lock);
865 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200866
867 /* Map I/O memory and request IRQ. */
868 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
869 mmu->base = devm_ioremap_resource(&pdev->dev, res);
870 if (IS_ERR(mmu->base))
871 return PTR_ERR(mmu->base);
872
Laurent Pinchart275f5052014-03-17 01:02:46 +0100873 /*
874 * The IPMMU has two register banks, for secure and non-secure modes.
875 * The bank mapped at the beginning of the IPMMU address space
876 * corresponds to the running mode of the CPU. When running in secure
877 * mode the non-secure register bank is also available at an offset.
878 *
879 * Secure mode operation isn't clearly documented and is thus currently
880 * not implemented in the driver. Furthermore, preliminary tests of
881 * non-secure operation with the main register bank were not successful.
882 * Offset the registers base unconditionally to point to the non-secure
883 * alias space for now.
884 */
885 mmu->base += IM_NS_ALIAS_OFFSET;
886
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200887 irq = platform_get_irq(pdev, 0);
888 if (irq < 0) {
889 dev_err(&pdev->dev, "no IRQ found\n");
890 return irq;
891 }
892
893 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
894 dev_name(&pdev->dev), mmu);
895 if (ret < 0) {
896 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
Axel Line222d6a2014-11-01 11:45:32 +0800897 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200898 }
899
900 ipmmu_device_reset(mmu);
901
Magnus Damm7af9a5f2017-08-21 14:53:35 +0900902 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
903 dev_name(&pdev->dev));
904 if (ret)
905 return ret;
906
Magnus Damm01da21e2017-07-17 22:05:10 +0900907 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
908 iommu_device_set_fwnode(&mmu->iommu, &pdev->dev.of_node->fwnode);
909
910 ret = iommu_device_register(&mmu->iommu);
911 if (ret)
912 return ret;
913
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200914 /*
915 * We can't create the ARM mapping here as it requires the bus to have
916 * an IOMMU, which only happens when bus_set_iommu() is called in
917 * ipmmu_init() after the probe function returns.
918 */
919
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200920 platform_set_drvdata(pdev, mmu);
921
922 return 0;
923}
924
925static int ipmmu_remove(struct platform_device *pdev)
926{
927 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
928
Magnus Damm7af9a5f2017-08-21 14:53:35 +0900929 iommu_device_sysfs_remove(&mmu->iommu);
Magnus Damm01da21e2017-07-17 22:05:10 +0900930 iommu_device_unregister(&mmu->iommu);
931
Magnus Damm3ae47292017-05-17 19:07:10 +0900932#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200933 arm_iommu_release_mapping(mmu->mapping);
Magnus Damm3ae47292017-05-17 19:07:10 +0900934#endif
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200935
936 ipmmu_device_reset(mmu);
937
938 return 0;
939}
940
Laurent Pinchart275f5052014-03-17 01:02:46 +0100941static const struct of_device_id ipmmu_of_ids[] = {
942 { .compatible = "renesas,ipmmu-vmsa", },
Axel Linac04f852015-03-17 08:06:45 +0800943 { }
Laurent Pinchart275f5052014-03-17 01:02:46 +0100944};
945
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200946static struct platform_driver ipmmu_driver = {
947 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200948 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +0100949 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200950 },
951 .probe = ipmmu_probe,
952 .remove = ipmmu_remove,
953};
954
955static int __init ipmmu_init(void)
956{
957 int ret;
958
959 ret = platform_driver_register(&ipmmu_driver);
960 if (ret < 0)
961 return ret;
962
963 if (!iommu_present(&platform_bus_type))
964 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
965
966 return 0;
967}
968
969static void __exit ipmmu_exit(void)
970{
971 return platform_driver_unregister(&ipmmu_driver);
972}
973
974subsys_initcall(ipmmu_init);
975module_exit(ipmmu_exit);
976
977MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
978MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
979MODULE_LICENSE("GPL v2");