xref: /openbmc/linux/drivers/iommu/ipmmu-vmsa.c (revision d477f603)
157d3f11cSKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2d25a2a16SLaurent Pinchart /*
38128ac3bSPaul Gortmaker  * IOMMU API for Renesas VMSA-compatible IPMMU
48128ac3bSPaul Gortmaker  * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5d25a2a16SLaurent Pinchart  *
617fe1618SYoshihiro Shimoda  * Copyright (C) 2014-2020 Renesas Electronics Corporation
7d25a2a16SLaurent Pinchart  */
8d25a2a16SLaurent Pinchart 
9dbb70692SMagnus Damm #include <linux/bitmap.h>
10d25a2a16SLaurent Pinchart #include <linux/delay.h>
11d25a2a16SLaurent Pinchart #include <linux/dma-mapping.h>
12d25a2a16SLaurent Pinchart #include <linux/err.h>
13d25a2a16SLaurent Pinchart #include <linux/export.h>
148128ac3bSPaul Gortmaker #include <linux/init.h>
15d25a2a16SLaurent Pinchart #include <linux/interrupt.h>
16d25a2a16SLaurent Pinchart #include <linux/io.h>
17b77cf11fSRob Herring #include <linux/iopoll.h>
18d25a2a16SLaurent Pinchart #include <linux/io-pgtable.h>
19275f5053SLaurent Pinchart #include <linux/iommu.h>
207b2d5961SMagnus Damm #include <linux/of.h>
21d25a2a16SLaurent Pinchart #include <linux/of_platform.h>
22d25a2a16SLaurent Pinchart #include <linux/pci.h>
23d25a2a16SLaurent Pinchart #include <linux/platform_device.h>
2458b8e8bfSMagnus Damm #include <linux/sizes.h>
25d25a2a16SLaurent Pinchart #include <linux/slab.h>
263ae47292SMagnus Damm #include <linux/sys_soc.h>
27d25a2a16SLaurent Pinchart 
2849c875f0SRobin Murphy #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
2949c875f0SRobin Murphy #include <asm/dma-iommu.h>
3049c875f0SRobin Murphy #else
3149c875f0SRobin Murphy #define arm_iommu_create_mapping(...)	NULL
323ae47292SMagnus Damm #define arm_iommu_attach_device(...)	-ENODEV
33d25a2a16SLaurent Pinchart #define arm_iommu_release_mapping(...)	do {} while (0)
347a62ced8SYoshihiro Shimoda #endif
35da38e9ecSGeert Uytterhoeven 
36da38e9ecSGeert Uytterhoeven #define IPMMU_CTX_MAX		16U
377a62ced8SYoshihiro Shimoda #define IPMMU_CTX_INVALID	-1
38dbb70692SMagnus Damm 
3933f3ac9bSMagnus Damm #define IPMMU_UTLB_MAX		64U
4033f3ac9bSMagnus Damm 
41fd5140e2SMagnus Damm struct ipmmu_features {
425fd16341SMagnus Damm 	bool use_ns_alias_offset;
43b7f3f047SGeert Uytterhoeven 	bool has_cache_leaf_nodes;
44f5c85891SMagnus Damm 	unsigned int number_of_contexts;
45c295f504SMagnus Damm 	unsigned int num_utlbs;
462ae86955SYoshihiro Shimoda 	bool setup_imbuscr;
473623002fSHai Nguyen Pham 	bool twobit_imttbcr_sl0;
483dc28d9fSYoshihiro Shimoda 	bool reserved_context;
493dc28d9fSYoshihiro Shimoda 	bool cache_snoop;
501289f7f1SYoshihiro Shimoda 	unsigned int ctx_offset_base;
5133f3ac9bSMagnus Damm 	unsigned int ctx_offset_stride;
5233f3ac9bSMagnus Damm 	unsigned int utlb_offset_base;
53d25a2a16SLaurent Pinchart };
54d25a2a16SLaurent Pinchart 
55d25a2a16SLaurent Pinchart struct ipmmu_vmsa_device {
5601da21e5SMagnus Damm 	struct device *dev;
57fd5140e2SMagnus Damm 	void __iomem *base;
5833f3ac9bSMagnus Damm 	struct iommu_device iommu;
595fd16341SMagnus Damm 	struct ipmmu_vmsa_device *root;
60dbb70692SMagnus Damm 	const struct ipmmu_features *features;
61dbb70692SMagnus Damm 	unsigned int num_ctx;
62dbb70692SMagnus Damm 	spinlock_t lock;			/* Protects ctx and domains[] */
63da38e9ecSGeert Uytterhoeven 	DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
64d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
65b354c73eSRobin Murphy 	s8 utlb_ctx[IPMMU_UTLB_MAX];
66d25a2a16SLaurent Pinchart 
67d25a2a16SLaurent Pinchart 	struct iommu_group *group;
68d25a2a16SLaurent Pinchart 	struct dma_iommu_mapping *mapping;
69d25a2a16SLaurent Pinchart };
70d25a2a16SLaurent Pinchart 
715914c5fdSJoerg Roedel struct ipmmu_vmsa_domain {
72d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu;
73f20ed39fSLaurent Pinchart 	struct iommu_domain io_domain;
74f20ed39fSLaurent Pinchart 
75f20ed39fSLaurent Pinchart 	struct io_pgtable_cfg cfg;
76d25a2a16SLaurent Pinchart 	struct io_pgtable_ops *iop;
7746583e8cSGeert Uytterhoeven 
78d25a2a16SLaurent Pinchart 	unsigned int context_id;
79d25a2a16SLaurent Pinchart 	struct mutex mutex;			/* Protects mappings */
805914c5fdSJoerg Roedel };
815914c5fdSJoerg Roedel 
to_vmsa_domain(struct iommu_domain * dom)825914c5fdSJoerg Roedel static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
835914c5fdSJoerg Roedel {
845914c5fdSJoerg Roedel 	return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
85e4efe4a9SRobin Murphy }
860fbc8b04SMagnus Damm 
to_ipmmu(struct device * dev)87be568d6dSJoerg Roedel static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
880fbc8b04SMagnus Damm {
890fbc8b04SMagnus Damm 	return dev_iommu_priv_get(dev);
90d25a2a16SLaurent Pinchart }
91d25a2a16SLaurent Pinchart 
92d25a2a16SLaurent Pinchart #define TLB_LOOP_TIMEOUT		100	/* 100us */
93d25a2a16SLaurent Pinchart 
94d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
95d25a2a16SLaurent Pinchart  * Registers Definition
96275f5053SLaurent Pinchart  */
97275f5053SLaurent Pinchart 
98df9828aaSYoshihiro Shimoda #define IM_NS_ALIAS_OFFSET		0x800
99df9828aaSYoshihiro Shimoda 
100df9828aaSYoshihiro Shimoda /* MMU "context" registers */
101df9828aaSYoshihiro Shimoda #define IMCTR				0x0000		/* R-Car Gen2/3 */
102df9828aaSYoshihiro Shimoda #define IMCTR_INTEN			(1 << 2)	/* R-Car Gen2/3 */
103d25a2a16SLaurent Pinchart #define IMCTR_FLUSH			(1 << 1)	/* R-Car Gen2/3 */
104df9828aaSYoshihiro Shimoda #define IMCTR_MMUEN			(1 << 0)	/* R-Car Gen2/3 */
105df9828aaSYoshihiro Shimoda 
1063623002fSHai Nguyen Pham #define IMTTBCR				0x0008		/* R-Car Gen2/3 */
1073623002fSHai Nguyen Pham #define IMTTBCR_EAE			(1 << 31)	/* R-Car Gen2/3 */
1083623002fSHai Nguyen Pham #define IMTTBCR_SH0_INNER_SHAREABLE	(3 << 12)	/* R-Car Gen2 only */
1095ca54fdcSGeert Uytterhoeven #define IMTTBCR_ORGN0_WB_WA		(1 << 10)	/* R-Car Gen2 only */
110df9828aaSYoshihiro Shimoda #define IMTTBCR_IRGN0_WB_WA		(1 << 8)	/* R-Car Gen2 only */
111d25a2a16SLaurent Pinchart #define IMTTBCR_SL0_TWOBIT_LVL_1	(2 << 6)	/* R-Car Gen3 only */
112df9828aaSYoshihiro Shimoda #define IMTTBCR_SL0_LVL_1		(1 << 4)	/* R-Car Gen2 only */
113df9828aaSYoshihiro Shimoda 
114df9828aaSYoshihiro Shimoda #define IMBUSCR				0x000c		/* R-Car Gen2 only */
115d25a2a16SLaurent Pinchart #define IMBUSCR_DVM			(1 << 2)	/* R-Car Gen2 only */
116df9828aaSYoshihiro Shimoda #define IMBUSCR_BUSSEL_MASK		(3 << 0)	/* R-Car Gen2 only */
117df9828aaSYoshihiro Shimoda 
118d25a2a16SLaurent Pinchart #define IMTTLBR0			0x0010		/* R-Car Gen2/3 */
119df9828aaSYoshihiro Shimoda #define IMTTUBR0			0x0014		/* R-Car Gen2/3 */
120df9828aaSYoshihiro Shimoda 
121df9828aaSYoshihiro Shimoda #define IMSTR				0x0020		/* R-Car Gen2/3 */
122df9828aaSYoshihiro Shimoda #define IMSTR_MHIT			(1 << 4)	/* R-Car Gen2/3 */
123df9828aaSYoshihiro Shimoda #define IMSTR_ABORT			(1 << 2)	/* R-Car Gen2/3 */
124d25a2a16SLaurent Pinchart #define IMSTR_PF			(1 << 1)	/* R-Car Gen2/3 */
125df9828aaSYoshihiro Shimoda #define IMSTR_TF			(1 << 0)	/* R-Car Gen2/3 */
126d25a2a16SLaurent Pinchart 
127df9828aaSYoshihiro Shimoda #define IMMAIR0				0x0028		/* R-Car Gen2/3 */
12882576aa8SGeert Uytterhoeven 
129d25a2a16SLaurent Pinchart #define IMELAR				0x0030		/* R-Car Gen2/3, IMEAR on R-Car Gen2 */
130df9828aaSYoshihiro Shimoda #define IMEUAR				0x0034		/* R-Car Gen3 only */
131ddbbddd7SMagnus Damm 
132df9828aaSYoshihiro Shimoda /* uTLB registers */
133df9828aaSYoshihiro Shimoda #define IMUCTR(n)			((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
134df9828aaSYoshihiro Shimoda #define IMUCTR0(n)			(0x0300 + ((n) * 16))		/* R-Car Gen2/3 */
135df9828aaSYoshihiro Shimoda #define IMUCTR32(n)			(0x0600 + (((n) - 32) * 16))	/* R-Car Gen3 only */
136df9828aaSYoshihiro Shimoda #define IMUCTR_TTSEL_MMU(n)		((n) << 4)	/* R-Car Gen2/3 */
137d25a2a16SLaurent Pinchart #define IMUCTR_FLUSH			(1 << 1)	/* R-Car Gen2/3 */
138ddbbddd7SMagnus Damm #define IMUCTR_MMUEN			(1 << 0)	/* R-Car Gen2/3 */
139df9828aaSYoshihiro Shimoda 
140df9828aaSYoshihiro Shimoda #define IMUASID(n)			((n) < 32 ? IMUASID0(n) : IMUASID32(n))
141d25a2a16SLaurent Pinchart #define IMUASID0(n)			(0x0308 + ((n) * 16))		/* R-Car Gen2/3 */
142d25a2a16SLaurent Pinchart #define IMUASID32(n)			(0x0608 + (((n) - 32) * 16))	/* R-Car Gen3 only */
143fd5140e2SMagnus Damm 
144fd5140e2SMagnus Damm /* -----------------------------------------------------------------------------
145fd5140e2SMagnus Damm  * Root device handling
146fd5140e2SMagnus Damm  */
147fd5140e2SMagnus Damm 
148fd5140e2SMagnus Damm static struct platform_driver ipmmu_driver;
149fd5140e2SMagnus Damm 
ipmmu_is_root(struct ipmmu_vmsa_device * mmu)150fd5140e2SMagnus Damm static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
151fd5140e2SMagnus Damm {
152fd5140e2SMagnus Damm 	return mmu->root == mmu;
153fd5140e2SMagnus Damm }
154fd5140e2SMagnus Damm 
__ipmmu_check_device(struct device * dev,void * data)155fd5140e2SMagnus Damm static int __ipmmu_check_device(struct device *dev, void *data)
156fd5140e2SMagnus Damm {
157fd5140e2SMagnus Damm 	struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
158fd5140e2SMagnus Damm 	struct ipmmu_vmsa_device **rootp = data;
159fd5140e2SMagnus Damm 
160fd5140e2SMagnus Damm 	if (ipmmu_is_root(mmu))
161fd5140e2SMagnus Damm 		*rootp = mmu;
162fd5140e2SMagnus Damm 
163fd5140e2SMagnus Damm 	return 0;
164fd5140e2SMagnus Damm }
165fd5140e2SMagnus Damm 
ipmmu_find_root(void)166fd5140e2SMagnus Damm static struct ipmmu_vmsa_device *ipmmu_find_root(void)
167fd5140e2SMagnus Damm {
168fd5140e2SMagnus Damm 	struct ipmmu_vmsa_device *root = NULL;
169fd5140e2SMagnus Damm 
170fd5140e2SMagnus Damm 	return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
171fd5140e2SMagnus Damm 				      __ipmmu_check_device) == 0 ? root : NULL;
172fd5140e2SMagnus Damm }
173d25a2a16SLaurent Pinchart 
174d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
175d25a2a16SLaurent Pinchart  * Read/Write Access
176d25a2a16SLaurent Pinchart  */
177d25a2a16SLaurent Pinchart 
ipmmu_read(struct ipmmu_vmsa_device * mmu,unsigned int offset)178d25a2a16SLaurent Pinchart static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
179d25a2a16SLaurent Pinchart {
180d25a2a16SLaurent Pinchart 	return ioread32(mmu->base + offset);
181d25a2a16SLaurent Pinchart }
182d25a2a16SLaurent Pinchart 
ipmmu_write(struct ipmmu_vmsa_device * mmu,unsigned int offset,u32 data)183d25a2a16SLaurent Pinchart static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
184d25a2a16SLaurent Pinchart 			u32 data)
185d25a2a16SLaurent Pinchart {
186d25a2a16SLaurent Pinchart 	iowrite32(data, mmu->base + offset);
18716d9454fSYoshihiro Shimoda }
18816d9454fSYoshihiro Shimoda 
ipmmu_ctx_reg(struct ipmmu_vmsa_device * mmu,unsigned int context_id,unsigned int reg)18916d9454fSYoshihiro Shimoda static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
1907a62ced8SYoshihiro Shimoda 				  unsigned int context_id, unsigned int reg)
1917a62ced8SYoshihiro Shimoda {
1927a62ced8SYoshihiro Shimoda 	unsigned int base = mmu->features->ctx_offset_base;
1937a62ced8SYoshihiro Shimoda 
1947a62ced8SYoshihiro Shimoda 	if (context_id > 7)
1957a62ced8SYoshihiro Shimoda 		base += 0x800 - 8 * 0x40;
19616d9454fSYoshihiro Shimoda 
19716d9454fSYoshihiro Shimoda 	return base + context_id * mmu->features->ctx_offset_stride + reg;
19816d9454fSYoshihiro Shimoda }
19916d9454fSYoshihiro Shimoda 
ipmmu_ctx_read(struct ipmmu_vmsa_device * mmu,unsigned int context_id,unsigned int reg)20016d9454fSYoshihiro Shimoda static u32 ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
20116d9454fSYoshihiro Shimoda 			  unsigned int context_id, unsigned int reg)
20216d9454fSYoshihiro Shimoda {
20316d9454fSYoshihiro Shimoda 	return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg));
20416d9454fSYoshihiro Shimoda }
20516d9454fSYoshihiro Shimoda 
ipmmu_ctx_write(struct ipmmu_vmsa_device * mmu,unsigned int context_id,unsigned int reg,u32 data)20616d9454fSYoshihiro Shimoda static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu,
20716d9454fSYoshihiro Shimoda 			    unsigned int context_id, unsigned int reg, u32 data)
20816d9454fSYoshihiro Shimoda {
20916d9454fSYoshihiro Shimoda 	ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data);
210d574893aSMagnus Damm }
211d574893aSMagnus Damm 
ipmmu_ctx_read_root(struct ipmmu_vmsa_domain * domain,unsigned int reg)212d25a2a16SLaurent Pinchart static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
21316d9454fSYoshihiro Shimoda 			       unsigned int reg)
214d25a2a16SLaurent Pinchart {
215d25a2a16SLaurent Pinchart 	return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg);
216d574893aSMagnus Damm }
217d574893aSMagnus Damm 
ipmmu_ctx_write_root(struct ipmmu_vmsa_domain * domain,unsigned int reg,u32 data)218d25a2a16SLaurent Pinchart static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
21916d9454fSYoshihiro Shimoda 				 unsigned int reg, u32 data)
220d25a2a16SLaurent Pinchart {
221d25a2a16SLaurent Pinchart 	ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
222d574893aSMagnus Damm }
223d574893aSMagnus Damm 
ipmmu_ctx_write_all(struct ipmmu_vmsa_domain * domain,unsigned int reg,u32 data)224d574893aSMagnus Damm static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
225d574893aSMagnus Damm 				unsigned int reg, u32 data)
22616d9454fSYoshihiro Shimoda {
227d574893aSMagnus Damm 	if (domain->mmu != domain->mmu->root)
22816d9454fSYoshihiro Shimoda 		ipmmu_ctx_write(domain->mmu, domain->context_id, reg, data);
229d574893aSMagnus Damm 
230d574893aSMagnus Damm 	ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
2313667c997SYoshihiro Shimoda }
2323667c997SYoshihiro Shimoda 
ipmmu_utlb_reg(struct ipmmu_vmsa_device * mmu,unsigned int reg)2331289f7f1SYoshihiro Shimoda static u32 ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, unsigned int reg)
2343667c997SYoshihiro Shimoda {
2353667c997SYoshihiro Shimoda 	return mmu->features->utlb_offset_base + reg;
2363667c997SYoshihiro Shimoda }
2373667c997SYoshihiro Shimoda 
ipmmu_imuasid_write(struct ipmmu_vmsa_device * mmu,unsigned int utlb,u32 data)2383667c997SYoshihiro Shimoda static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
2393667c997SYoshihiro Shimoda 				unsigned int utlb, u32 data)
2403667c997SYoshihiro Shimoda {
2413667c997SYoshihiro Shimoda 	ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data);
2423667c997SYoshihiro Shimoda }
2433667c997SYoshihiro Shimoda 
ipmmu_imuctr_write(struct ipmmu_vmsa_device * mmu,unsigned int utlb,u32 data)2443667c997SYoshihiro Shimoda static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu,
2453667c997SYoshihiro Shimoda 			       unsigned int utlb, u32 data)
246d25a2a16SLaurent Pinchart {
247d25a2a16SLaurent Pinchart 	ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data);
248d25a2a16SLaurent Pinchart }
249d25a2a16SLaurent Pinchart 
250d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
251d25a2a16SLaurent Pinchart  * TLB and microTLB Management
252d25a2a16SLaurent Pinchart  */
253d25a2a16SLaurent Pinchart 
254d25a2a16SLaurent Pinchart /* Wait for any pending TLB invalidations to complete */
ipmmu_tlb_sync(struct ipmmu_vmsa_domain * domain)255d25a2a16SLaurent Pinchart static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
256d25a2a16SLaurent Pinchart {
257d574893aSMagnus Damm 	u32 val;
258d25a2a16SLaurent Pinchart 
259d25a2a16SLaurent Pinchart 	if (read_poll_timeout_atomic(ipmmu_ctx_read_root, val,
260d25a2a16SLaurent Pinchart 				     !(val & IMCTR_FLUSH), 1, TLB_LOOP_TIMEOUT,
261d25a2a16SLaurent Pinchart 				     false, domain, IMCTR))
262d25a2a16SLaurent Pinchart 		dev_err_ratelimited(domain->mmu->dev,
263d25a2a16SLaurent Pinchart 			"TLB sync timed out -- MMU may be deadlocked\n");
264d25a2a16SLaurent Pinchart }
265d25a2a16SLaurent Pinchart 
ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain * domain)266d25a2a16SLaurent Pinchart static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
267d25a2a16SLaurent Pinchart {
268d25a2a16SLaurent Pinchart 	u32 reg;
269d25a2a16SLaurent Pinchart 
270d25a2a16SLaurent Pinchart 	reg = ipmmu_ctx_read_root(domain, IMCTR);
271d25a2a16SLaurent Pinchart 	reg |= IMCTR_FLUSH;
272d574893aSMagnus Damm 	ipmmu_ctx_write_all(domain, IMCTR, reg);
273d25a2a16SLaurent Pinchart 
274d574893aSMagnus Damm 	ipmmu_tlb_sync(domain);
275d25a2a16SLaurent Pinchart }
276d25a2a16SLaurent Pinchart 
277d25a2a16SLaurent Pinchart /*
278d25a2a16SLaurent Pinchart  * Enable MMU translation for the microTLB.
279d25a2a16SLaurent Pinchart  */
ipmmu_utlb_enable(struct ipmmu_vmsa_domain * domain,unsigned int utlb)280d25a2a16SLaurent Pinchart static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
281d25a2a16SLaurent Pinchart 			      unsigned int utlb)
282d25a2a16SLaurent Pinchart {
283192d2045SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu = domain->mmu;
284d25a2a16SLaurent Pinchart 
285d25a2a16SLaurent Pinchart 	/*
286d25a2a16SLaurent Pinchart 	 * TODO: Reference-count the microTLB as several bus masters can be
287192d2045SLaurent Pinchart 	 * connected to the same microTLB.
288192d2045SLaurent Pinchart 	 */
289192d2045SLaurent Pinchart 
290192d2045SLaurent Pinchart 	/* TODO: What should we set the ASID to ? */
291192d2045SLaurent Pinchart 	ipmmu_imuasid_write(mmu, utlb, 0);
292d25a2a16SLaurent Pinchart 	/* TODO: Do we need to flush the microTLB ? */
2933667c997SYoshihiro Shimoda 	ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) |
294d25a2a16SLaurent Pinchart 				      IMUCTR_FLUSH | IMUCTR_MMUEN);
2953667c997SYoshihiro Shimoda 	mmu->utlb_ctx[utlb] = domain->context_id;
2963667c997SYoshihiro Shimoda }
297da38e9ecSGeert Uytterhoeven 
ipmmu_tlb_flush_all(void * cookie)298d25a2a16SLaurent Pinchart static void ipmmu_tlb_flush_all(void *cookie)
299d25a2a16SLaurent Pinchart {
300f20ed39fSLaurent Pinchart 	struct ipmmu_vmsa_domain *domain = cookie;
301d25a2a16SLaurent Pinchart 
302f20ed39fSLaurent Pinchart 	ipmmu_tlb_invalidate(domain);
303f20ed39fSLaurent Pinchart }
304f20ed39fSLaurent Pinchart 
ipmmu_tlb_flush(unsigned long iova,size_t size,size_t granule,void * cookie)305f20ed39fSLaurent Pinchart static void ipmmu_tlb_flush(unsigned long iova, size_t size,
306f20ed39fSLaurent Pinchart 				size_t granule, void *cookie)
30705aed941SWill Deacon {
30805aed941SWill Deacon 	ipmmu_tlb_flush_all(cookie);
309f20ed39fSLaurent Pinchart }
31005aed941SWill Deacon 
311f20ed39fSLaurent Pinchart static const struct iommu_flush_ops ipmmu_flush_ops = {
312f20ed39fSLaurent Pinchart 	.tlb_flush_all = ipmmu_tlb_flush_all,
313298f7889SWill Deacon 	.tlb_flush_walk = ipmmu_tlb_flush,
314f20ed39fSLaurent Pinchart };
31505aed941SWill Deacon 
316f20ed39fSLaurent Pinchart /* -----------------------------------------------------------------------------
317f20ed39fSLaurent Pinchart  * Domain/Context Management
318d25a2a16SLaurent Pinchart  */
319d25a2a16SLaurent Pinchart 
ipmmu_domain_allocate_context(struct ipmmu_vmsa_device * mmu,struct ipmmu_vmsa_domain * domain)320d25a2a16SLaurent Pinchart static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
321d25a2a16SLaurent Pinchart 					 struct ipmmu_vmsa_domain *domain)
322dbb70692SMagnus Damm {
323dbb70692SMagnus Damm 	unsigned long flags;
324dbb70692SMagnus Damm 	int ret;
325dbb70692SMagnus Damm 
326dbb70692SMagnus Damm 	spin_lock_irqsave(&mmu->lock, flags);
327dbb70692SMagnus Damm 
328dbb70692SMagnus Damm 	ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
329dbb70692SMagnus Damm 	if (ret != mmu->num_ctx) {
3305fd16341SMagnus Damm 		mmu->domains[ret] = domain;
3315fd16341SMagnus Damm 		set_bit(ret, mmu->ctx);
332dbb70692SMagnus Damm 	} else
333dbb70692SMagnus Damm 		ret = -EBUSY;
3345fd16341SMagnus Damm 
3355fd16341SMagnus Damm 	spin_unlock_irqrestore(&mmu->lock, flags);
336dbb70692SMagnus Damm 
337dbb70692SMagnus Damm 	return ret;
338dbb70692SMagnus Damm }
339dbb70692SMagnus Damm 
ipmmu_domain_free_context(struct ipmmu_vmsa_device * mmu,unsigned int context_id)340dbb70692SMagnus Damm static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
341dbb70692SMagnus Damm 				      unsigned int context_id)
342a175a67dSOleksandr Tyshchenko {
343a175a67dSOleksandr Tyshchenko 	unsigned long flags;
344a175a67dSOleksandr Tyshchenko 
345a175a67dSOleksandr Tyshchenko 	spin_lock_irqsave(&mmu->lock, flags);
346a175a67dSOleksandr Tyshchenko 
347a175a67dSOleksandr Tyshchenko 	clear_bit(context_id, mmu->ctx);
348a175a67dSOleksandr Tyshchenko 	mmu->domains[context_id] = NULL;
349a175a67dSOleksandr Tyshchenko 
350a175a67dSOleksandr Tyshchenko 	spin_unlock_irqrestore(&mmu->lock, flags);
351a175a67dSOleksandr Tyshchenko }
352a175a67dSOleksandr Tyshchenko 
ipmmu_domain_setup_context(struct ipmmu_vmsa_domain * domain)353a175a67dSOleksandr Tyshchenko static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain *domain)
354a175a67dSOleksandr Tyshchenko {
355892db541SGeert Uytterhoeven 	u64 ttbr;
356d25a2a16SLaurent Pinchart 	u32 tmp;
357f64232eeSGeert Uytterhoeven 
358c295f504SMagnus Damm 	/* TTBR0 */
359a175a67dSOleksandr Tyshchenko 	ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr;
360d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
361d1e5f26fSRobin Murphy 	ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
362d574893aSMagnus Damm 
363d574893aSMagnus Damm 	/*
364d25a2a16SLaurent Pinchart 	 * TTBCR
365d25a2a16SLaurent Pinchart 	 * We use long descriptors and allocate the whole 32-bit VA space to
366d25a2a16SLaurent Pinchart 	 * TTBR0.
3673623002fSHai Nguyen Pham 	 */
3683623002fSHai Nguyen Pham 	if (domain->mmu->features->twobit_imttbcr_sl0)
369d25a2a16SLaurent Pinchart 		tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
370c295f504SMagnus Damm 	else
371c295f504SMagnus Damm 		tmp = IMTTBCR_SL0_LVL_1;
372c295f504SMagnus Damm 
373c295f504SMagnus Damm 	if (domain->mmu->features->cache_snoop)
374c295f504SMagnus Damm 		tmp |= IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
3753623002fSHai Nguyen Pham 		       IMTTBCR_IRGN0_WB_WA;
3763623002fSHai Nguyen Pham 
3773623002fSHai Nguyen Pham 	ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | tmp);
3783623002fSHai Nguyen Pham 
3793623002fSHai Nguyen Pham 	/* MAIR0 */
380d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_root(domain, IMMAIR0,
381f20ed39fSLaurent Pinchart 			     domain->cfg.arm_lpae_s1_cfg.mair);
382d574893aSMagnus Damm 
383205577abSRobin Murphy 	/* IMBUSCR */
384d25a2a16SLaurent Pinchart 	if (domain->mmu->features->setup_imbuscr)
385d25a2a16SLaurent Pinchart 		ipmmu_ctx_write_root(domain, IMBUSCR,
386f5c85891SMagnus Damm 				     ipmmu_ctx_read_root(domain, IMBUSCR) &
387d574893aSMagnus Damm 				     ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
388d574893aSMagnus Damm 
389d25a2a16SLaurent Pinchart 	/*
390d25a2a16SLaurent Pinchart 	 * IMSTR
391d25a2a16SLaurent Pinchart 	 * Clear all interrupt flags.
392d25a2a16SLaurent Pinchart 	 */
393d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
394d25a2a16SLaurent Pinchart 
395d574893aSMagnus Damm 	/*
396d25a2a16SLaurent Pinchart 	 * IMCTR
397d25a2a16SLaurent Pinchart 	 * Enable the MMU and interrupt generation. The long-descriptor
398d25a2a16SLaurent Pinchart 	 * translation table format doesn't use TEX remapping. Don't enable AF
399d25a2a16SLaurent Pinchart 	 * software management as we have no use for it. Flush the TLB as
400d25a2a16SLaurent Pinchart 	 * required when modifying the context registers.
401d25a2a16SLaurent Pinchart 	 */
402d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_all(domain, IMCTR,
403d25a2a16SLaurent Pinchart 			    IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
404d574893aSMagnus Damm }
405d574893aSMagnus Damm 
ipmmu_domain_init_context(struct ipmmu_vmsa_domain * domain)406892db541SGeert Uytterhoeven static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
407d25a2a16SLaurent Pinchart {
408892db541SGeert Uytterhoeven 	int ret;
409892db541SGeert Uytterhoeven 
410892db541SGeert Uytterhoeven 	/*
411892db541SGeert Uytterhoeven 	 * Allocate the page table operations.
412892db541SGeert Uytterhoeven 	 *
413892db541SGeert Uytterhoeven 	 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
414892db541SGeert Uytterhoeven 	 * access, Long-descriptor format" that the NStable bit being set in a
415892db541SGeert Uytterhoeven 	 * table descriptor will result in the NStable and NS bits of all child
416892db541SGeert Uytterhoeven 	 * entries being ignored and considered as being set. The IPMMU seems
417892db541SGeert Uytterhoeven 	 * not to comply with this, as it generates a secure access page fault
418892db541SGeert Uytterhoeven 	 * if any of the NStable and NS bits isn't set when running in
419892db541SGeert Uytterhoeven 	 * non-secure mode.
420892db541SGeert Uytterhoeven 	 */
421892db541SGeert Uytterhoeven 	domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
422892db541SGeert Uytterhoeven 	domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
423892db541SGeert Uytterhoeven 	domain->cfg.ias = 32;
424892db541SGeert Uytterhoeven 	domain->cfg.oas = 40;
425892db541SGeert Uytterhoeven 	domain->cfg.tlb = &ipmmu_flush_ops;
426892db541SGeert Uytterhoeven 	domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
427298f7889SWill Deacon 	domain->io_domain.geometry.force_aperture = true;
428892db541SGeert Uytterhoeven 	/*
429892db541SGeert Uytterhoeven 	 * TODO: Add support for coherent walk through CCI with DVM and remove
430892db541SGeert Uytterhoeven 	 * cache handling. For now, delegate it to the io-pgtable code.
431892db541SGeert Uytterhoeven 	 */
432892db541SGeert Uytterhoeven 	domain->cfg.coherent_walk = false;
433892db541SGeert Uytterhoeven 	domain->cfg.iommu_dev = domain->mmu->root->dev;
4343430abd6SJoerg Roedel 
435892db541SGeert Uytterhoeven 	/*
436892db541SGeert Uytterhoeven 	 * Find an unused context.
437892db541SGeert Uytterhoeven 	 */
438892db541SGeert Uytterhoeven 	ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
439892db541SGeert Uytterhoeven 	if (ret < 0)
440892db541SGeert Uytterhoeven 		return ret;
441892db541SGeert Uytterhoeven 
442892db541SGeert Uytterhoeven 	domain->context_id = ret;
443892db541SGeert Uytterhoeven 
444892db541SGeert Uytterhoeven 	domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
445892db541SGeert Uytterhoeven 					   domain);
446892db541SGeert Uytterhoeven 	if (!domain->iop) {
447892db541SGeert Uytterhoeven 		ipmmu_domain_free_context(domain->mmu->root,
448892db541SGeert Uytterhoeven 					  domain->context_id);
449892db541SGeert Uytterhoeven 		return -EINVAL;
450892db541SGeert Uytterhoeven 	}
451892db541SGeert Uytterhoeven 
452892db541SGeert Uytterhoeven 	ipmmu_domain_setup_context(domain);
453892db541SGeert Uytterhoeven 	return 0;
454892db541SGeert Uytterhoeven }
455d25a2a16SLaurent Pinchart 
ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain * domain)456d25a2a16SLaurent Pinchart static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
457d25a2a16SLaurent Pinchart {
458d25a2a16SLaurent Pinchart 	if (!domain->mmu)
459d25a2a16SLaurent Pinchart 		return;
460e5b78f2eSGeert Uytterhoeven 
461e5b78f2eSGeert Uytterhoeven 	/*
462e5b78f2eSGeert Uytterhoeven 	 * Disable the context. Flush the TLB as required when modifying the
463d25a2a16SLaurent Pinchart 	 * context registers.
464d25a2a16SLaurent Pinchart 	 *
465d25a2a16SLaurent Pinchart 	 * TODO: Is TLB flush really needed ?
466d25a2a16SLaurent Pinchart 	 */
467d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
468d25a2a16SLaurent Pinchart 	ipmmu_tlb_sync(domain);
469d574893aSMagnus Damm 	ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
470d25a2a16SLaurent Pinchart }
471fd5140e2SMagnus Damm 
472d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
473d25a2a16SLaurent Pinchart  * Fault Handling
474d25a2a16SLaurent Pinchart  */
475d25a2a16SLaurent Pinchart 
ipmmu_domain_irq(struct ipmmu_vmsa_domain * domain)476d25a2a16SLaurent Pinchart static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
477d25a2a16SLaurent Pinchart {
478d25a2a16SLaurent Pinchart 	const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
479d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu = domain->mmu;
480d25a2a16SLaurent Pinchart 	unsigned long iova;
481d25a2a16SLaurent Pinchart 	u32 status;
48282576aa8SGeert Uytterhoeven 
483d25a2a16SLaurent Pinchart 	status = ipmmu_ctx_read_root(domain, IMSTR);
484d25a2a16SLaurent Pinchart 	if (!(status & err_mask))
485d574893aSMagnus Damm 		return IRQ_NONE;
486d25a2a16SLaurent Pinchart 
487d25a2a16SLaurent Pinchart 	iova = ipmmu_ctx_read_root(domain, IMELAR);
488d25a2a16SLaurent Pinchart 	if (IS_ENABLED(CONFIG_64BIT))
48982576aa8SGeert Uytterhoeven 		iova |= (u64)ipmmu_ctx_read_root(domain, IMEUAR) << 32;
49082576aa8SGeert Uytterhoeven 
49182576aa8SGeert Uytterhoeven 	/*
492d25a2a16SLaurent Pinchart 	 * Clear the error status flags. Unlike traditional interrupt flag
493d25a2a16SLaurent Pinchart 	 * registers that must be cleared by writing 1, this status register
494d25a2a16SLaurent Pinchart 	 * seems to require 0. The error address register must be read before,
495d25a2a16SLaurent Pinchart 	 * otherwise its value will be 0.
496d25a2a16SLaurent Pinchart 	 */
497d25a2a16SLaurent Pinchart 	ipmmu_ctx_write_root(domain, IMSTR, 0);
498d25a2a16SLaurent Pinchart 
499d574893aSMagnus Damm 	/* Log fatal errors. */
500d25a2a16SLaurent Pinchart 	if (status & IMSTR_MHIT)
501d25a2a16SLaurent Pinchart 		dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%lx\n",
502d25a2a16SLaurent Pinchart 				    iova);
50382576aa8SGeert Uytterhoeven 	if (status & IMSTR_ABORT)
504d25a2a16SLaurent Pinchart 		dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%lx\n",
505d25a2a16SLaurent Pinchart 				    iova);
50682576aa8SGeert Uytterhoeven 
507d25a2a16SLaurent Pinchart 	if (!(status & (IMSTR_PF | IMSTR_TF)))
508d25a2a16SLaurent Pinchart 		return IRQ_NONE;
509d25a2a16SLaurent Pinchart 
510d25a2a16SLaurent Pinchart 	/*
511d25a2a16SLaurent Pinchart 	 * Try to handle page faults and translation faults.
512d25a2a16SLaurent Pinchart 	 *
513d25a2a16SLaurent Pinchart 	 * TODO: We need to look up the faulty device based on the I/O VA. Use
514d25a2a16SLaurent Pinchart 	 * the IOMMU device for now.
515d25a2a16SLaurent Pinchart 	 */
516d25a2a16SLaurent Pinchart 	if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
517d25a2a16SLaurent Pinchart 		return IRQ_HANDLED;
5185914c5fdSJoerg Roedel 
519d25a2a16SLaurent Pinchart 	dev_err_ratelimited(mmu->dev,
520d25a2a16SLaurent Pinchart 			    "Unhandled fault: status 0x%08x iova 0x%lx\n",
521d25a2a16SLaurent Pinchart 			    status, iova);
52282576aa8SGeert Uytterhoeven 
523d25a2a16SLaurent Pinchart 	return IRQ_HANDLED;
524d25a2a16SLaurent Pinchart }
525d25a2a16SLaurent Pinchart 
ipmmu_irq(int irq,void * dev)526d25a2a16SLaurent Pinchart static irqreturn_t ipmmu_irq(int irq, void *dev)
527d25a2a16SLaurent Pinchart {
528d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu = dev;
529d25a2a16SLaurent Pinchart 	irqreturn_t status = IRQ_NONE;
530d25a2a16SLaurent Pinchart 	unsigned int i;
531dbb70692SMagnus Damm 	unsigned long flags;
532dbb70692SMagnus Damm 
533dbb70692SMagnus Damm 	spin_lock_irqsave(&mmu->lock, flags);
534d25a2a16SLaurent Pinchart 
535dbb70692SMagnus Damm 	/*
536d25a2a16SLaurent Pinchart 	 * Check interrupts for all active contexts.
537dbb70692SMagnus Damm 	 */
538dbb70692SMagnus Damm 	for (i = 0; i < mmu->num_ctx; i++) {
539dbb70692SMagnus Damm 		if (!mmu->domains[i])
5405fd16341SMagnus Damm 			continue;
541dbb70692SMagnus Damm 		if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
542dbb70692SMagnus Damm 			status = IRQ_HANDLED;
543dbb70692SMagnus Damm 	}
544dbb70692SMagnus Damm 
545dbb70692SMagnus Damm 	spin_unlock_irqrestore(&mmu->lock, flags);
546d25a2a16SLaurent Pinchart 
547dbb70692SMagnus Damm 	return status;
548dbb70692SMagnus Damm }
549dbb70692SMagnus Damm 
550d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
551d25a2a16SLaurent Pinchart  * IOMMU Operations
552d25a2a16SLaurent Pinchart  */
553d25a2a16SLaurent Pinchart 
ipmmu_domain_alloc(unsigned type)554d25a2a16SLaurent Pinchart static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
555d25a2a16SLaurent Pinchart {
5565d894182SRobin Murphy 	struct ipmmu_vmsa_domain *domain;
557d25a2a16SLaurent Pinchart 
558d25a2a16SLaurent Pinchart 	if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
559d25a2a16SLaurent Pinchart 		return NULL;
5605d894182SRobin Murphy 
5615d894182SRobin Murphy 	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
5625d894182SRobin Murphy 	if (!domain)
563d25a2a16SLaurent Pinchart 		return NULL;
564d25a2a16SLaurent Pinchart 
5655914c5fdSJoerg Roedel 	mutex_init(&domain->mutex);
566d25a2a16SLaurent Pinchart 
56746583e8cSGeert Uytterhoeven 	return &domain->io_domain;
568d25a2a16SLaurent Pinchart }
5695914c5fdSJoerg Roedel 
ipmmu_domain_free(struct iommu_domain * io_domain)570d25a2a16SLaurent Pinchart static void ipmmu_domain_free(struct iommu_domain *io_domain)
571d25a2a16SLaurent Pinchart {
5725914c5fdSJoerg Roedel 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
573d25a2a16SLaurent Pinchart 
5745914c5fdSJoerg Roedel 	/*
575d25a2a16SLaurent Pinchart 	 * Free the domain resources. We assume that all devices have already
576d25a2a16SLaurent Pinchart 	 * been detached.
577d25a2a16SLaurent Pinchart 	 */
578d25a2a16SLaurent Pinchart 	ipmmu_domain_destroy_context(domain);
579d25a2a16SLaurent Pinchart 	free_io_pgtable_ops(domain->iop);
580d25a2a16SLaurent Pinchart 	kfree(domain);
581f20ed39fSLaurent Pinchart }
582d25a2a16SLaurent Pinchart 
ipmmu_attach_device(struct iommu_domain * io_domain,struct device * dev)583d25a2a16SLaurent Pinchart static int ipmmu_attach_device(struct iommu_domain *io_domain,
584d25a2a16SLaurent Pinchart 			       struct device *dev)
585d25a2a16SLaurent Pinchart {
586d25a2a16SLaurent Pinchart 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
587d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
588df903655SJoerg Roedel 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
589e4efe4a9SRobin Murphy 	unsigned int i;
5905914c5fdSJoerg Roedel 	int ret = 0;
591a166d31eSLaurent Pinchart 
592d25a2a16SLaurent Pinchart 	if (!mmu) {
593d25a2a16SLaurent Pinchart 		dev_err(dev, "Cannot attach to IPMMU\n");
594e4efe4a9SRobin Murphy 		return -ENXIO;
595d25a2a16SLaurent Pinchart 	}
596d25a2a16SLaurent Pinchart 
597d25a2a16SLaurent Pinchart 	mutex_lock(&domain->mutex);
598d25a2a16SLaurent Pinchart 
59946583e8cSGeert Uytterhoeven 	if (!domain->mmu) {
600d25a2a16SLaurent Pinchart 		/* The domain hasn't been used yet, initialize it. */
601d25a2a16SLaurent Pinchart 		domain->mmu = mmu;
602d25a2a16SLaurent Pinchart 		ret = ipmmu_domain_init_context(domain);
603d25a2a16SLaurent Pinchart 		if (ret < 0) {
604d25a2a16SLaurent Pinchart 			dev_err(dev, "Unable to initialize IPMMU context\n");
6055fd16341SMagnus Damm 			domain->mmu = NULL;
6065fd16341SMagnus Damm 		} else {
6075fd16341SMagnus Damm 			dev_info(dev, "Using IPMMU context %u\n",
6085fd16341SMagnus Damm 				 domain->context_id);
6095fd16341SMagnus Damm 		}
6105fd16341SMagnus Damm 	} else if (domain->mmu != mmu) {
6115fd16341SMagnus Damm 		/*
612d25a2a16SLaurent Pinchart 		 * Something is wrong, we can't attach two devices using
613d25a2a16SLaurent Pinchart 		 * different IOMMUs to the same domain.
614d25a2a16SLaurent Pinchart 		 */
615d25a2a16SLaurent Pinchart 		ret = -EINVAL;
616d25a2a16SLaurent Pinchart 	} else
617d25a2a16SLaurent Pinchart 		dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
6183ae47292SMagnus Damm 
6193ae47292SMagnus Damm 	mutex_unlock(&domain->mutex);
620d25a2a16SLaurent Pinchart 
62146583e8cSGeert Uytterhoeven 	if (ret < 0)
622d25a2a16SLaurent Pinchart 		return ret;
623d25a2a16SLaurent Pinchart 
624d25a2a16SLaurent Pinchart 	for (i = 0; i < fwspec->num_ids; ++i)
625d25a2a16SLaurent Pinchart 		ipmmu_utlb_enable(domain, fwspec->ids[i]);
6267b2d5961SMagnus Damm 
6277b2d5961SMagnus Damm 	return 0;
628d25a2a16SLaurent Pinchart }
629d25a2a16SLaurent Pinchart 
ipmmu_map(struct iommu_domain * io_domain,unsigned long iova,phys_addr_t paddr,size_t pgsize,size_t pgcount,int prot,gfp_t gfp,size_t * mapped)630d25a2a16SLaurent Pinchart static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
631d25a2a16SLaurent Pinchart 		     phys_addr_t paddr, size_t pgsize, size_t pgcount,
632d25a2a16SLaurent Pinchart 		     int prot, gfp_t gfp, size_t *mapped)
6330a17bbabSRobin Murphy {
6340a17bbabSRobin Murphy 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
635d25a2a16SLaurent Pinchart 
6365914c5fdSJoerg Roedel 	return domain->iop->map_pages(domain->iop, iova, paddr, pgsize, pgcount,
637d25a2a16SLaurent Pinchart 				      prot, gfp, mapped);
6380a17bbabSRobin Murphy }
6390a17bbabSRobin Murphy 
ipmmu_unmap(struct iommu_domain * io_domain,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)640d25a2a16SLaurent Pinchart static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
641d25a2a16SLaurent Pinchart 			  size_t pgsize, size_t pgcount,
642d25a2a16SLaurent Pinchart 			  struct iommu_iotlb_gather *gather)
6430a17bbabSRobin Murphy {
6440a17bbabSRobin Murphy 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
645d25a2a16SLaurent Pinchart 
6465914c5fdSJoerg Roedel 	return domain->iop->unmap_pages(domain->iop, iova, pgsize, pgcount, gather);
647d25a2a16SLaurent Pinchart }
6480a17bbabSRobin Murphy 
ipmmu_flush_iotlb_all(struct iommu_domain * io_domain)649d25a2a16SLaurent Pinchart static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain)
650d25a2a16SLaurent Pinchart {
65156f8af5eSWill Deacon 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
65232b12449SRobin Murphy 
65332b12449SRobin Murphy 	if (domain->mmu)
65432b12449SRobin Murphy 		ipmmu_tlb_flush_all(domain);
65532b12449SRobin Murphy }
65632b12449SRobin Murphy 
ipmmu_iotlb_sync(struct iommu_domain * io_domain,struct iommu_iotlb_gather * gather)65732b12449SRobin Murphy static void ipmmu_iotlb_sync(struct iommu_domain *io_domain,
65832b12449SRobin Murphy 			     struct iommu_iotlb_gather *gather)
65956f8af5eSWill Deacon {
66056f8af5eSWill Deacon 	ipmmu_flush_iotlb_all(io_domain);
66156f8af5eSWill Deacon }
66256f8af5eSWill Deacon 
ipmmu_iova_to_phys(struct iommu_domain * io_domain,dma_addr_t iova)66356f8af5eSWill Deacon static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
66456f8af5eSWill Deacon 				      dma_addr_t iova)
665d25a2a16SLaurent Pinchart {
666d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
667d25a2a16SLaurent Pinchart 
6685914c5fdSJoerg Roedel 	/* TODO: Is locking needed ? */
669d25a2a16SLaurent Pinchart 
670d25a2a16SLaurent Pinchart 	return domain->iop->iova_to_phys(domain->iop, iova);
671d25a2a16SLaurent Pinchart }
672f20ed39fSLaurent Pinchart 
ipmmu_init_platform_device(struct device * dev,struct of_phandle_args * args)673d25a2a16SLaurent Pinchart static int ipmmu_init_platform_device(struct device *dev,
674d25a2a16SLaurent Pinchart 				      struct of_phandle_args *args)
6757b2d5961SMagnus Damm {
6767b2d5961SMagnus Damm 	struct platform_device *ipmmu_pdev;
677192d2045SLaurent Pinchart 
6787b2d5961SMagnus Damm 	ipmmu_pdev = of_find_device_by_node(args->np);
679d25a2a16SLaurent Pinchart 	if (!ipmmu_pdev)
6807b2d5961SMagnus Damm 		return -ENODEV;
6817b2d5961SMagnus Damm 
682bb590c90SLaurent Pinchart 	dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev));
683bb590c90SLaurent Pinchart 
684be568d6dSJoerg Roedel 	return 0;
685df903655SJoerg Roedel }
686383fef5fSMagnus Damm 
687383fef5fSMagnus Damm static const struct soc_device_attribute soc_needs_opt_in[] = {
688383fef5fSMagnus Damm 	{ .family = "R-Car Gen3", },
689815cdd86SYoshihiro Shimoda 	{ .family = "R-Car Gen4", },
690815cdd86SYoshihiro Shimoda 	{ .family = "RZ/G2", },
691ae684cafSYoshihiro Shimoda 	{ /* sentinel */ }
692815cdd86SYoshihiro Shimoda };
693815cdd86SYoshihiro Shimoda 
694815cdd86SYoshihiro Shimoda static const struct soc_device_attribute soc_denylist[] = {
695815cdd86SYoshihiro Shimoda 	{ .soc_id = "r8a774a1", },
696815cdd86SYoshihiro Shimoda 	{ .soc_id = "r8a7795", .revision = "ES2.*" },
69760fb0083SFabrizio Castro 	{ .soc_id = "r8a7796", },
698815cdd86SYoshihiro Shimoda 	{ /* sentinel */ }
6990b8ac140SMagnus Damm };
70058b8e8bfSMagnus Damm 
70158b8e8bfSMagnus Damm static const char * const devices_allowlist[] = {
70258b8e8bfSMagnus Damm 	"ee100000.mmc",
703815cdd86SYoshihiro Shimoda 	"ee120000.mmc",
704cec0813dSYoshihiro Shimoda 	"ee140000.mmc",
705cec0813dSYoshihiro Shimoda 	"ee160000.mmc"
706cec0813dSYoshihiro Shimoda };
707cec0813dSYoshihiro Shimoda 
ipmmu_device_is_allowed(struct device * dev)708b7ee92c6SYoshihiro Shimoda static bool ipmmu_device_is_allowed(struct device *dev)
709b7ee92c6SYoshihiro Shimoda {
710815cdd86SYoshihiro Shimoda 	unsigned int i;
711b7ee92c6SYoshihiro Shimoda 
71280759649SYoshihiro Shimoda 	/*
71380759649SYoshihiro Shimoda 	 * R-Car Gen3/4 and RZ/G2 use the allow list to opt-in devices.
714b7ee92c6SYoshihiro Shimoda 	 * For Other SoCs, this returns true anyway.
715ae684cafSYoshihiro Shimoda 	 */
716b7ee92c6SYoshihiro Shimoda 	if (!soc_device_match(soc_needs_opt_in))
717b7ee92c6SYoshihiro Shimoda 		return true;
718815cdd86SYoshihiro Shimoda 
719b7ee92c6SYoshihiro Shimoda 	/* Check whether this SoC can use the IPMMU correctly or not */
720b7ee92c6SYoshihiro Shimoda 	if (soc_device_match(soc_denylist))
721815cdd86SYoshihiro Shimoda 		return false;
722815cdd86SYoshihiro Shimoda 
723b7ee92c6SYoshihiro Shimoda 	/* Check whether this device is a PCI device */
724b7ee92c6SYoshihiro Shimoda 	if (dev_is_pci(dev))
725815cdd86SYoshihiro Shimoda 		return true;
726815cdd86SYoshihiro Shimoda 
727815cdd86SYoshihiro Shimoda 	/* Check whether this device can work with the IPMMU */
72880759649SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(devices_allowlist); i++) {
72980759649SYoshihiro Shimoda 		if (!strcmp(dev_name(dev), devices_allowlist[i]))
73080759649SYoshihiro Shimoda 			return true;
73180759649SYoshihiro Shimoda 	}
732b7ee92c6SYoshihiro Shimoda 
733b7ee92c6SYoshihiro Shimoda 	/* Otherwise, do not allow use of IPMMU */
734b7ee92c6SYoshihiro Shimoda 	return false;
73549558da0SMagnus Damm }
73649558da0SMagnus Damm 
ipmmu_of_xlate(struct device * dev,struct of_phandle_args * spec)73749558da0SMagnus Damm static int ipmmu_of_xlate(struct device *dev,
738815cdd86SYoshihiro Shimoda 			  struct of_phandle_args *spec)
73958b8e8bfSMagnus Damm {
74058b8e8bfSMagnus Damm 	if (!ipmmu_device_is_allowed(dev))
7417b2d5961SMagnus Damm 		return -ENODEV;
7427b2d5961SMagnus Damm 
74349558da0SMagnus Damm 	iommu_fwspec_add_ids(dev, spec->args, 1);
744e4efe4a9SRobin Murphy 
74549558da0SMagnus Damm 	/* Initialize once - xlate() will call multiple times */
74649558da0SMagnus Damm 	if (to_ipmmu(dev))
7477b2d5961SMagnus Damm 		return 0;
74849558da0SMagnus Damm 
74949558da0SMagnus Damm 	return ipmmu_init_platform_device(dev, spec);
75049c875f0SRobin Murphy }
751383fef5fSMagnus Damm 
ipmmu_init_arm_mapping(struct device * dev)752e4efe4a9SRobin Murphy static int ipmmu_init_arm_mapping(struct device *dev)
753383fef5fSMagnus Damm {
754383fef5fSMagnus Damm 	struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
755d25a2a16SLaurent Pinchart 	int ret;
756d25a2a16SLaurent Pinchart 
757d25a2a16SLaurent Pinchart 	/*
758d25a2a16SLaurent Pinchart 	 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
759d25a2a16SLaurent Pinchart 	 * VAs. This will allocate a corresponding IOMMU domain.
760d25a2a16SLaurent Pinchart 	 *
761d25a2a16SLaurent Pinchart 	 * TODO:
762d25a2a16SLaurent Pinchart 	 * - Create one mapping per context (TLB).
763d25a2a16SLaurent Pinchart 	 * - Make the mapping size configurable ? We currently use a 2GB mapping
764d25a2a16SLaurent Pinchart 	 *   at a 1GB offset to ensure that NULL VAs will fault.
765d25a2a16SLaurent Pinchart 	 */
766d25a2a16SLaurent Pinchart 	if (!mmu->mapping) {
767d25a2a16SLaurent Pinchart 		struct dma_iommu_mapping *mapping;
768720b0cefSJoerg Roedel 
769d25a2a16SLaurent Pinchart 		mapping = arm_iommu_create_mapping(&platform_bus_type,
770d25a2a16SLaurent Pinchart 						   SZ_1G, SZ_2G);
771b8f80bffSLaurent Pinchart 		if (IS_ERR(mapping)) {
772b8f80bffSLaurent Pinchart 			dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
773d25a2a16SLaurent Pinchart 			ret = PTR_ERR(mapping);
774d25a2a16SLaurent Pinchart 			goto error;
775d25a2a16SLaurent Pinchart 		}
776d25a2a16SLaurent Pinchart 
777d25a2a16SLaurent Pinchart 		mmu->mapping = mapping;
778d25a2a16SLaurent Pinchart 	}
779d25a2a16SLaurent Pinchart 
780d25a2a16SLaurent Pinchart 	/* Attach the ARM VA mapping to the device. */
781d25a2a16SLaurent Pinchart 	ret = arm_iommu_attach_device(dev, mmu->mapping);
782d25a2a16SLaurent Pinchart 	if (ret < 0) {
783d25a2a16SLaurent Pinchart 		dev_err(dev, "Failed to attach device to VA mapping\n");
784d25a2a16SLaurent Pinchart 		goto error;
785d25a2a16SLaurent Pinchart 	}
786d25a2a16SLaurent Pinchart 
787d25a2a16SLaurent Pinchart 	return 0;
78849c875f0SRobin Murphy 
78949c875f0SRobin Murphy error:
790a166d31eSLaurent Pinchart 	if (mmu->mapping)
791d25a2a16SLaurent Pinchart 		arm_iommu_release_mapping(mmu->mapping);
792d25a2a16SLaurent Pinchart 
793d25a2a16SLaurent Pinchart 	return ret;
7946580c8a7SJoerg Roedel }
7953ae47292SMagnus Damm 
ipmmu_probe_device(struct device * dev)79680eaa9f5SGeert Uytterhoeven static struct iommu_device *ipmmu_probe_device(struct device *dev)
7973ae47292SMagnus Damm {
7980fbc8b04SMagnus Damm 	struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
7990fbc8b04SMagnus Damm 
8000fbc8b04SMagnus Damm 	/*
80180eaa9f5SGeert Uytterhoeven 	 * Only let through devices that have been verified in xlate()
8026580c8a7SJoerg Roedel 	 */
8033ae47292SMagnus Damm 	if (!mmu)
8046580c8a7SJoerg Roedel 		return ERR_PTR(-ENODEV);
80580eaa9f5SGeert Uytterhoeven 
80680eaa9f5SGeert Uytterhoeven 	return &mmu->iommu;
8076580c8a7SJoerg Roedel }
8083ae47292SMagnus Damm 
ipmmu_probe_finalize(struct device * dev)8096580c8a7SJoerg Roedel static void ipmmu_probe_finalize(struct device *dev)
81080eaa9f5SGeert Uytterhoeven {
8116580c8a7SJoerg Roedel 	int ret = 0;
8126580c8a7SJoerg Roedel 
8136580c8a7SJoerg Roedel 	if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
8146580c8a7SJoerg Roedel 		ret = ipmmu_init_arm_mapping(dev);
8156580c8a7SJoerg Roedel 
8166580c8a7SJoerg Roedel 	if (ret)
8176580c8a7SJoerg Roedel 		dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n");
8186580c8a7SJoerg Roedel }
8196580c8a7SJoerg Roedel 
ipmmu_release_device(struct device * dev)82024dfb197SLu Baolu static void ipmmu_release_device(struct device *dev)
82124dfb197SLu Baolu {
82224dfb197SLu Baolu 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
82324dfb197SLu Baolu 	struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
82424dfb197SLu Baolu 	unsigned int i;
82524dfb197SLu Baolu 
82624dfb197SLu Baolu 	for (i = 0; i < fwspec->num_ids; ++i) {
82724dfb197SLu Baolu 		unsigned int utlb = fwspec->ids[i];
82824dfb197SLu Baolu 
82924dfb197SLu Baolu 		ipmmu_imuctr_write(mmu, utlb, 0);
83024dfb197SLu Baolu 		mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
83124dfb197SLu Baolu 	}
8323ae47292SMagnus Damm 
8333ae47292SMagnus Damm 	arm_iommu_release_mapping(mmu->mapping);
834b354c73eSRobin Murphy }
8353ae47292SMagnus Damm 
ipmmu_find_group(struct device * dev)836e4efe4a9SRobin Murphy static struct iommu_group *ipmmu_find_group(struct device *dev)
8373ae47292SMagnus Damm {
8383ae47292SMagnus Damm 	struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
839e4efe4a9SRobin Murphy 	struct iommu_group *group;
840e4efe4a9SRobin Murphy 
841b354c73eSRobin Murphy 	if (mmu->group)
842b354c73eSRobin Murphy 		return iommu_group_ref_get(mmu->group);
843b354c73eSRobin Murphy 
844e4efe4a9SRobin Murphy 	group = iommu_group_alloc();
8453ae47292SMagnus Damm 	if (!IS_ERR(group))
8463ae47292SMagnus Damm 		mmu->group = group;
8473ae47292SMagnus Damm 
8483ae47292SMagnus Damm 	return group;
8493ae47292SMagnus Damm }
8501c7e7c02SRobin Murphy 
8516580c8a7SJoerg Roedel static const struct iommu_ops ipmmu_ops = {
8526580c8a7SJoerg Roedel 	.domain_alloc = ipmmu_domain_alloc,
8536580c8a7SJoerg Roedel 	.probe_device = ipmmu_probe_device,
8542ba20b5aSArnd Bergmann 	.release_device = ipmmu_release_device,
8552ba20b5aSArnd Bergmann 	.probe_finalize = ipmmu_probe_finalize,
8563ae47292SMagnus Damm 	.device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)
85749558da0SMagnus Damm 			? generic_device_group : ipmmu_find_group,
8589a630a4bSLu Baolu 	.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
8599a630a4bSLu Baolu 	.of_xlate = ipmmu_of_xlate,
8600a17bbabSRobin Murphy 	.default_domain_ops = &(const struct iommu_domain_ops) {
8610a17bbabSRobin Murphy 		.attach_dev	= ipmmu_attach_device,
8629a630a4bSLu Baolu 		.map_pages	= ipmmu_map,
8639a630a4bSLu Baolu 		.unmap_pages	= ipmmu_unmap,
8649a630a4bSLu Baolu 		.flush_iotlb_all = ipmmu_flush_iotlb_all,
8659a630a4bSLu Baolu 		.iotlb_sync	= ipmmu_iotlb_sync,
8669a630a4bSLu Baolu 		.iova_to_phys	= ipmmu_iova_to_phys,
8673ae47292SMagnus Damm 		.free		= ipmmu_domain_free,
8683ae47292SMagnus Damm 	}
869d25a2a16SLaurent Pinchart };
870d25a2a16SLaurent Pinchart 
871d25a2a16SLaurent Pinchart /* -----------------------------------------------------------------------------
872d25a2a16SLaurent Pinchart  * Probe/remove and init
873d25a2a16SLaurent Pinchart  */
874d25a2a16SLaurent Pinchart 
ipmmu_device_reset(struct ipmmu_vmsa_device * mmu)875d25a2a16SLaurent Pinchart static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
876d25a2a16SLaurent Pinchart {
877d25a2a16SLaurent Pinchart 	unsigned int i;
8785fd16341SMagnus Damm 
87916d9454fSYoshihiro Shimoda 	/* Disable all contexts. */
880d25a2a16SLaurent Pinchart 	for (i = 0; i < mmu->num_ctx; ++i)
881d25a2a16SLaurent Pinchart 		ipmmu_ctx_write(mmu, i, IMCTR, 0);
88233f3ac9bSMagnus Damm }
88333f3ac9bSMagnus Damm 
884fd5140e2SMagnus Damm static const struct ipmmu_features ipmmu_features_default = {
8855fd16341SMagnus Damm 	.use_ns_alias_offset = true,
886b7f3f047SGeert Uytterhoeven 	.has_cache_leaf_nodes = false,
887f5c85891SMagnus Damm 	.number_of_contexts = 1, /* software only tested with one context */
888c295f504SMagnus Damm 	.num_utlbs = 32,
8892ae86955SYoshihiro Shimoda 	.setup_imbuscr = true,
8903623002fSHai Nguyen Pham 	.twobit_imttbcr_sl0 = false,
8913dc28d9fSYoshihiro Shimoda 	.reserved_context = false,
8923dc28d9fSYoshihiro Shimoda 	.cache_snoop = true,
8931289f7f1SYoshihiro Shimoda 	.ctx_offset_base = 0,
89433f3ac9bSMagnus Damm 	.ctx_offset_stride = 0x40,
89533f3ac9bSMagnus Damm 	.utlb_offset_base = 0,
8960b8ac140SMagnus Damm };
89758b8e8bfSMagnus Damm 
89858b8e8bfSMagnus Damm static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
89958b8e8bfSMagnus Damm 	.use_ns_alias_offset = false,
900b7f3f047SGeert Uytterhoeven 	.has_cache_leaf_nodes = true,
90158b8e8bfSMagnus Damm 	.number_of_contexts = 8,
90258b8e8bfSMagnus Damm 	.num_utlbs = 48,
9032ae86955SYoshihiro Shimoda 	.setup_imbuscr = false,
9043623002fSHai Nguyen Pham 	.twobit_imttbcr_sl0 = true,
9053dc28d9fSYoshihiro Shimoda 	.reserved_context = true,
9063dc28d9fSYoshihiro Shimoda 	.cache_snoop = false,
9071289f7f1SYoshihiro Shimoda 	.ctx_offset_base = 0,
90858b8e8bfSMagnus Damm 	.ctx_offset_stride = 0x40,
90958b8e8bfSMagnus Damm 	.utlb_offset_base = 0,
910ae684cafSYoshihiro Shimoda };
9117a62ced8SYoshihiro Shimoda 
9127a62ced8SYoshihiro Shimoda static const struct ipmmu_features ipmmu_features_rcar_gen4 = {
9137a62ced8SYoshihiro Shimoda 	.use_ns_alias_offset = false,
9147a62ced8SYoshihiro Shimoda 	.has_cache_leaf_nodes = true,
9157a62ced8SYoshihiro Shimoda 	.number_of_contexts = 16,
9167a62ced8SYoshihiro Shimoda 	.num_utlbs = 64,
9177a62ced8SYoshihiro Shimoda 	.setup_imbuscr = false,
9187a62ced8SYoshihiro Shimoda 	.twobit_imttbcr_sl0 = true,
9197a62ced8SYoshihiro Shimoda 	.reserved_context = true,
9207a62ced8SYoshihiro Shimoda 	.cache_snoop = false,
9217a62ced8SYoshihiro Shimoda 	.ctx_offset_base = 0x10000,
9227a62ced8SYoshihiro Shimoda 	.ctx_offset_stride = 0x1040,
9237a62ced8SYoshihiro Shimoda 	.utlb_offset_base = 0x3000,
92433f3ac9bSMagnus Damm };
92533f3ac9bSMagnus Damm 
92633f3ac9bSMagnus Damm static const struct of_device_id ipmmu_of_ids[] = {
92733f3ac9bSMagnus Damm 	{
92833f3ac9bSMagnus Damm 		.compatible = "renesas,ipmmu-vmsa",
92960fb0083SFabrizio Castro 		.data = &ipmmu_features_default,
93060fb0083SFabrizio Castro 	}, {
93160fb0083SFabrizio Castro 		.compatible = "renesas,ipmmu-r8a774a1",
932757f26a3SBiju Das 		.data = &ipmmu_features_rcar_gen3,
933757f26a3SBiju Das 	}, {
934757f26a3SBiju Das 		.compatible = "renesas,ipmmu-r8a774b1",
935b6d39cd8SFabrizio Castro 		.data = &ipmmu_features_rcar_gen3,
936b6d39cd8SFabrizio Castro 	}, {
937b6d39cd8SFabrizio Castro 		.compatible = "renesas,ipmmu-r8a774c0",
9384b2aa7a6SMarian-Cristian Rotariu 		.data = &ipmmu_features_rcar_gen3,
9394b2aa7a6SMarian-Cristian Rotariu 	}, {
9404b2aa7a6SMarian-Cristian Rotariu 		.compatible = "renesas,ipmmu-r8a774e1",
94158b8e8bfSMagnus Damm 		.data = &ipmmu_features_rcar_gen3,
9420b8ac140SMagnus Damm 	}, {
9430b8ac140SMagnus Damm 		.compatible = "renesas,ipmmu-r8a7795",
9440b8ac140SMagnus Damm 		.data = &ipmmu_features_rcar_gen3,
9450b8ac140SMagnus Damm 	}, {
94658b8e8bfSMagnus Damm 		.compatible = "renesas,ipmmu-r8a7796",
94717fe1618SYoshihiro Shimoda 		.data = &ipmmu_features_rcar_gen3,
94817fe1618SYoshihiro Shimoda 	}, {
94917fe1618SYoshihiro Shimoda 		.compatible = "renesas,ipmmu-r8a77961",
95098dbffd3SJacopo Mondi 		.data = &ipmmu_features_rcar_gen3,
95198dbffd3SJacopo Mondi 	}, {
95298dbffd3SJacopo Mondi 		.compatible = "renesas,ipmmu-r8a77965",
9533701c123SSimon Horman 		.data = &ipmmu_features_rcar_gen3,
9543701c123SSimon Horman 	}, {
9553701c123SSimon Horman 		.compatible = "renesas,ipmmu-r8a77970",
9561cdeb52eSNikita Yushchenko 		.data = &ipmmu_features_rcar_gen3,
9571cdeb52eSNikita Yushchenko 	}, {
9581cdeb52eSNikita Yushchenko 		.compatible = "renesas,ipmmu-r8a77980",
959b0c32912SHai Nguyen Pham 		.data = &ipmmu_features_rcar_gen3,
960b0c32912SHai Nguyen Pham 	}, {
961b0c32912SHai Nguyen Pham 		.compatible = "renesas,ipmmu-r8a77990",
9623701c123SSimon Horman 		.data = &ipmmu_features_rcar_gen3,
9633701c123SSimon Horman 	}, {
96433f3ac9bSMagnus Damm 		.compatible = "renesas,ipmmu-r8a77995",
9657a62ced8SYoshihiro Shimoda 		.data = &ipmmu_features_rcar_gen3,
966ae684cafSYoshihiro Shimoda 	}, {
967ae684cafSYoshihiro Shimoda 		.compatible = "renesas,ipmmu-r8a779a0",
9689f7d09feSYoshihiro Shimoda 		.data = &ipmmu_features_rcar_gen4,
969ae684cafSYoshihiro Shimoda 	}, {
9707a62ced8SYoshihiro Shimoda 		.compatible = "renesas,rcar-gen4-ipmmu-vmsa",
97133f3ac9bSMagnus Damm 		.data = &ipmmu_features_rcar_gen4,
97233f3ac9bSMagnus Damm 	}, {
97333f3ac9bSMagnus Damm 		/* Terminator */
97433f3ac9bSMagnus Damm 	},
975d25a2a16SLaurent Pinchart };
976d25a2a16SLaurent Pinchart 
ipmmu_probe(struct platform_device * pdev)977d25a2a16SLaurent Pinchart static int ipmmu_probe(struct platform_device *pdev)
978d25a2a16SLaurent Pinchart {
979d25a2a16SLaurent Pinchart 	struct ipmmu_vmsa_device *mmu;
980d25a2a16SLaurent Pinchart 	struct resource *res;
981d25a2a16SLaurent Pinchart 	int irq;
982d25a2a16SLaurent Pinchart 	int ret;
983d25a2a16SLaurent Pinchart 
984d25a2a16SLaurent Pinchart 	mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
985d25a2a16SLaurent Pinchart 	if (!mmu) {
986d25a2a16SLaurent Pinchart 		dev_err(&pdev->dev, "cannot allocate device data\n");
987d25a2a16SLaurent Pinchart 		return -ENOMEM;
988d25a2a16SLaurent Pinchart 	}
989dbb70692SMagnus Damm 
990dbb70692SMagnus Damm 	mmu->dev = &pdev->dev;
99133f3ac9bSMagnus Damm 	spin_lock_init(&mmu->lock);
992da38e9ecSGeert Uytterhoeven 	bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
9931fdbbfd5SJiasheng Jiang 	mmu->features = of_device_get_match_data(&pdev->dev);
9941fdbbfd5SJiasheng Jiang 	memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs);
9951fdbbfd5SJiasheng Jiang 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
996d25a2a16SLaurent Pinchart 	if (ret)
997d25a2a16SLaurent Pinchart 		return ret;
998d25a2a16SLaurent Pinchart 
999d25a2a16SLaurent Pinchart 	/* Map I/O memory and request IRQ. */
1000d25a2a16SLaurent Pinchart 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001d25a2a16SLaurent Pinchart 	mmu->base = devm_ioremap_resource(&pdev->dev, res);
1002d25a2a16SLaurent Pinchart 	if (IS_ERR(mmu->base))
1003275f5053SLaurent Pinchart 		return PTR_ERR(mmu->base);
1004275f5053SLaurent Pinchart 
1005275f5053SLaurent Pinchart 	/*
1006275f5053SLaurent Pinchart 	 * The IPMMU has two register banks, for secure and non-secure modes.
1007275f5053SLaurent Pinchart 	 * The bank mapped at the beginning of the IPMMU address space
1008275f5053SLaurent Pinchart 	 * corresponds to the running mode of the CPU. When running in secure
1009275f5053SLaurent Pinchart 	 * mode the non-secure register bank is also available at an offset.
1010275f5053SLaurent Pinchart 	 *
1011275f5053SLaurent Pinchart 	 * Secure mode operation isn't clearly documented and is thus currently
1012275f5053SLaurent Pinchart 	 * not implemented in the driver. Furthermore, preliminary tests of
1013275f5053SLaurent Pinchart 	 * non-secure operation with the main register bank were not successful.
1014275f5053SLaurent Pinchart 	 * Offset the registers base unconditionally to point to the non-secure
101533f3ac9bSMagnus Damm 	 * alias space for now.
1016275f5053SLaurent Pinchart 	 */
1017275f5053SLaurent Pinchart 	if (mmu->features->use_ns_alias_offset)
1018b43e0d8aSGeert Uytterhoeven 		mmu->base += IM_NS_ALIAS_OFFSET;
10195fd16341SMagnus Damm 
1020fd5140e2SMagnus Damm 	mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
1021fd5140e2SMagnus Damm 
1022fd5140e2SMagnus Damm 	/*
1023fd5140e2SMagnus Damm 	 * Determine if this IPMMU instance is a root device by checking for
1024fd5140e2SMagnus Damm 	 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
1025a6c9e387SRob Herring 	 */
1026fd5140e2SMagnus Damm 	if (!mmu->features->has_cache_leaf_nodes ||
1027fd5140e2SMagnus Damm 	    !of_property_present(pdev->dev.of_node, "renesas,ipmmu-main"))
1028fd5140e2SMagnus Damm 		mmu->root = mmu;
1029fd5140e2SMagnus Damm 	else
1030fd5140e2SMagnus Damm 		mmu->root = ipmmu_find_root();
1031fd5140e2SMagnus Damm 
1032fd5140e2SMagnus Damm 	/*
1033fd5140e2SMagnus Damm 	 * Wait until the root device has been registered for sure.
1034fd5140e2SMagnus Damm 	 */
1035fd5140e2SMagnus Damm 	if (!mmu->root)
1036fd5140e2SMagnus Damm 		return -EPROBE_DEFER;
1037fd5140e2SMagnus Damm 
1038ec37d4e9SGeert Uytterhoeven 	/* Root devices have mandatory IRQs */
1039565d4542SYueHaibing 	if (ipmmu_is_root(mmu)) {
1040d25a2a16SLaurent Pinchart 		irq = platform_get_irq(pdev, 0);
1041d25a2a16SLaurent Pinchart 		if (irq < 0)
1042d25a2a16SLaurent Pinchart 			return irq;
1043d25a2a16SLaurent Pinchart 
1044d25a2a16SLaurent Pinchart 		ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1045d25a2a16SLaurent Pinchart 				       dev_name(&pdev->dev), mmu);
1046e222d6a4SAxel Lin 		if (ret < 0) {
1047d25a2a16SLaurent Pinchart 			dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1048d25a2a16SLaurent Pinchart 			return ret;
1049d25a2a16SLaurent Pinchart 		}
10502ae86955SYoshihiro Shimoda 
10512ae86955SYoshihiro Shimoda 		ipmmu_device_reset(mmu);
10522ae86955SYoshihiro Shimoda 
10532ae86955SYoshihiro Shimoda 		if (mmu->features->reserved_context) {
10542ae86955SYoshihiro Shimoda 			dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
1055fd5140e2SMagnus Damm 			set_bit(0, mmu->ctx);
1056d25a2a16SLaurent Pinchart 		}
1057cda52fcdSMagnus Damm 	}
1058cda52fcdSMagnus Damm 
1059cda52fcdSMagnus Damm 	/*
1060cda52fcdSMagnus Damm 	 * Register the IPMMU to the IOMMU subsystem in the following cases:
1061cda52fcdSMagnus Damm 	 * - R-Car Gen2 IPMMU (all devices registered)
1062cda52fcdSMagnus Damm 	 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
10637af9a5fdSMagnus Damm 	 */
10647af9a5fdSMagnus Damm 	if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
10657af9a5fdSMagnus Damm 		ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
10667af9a5fdSMagnus Damm 					     dev_name(&pdev->dev));
10677af9a5fdSMagnus Damm 		if (ret)
10682d471b20SRobin Murphy 			return ret;
106901da21e5SMagnus Damm 
107001da21e5SMagnus Damm 		ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev);
1071cda52fcdSMagnus Damm 		if (ret)
1072cda52fcdSMagnus Damm 			return ret;
1073d25a2a16SLaurent Pinchart 	}
1074d25a2a16SLaurent Pinchart 
1075d25a2a16SLaurent Pinchart 	/*
1076d25a2a16SLaurent Pinchart 	 * We can't create the ARM mapping here as it requires the bus to have
1077d25a2a16SLaurent Pinchart 	 * an IOMMU, which only happens when bus_set_iommu() is called in
1078d25a2a16SLaurent Pinchart 	 * ipmmu_init() after the probe function returns.
1079d25a2a16SLaurent Pinchart 	 */
1080d25a2a16SLaurent Pinchart 
1081d25a2a16SLaurent Pinchart 	platform_set_drvdata(pdev, mmu);
1082d25a2a16SLaurent Pinchart 
1083d25a2a16SLaurent Pinchart 	return 0;
1084*7471ea50SUwe Kleine-König }
1085d25a2a16SLaurent Pinchart 
ipmmu_remove(struct platform_device * pdev)1086d25a2a16SLaurent Pinchart static void ipmmu_remove(struct platform_device *pdev)
1087d25a2a16SLaurent Pinchart {
10887af9a5fdSMagnus Damm 	struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
108901da21e5SMagnus Damm 
109001da21e5SMagnus Damm 	iommu_device_sysfs_remove(&mmu->iommu);
1091d25a2a16SLaurent Pinchart 	iommu_device_unregister(&mmu->iommu);
1092d25a2a16SLaurent Pinchart 
1093d25a2a16SLaurent Pinchart 	arm_iommu_release_mapping(mmu->mapping);
1094d25a2a16SLaurent Pinchart 
1095d25a2a16SLaurent Pinchart 	ipmmu_device_reset(mmu);
1096da38e9ecSGeert Uytterhoeven }
1097da38e9ecSGeert Uytterhoeven 
1098da38e9ecSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
ipmmu_resume_noirq(struct device * dev)1099da38e9ecSGeert Uytterhoeven static int ipmmu_resume_noirq(struct device *dev)
1100da38e9ecSGeert Uytterhoeven {
1101da38e9ecSGeert Uytterhoeven 	struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
1102da38e9ecSGeert Uytterhoeven 	unsigned int i;
1103da38e9ecSGeert Uytterhoeven 
1104da38e9ecSGeert Uytterhoeven 	/* Reset root MMU and restore contexts */
1105da38e9ecSGeert Uytterhoeven 	if (ipmmu_is_root(mmu)) {
1106da38e9ecSGeert Uytterhoeven 		ipmmu_device_reset(mmu);
1107da38e9ecSGeert Uytterhoeven 
1108da38e9ecSGeert Uytterhoeven 		for (i = 0; i < mmu->num_ctx; i++) {
1109da38e9ecSGeert Uytterhoeven 			if (!mmu->domains[i])
1110da38e9ecSGeert Uytterhoeven 				continue;
1111da38e9ecSGeert Uytterhoeven 
1112da38e9ecSGeert Uytterhoeven 			ipmmu_domain_setup_context(mmu->domains[i]);
1113da38e9ecSGeert Uytterhoeven 		}
1114da38e9ecSGeert Uytterhoeven 	}
1115da38e9ecSGeert Uytterhoeven 
1116da38e9ecSGeert Uytterhoeven 	/* Re-enable active micro-TLBs */
1117da38e9ecSGeert Uytterhoeven 	for (i = 0; i < mmu->features->num_utlbs; i++) {
1118da38e9ecSGeert Uytterhoeven 		if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID)
1119da38e9ecSGeert Uytterhoeven 			continue;
1120da38e9ecSGeert Uytterhoeven 
1121da38e9ecSGeert Uytterhoeven 		ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i);
1122da38e9ecSGeert Uytterhoeven 	}
1123da38e9ecSGeert Uytterhoeven 
1124da38e9ecSGeert Uytterhoeven 	return 0;
1125da38e9ecSGeert Uytterhoeven }
1126da38e9ecSGeert Uytterhoeven 
1127da38e9ecSGeert Uytterhoeven static const struct dev_pm_ops ipmmu_pm  = {
1128da38e9ecSGeert Uytterhoeven 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq)
1129da38e9ecSGeert Uytterhoeven };
1130da38e9ecSGeert Uytterhoeven #define DEV_PM_OPS	&ipmmu_pm
1131da38e9ecSGeert Uytterhoeven #else
1132da38e9ecSGeert Uytterhoeven #define DEV_PM_OPS	NULL
1133d25a2a16SLaurent Pinchart #endif /* CONFIG_PM_SLEEP */
1134d25a2a16SLaurent Pinchart 
1135d25a2a16SLaurent Pinchart static struct platform_driver ipmmu_driver = {
1136275f5053SLaurent Pinchart 	.driver = {
1137da38e9ecSGeert Uytterhoeven 		.name = "ipmmu-vmsa",
1138d25a2a16SLaurent Pinchart 		.of_match_table = of_match_ptr(ipmmu_of_ids),
1139d25a2a16SLaurent Pinchart 		.pm = DEV_PM_OPS,
1140*7471ea50SUwe Kleine-König 	},
1141d25a2a16SLaurent Pinchart 	.probe = ipmmu_probe,
1142b87d6d7fSRobin Murphy 	.remove_new = ipmmu_remove,
1143 };
1144 builtin_platform_driver(ipmmu_driver);
1145