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