rockchip-iommu.c 34.5 KB
Newer Older
D
Daniel Kurtz 已提交
1 2 3 4 5 6
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

7
#include <linux/clk.h>
D
Daniel Kurtz 已提交
8 9 10
#include <linux/compiler.h>
#include <linux/delay.h>
#include <linux/device.h>
11
#include <linux/dma-iommu.h>
12
#include <linux/dma-mapping.h>
D
Daniel Kurtz 已提交
13 14 15 16
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iommu.h>
17
#include <linux/iopoll.h>
D
Daniel Kurtz 已提交
18 19 20 21
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/of.h>
22
#include <linux/of_iommu.h>
D
Daniel Kurtz 已提交
23 24
#include <linux/of_platform.h>
#include <linux/platform_device.h>
25
#include <linux/pm_runtime.h>
D
Daniel Kurtz 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <linux/slab.h>
#include <linux/spinlock.h>

/** MMU register offsets */
#define RK_MMU_DTE_ADDR		0x00	/* Directory table address */
#define RK_MMU_STATUS		0x04
#define RK_MMU_COMMAND		0x08
#define RK_MMU_PAGE_FAULT_ADDR	0x0C	/* IOVA of last page fault */
#define RK_MMU_ZAP_ONE_LINE	0x10	/* Shootdown one IOTLB entry */
#define RK_MMU_INT_RAWSTAT	0x14	/* IRQ status ignoring mask */
#define RK_MMU_INT_CLEAR	0x18	/* Acknowledge and re-arm irq */
#define RK_MMU_INT_MASK		0x1C	/* IRQ enable */
#define RK_MMU_INT_STATUS	0x20	/* IRQ status after masking */
#define RK_MMU_AUTO_GATING	0x24

#define DTE_ADDR_DUMMY		0xCAFEBABE
42 43 44 45

#define RK_MMU_POLL_PERIOD_US		100
#define RK_MMU_FORCE_RESET_TIMEOUT_US	100000
#define RK_MMU_POLL_TIMEOUT_US		1000
D
Daniel Kurtz 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/* RK_MMU_STATUS fields */
#define RK_MMU_STATUS_PAGING_ENABLED       BIT(0)
#define RK_MMU_STATUS_PAGE_FAULT_ACTIVE    BIT(1)
#define RK_MMU_STATUS_STALL_ACTIVE         BIT(2)
#define RK_MMU_STATUS_IDLE                 BIT(3)
#define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY  BIT(4)
#define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE  BIT(5)
#define RK_MMU_STATUS_STALL_NOT_ACTIVE     BIT(31)

/* RK_MMU_COMMAND command values */
#define RK_MMU_CMD_ENABLE_PAGING    0  /* Enable memory translation */
#define RK_MMU_CMD_DISABLE_PAGING   1  /* Disable memory translation */
#define RK_MMU_CMD_ENABLE_STALL     2  /* Stall paging to allow other cmds */
#define RK_MMU_CMD_DISABLE_STALL    3  /* Stop stall re-enables paging */
#define RK_MMU_CMD_ZAP_CACHE        4  /* Shoot down entire IOTLB */
#define RK_MMU_CMD_PAGE_FAULT_DONE  5  /* Clear page fault */
#define RK_MMU_CMD_FORCE_RESET      6  /* Reset all registers */

/* RK_MMU_INT_* register fields */
#define RK_MMU_IRQ_PAGE_FAULT    0x01  /* page fault */
#define RK_MMU_IRQ_BUS_ERROR     0x02  /* bus read error */
#define RK_MMU_IRQ_MASK          (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR)

#define NUM_DT_ENTRIES 1024
#define NUM_PT_ENTRIES 1024

#define SPAGE_ORDER 12
#define SPAGE_SIZE (1 << SPAGE_ORDER)

 /*
  * Support mapping any size that fits in one page table:
  *   4 KiB to 4 MiB
  */
#define RK_IOMMU_PGSIZE_BITMAP 0x007ff000

struct rk_iommu_domain {
	struct list_head iommus;
	u32 *dt; /* page directory table */
85
	dma_addr_t dt_dma;
D
Daniel Kurtz 已提交
86 87
	spinlock_t iommus_lock; /* lock for iommus list */
	spinlock_t dt_lock; /* lock for modifying page directory table */
88 89

	struct iommu_domain domain;
D
Daniel Kurtz 已提交
90 91
};

92 93 94 95 96
/* list of clocks required by IOMMU */
static const char * const rk_iommu_clocks[] = {
	"aclk", "iface",
};

D
Daniel Kurtz 已提交
97 98
struct rk_iommu {
	struct device *dev;
99 100
	void __iomem **bases;
	int num_mmu;
101 102
	struct clk_bulk_data *clocks;
	int num_clocks;
103
	bool reset_disabled;
104
	struct iommu_device iommu;
D
Daniel Kurtz 已提交
105 106
	struct list_head node; /* entry in rk_iommu_domain.iommus */
	struct iommu_domain *domain; /* domain to which iommu is attached */
107
	struct iommu_group *group;
D
Daniel Kurtz 已提交
108 109
};

110
struct rk_iommudata {
111
	struct device_link *link; /* runtime PM link from IOMMU to master */
112 113 114
	struct rk_iommu *iommu;
};

115 116
static struct device *dma_dev;

117 118
static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma,
				  unsigned int count)
D
Daniel Kurtz 已提交
119
{
120
	size_t size = count * sizeof(u32); /* count of u32 entry */
D
Daniel Kurtz 已提交
121

122
	dma_sync_single_for_device(dma_dev, dma, size, DMA_TO_DEVICE);
D
Daniel Kurtz 已提交
123 124
}

125 126 127 128 129
static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
{
	return container_of(dom, struct rk_iommu_domain, domain);
}

D
Daniel Kurtz 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/*
 * The Rockchip rk3288 iommu uses a 2-level page table.
 * The first level is the "Directory Table" (DT).
 * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing
 * to a "Page Table".
 * The second level is the 1024 Page Tables (PT).
 * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to
 * a 4 KB page of physical memory.
 *
 * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries).
 * Each iommu device has a MMU_DTE_ADDR register that contains the physical
 * address of the start of the DT page.
 *
 * The structure of the page table is as follows:
 *
 *                   DT
 * MMU_DTE_ADDR -> +-----+
 *                 |     |
 *                 +-----+     PT
 *                 | DTE | -> +-----+
 *                 +-----+    |     |     Memory
 *                 |     |    +-----+     Page
 *                 |     |    | PTE | -> +-----+
 *                 +-----+    +-----+    |     |
 *                            |     |    |     |
 *                            |     |    |     |
 *                            +-----+    |     |
 *                                       |     |
 *                                       |     |
 *                                       +-----+
 */

/*
 * Each DTE has a PT address and a valid bit:
 * +---------------------+-----------+-+
 * | PT address          | Reserved  |V|
 * +---------------------+-----------+-+
 *  31:12 - PT address (PTs always starts on a 4 KB boundary)
 *  11: 1 - Reserved
 *      0 - 1 if PT @ PT address is valid
 */
#define RK_DTE_PT_ADDRESS_MASK    0xfffff000
#define RK_DTE_PT_VALID           BIT(0)

static inline phys_addr_t rk_dte_pt_address(u32 dte)
{
	return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK;
}

static inline bool rk_dte_is_pt_valid(u32 dte)
{
	return dte & RK_DTE_PT_VALID;
}

184
static inline u32 rk_mk_dte(dma_addr_t pt_dma)
D
Daniel Kurtz 已提交
185
{
186
	return (pt_dma & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID;
D
Daniel Kurtz 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

/*
 * Each PTE has a Page address, some flags and a valid bit:
 * +---------------------+---+-------+-+
 * | Page address        |Rsv| Flags |V|
 * +---------------------+---+-------+-+
 *  31:12 - Page address (Pages always start on a 4 KB boundary)
 *  11: 9 - Reserved
 *   8: 1 - Flags
 *      8 - Read allocate - allocate cache space on read misses
 *      7 - Read cache - enable cache & prefetch of data
 *      6 - Write buffer - enable delaying writes on their way to memory
 *      5 - Write allocate - allocate cache space on write misses
 *      4 - Write cache - different writes can be merged together
 *      3 - Override cache attributes
 *          if 1, bits 4-8 control cache attributes
 *          if 0, the system bus defaults are used
 *      2 - Writable
 *      1 - Readable
 *      0 - 1 if Page @ Page address is valid
 */
#define RK_PTE_PAGE_ADDRESS_MASK  0xfffff000
#define RK_PTE_PAGE_FLAGS_MASK    0x000001fe
#define RK_PTE_PAGE_WRITABLE      BIT(2)
#define RK_PTE_PAGE_READABLE      BIT(1)
#define RK_PTE_PAGE_VALID         BIT(0)

static inline phys_addr_t rk_pte_page_address(u32 pte)
{
	return (phys_addr_t)pte & RK_PTE_PAGE_ADDRESS_MASK;
}

static inline bool rk_pte_is_page_valid(u32 pte)
{
	return pte & RK_PTE_PAGE_VALID;
}

/* TODO: set cache flags per prot IOMMU_CACHE */
static u32 rk_mk_pte(phys_addr_t page, int prot)
{
	u32 flags = 0;
	flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0;
	flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0;
	page &= RK_PTE_PAGE_ADDRESS_MASK;
	return page | flags | RK_PTE_PAGE_VALID;
}

static u32 rk_mk_pte_invalid(u32 pte)
{
	return pte & ~RK_PTE_PAGE_VALID;
}

/*
 * rk3288 iova (IOMMU Virtual Address) format
 *  31       22.21       12.11          0
 * +-----------+-----------+-------------+
 * | DTE index | PTE index | Page offset |
 * +-----------+-----------+-------------+
 *  31:22 - DTE index   - index of DTE in DT
 *  21:12 - PTE index   - index of PTE in PT @ DTE.pt_address
 *  11: 0 - Page offset - offset into page @ PTE.page_address
 */
#define RK_IOVA_DTE_MASK    0xffc00000
#define RK_IOVA_DTE_SHIFT   22
#define RK_IOVA_PTE_MASK    0x003ff000
#define RK_IOVA_PTE_SHIFT   12
#define RK_IOVA_PAGE_MASK   0x00000fff
#define RK_IOVA_PAGE_SHIFT  0

static u32 rk_iova_dte_index(dma_addr_t iova)
{
	return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT;
}

static u32 rk_iova_pte_index(dma_addr_t iova)
{
	return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT;
}

static u32 rk_iova_page_offset(dma_addr_t iova)
{
	return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT;
}

272
static u32 rk_iommu_read(void __iomem *base, u32 offset)
D
Daniel Kurtz 已提交
273
{
274
	return readl(base + offset);
D
Daniel Kurtz 已提交
275 276
}

277
static void rk_iommu_write(void __iomem *base, u32 offset, u32 value)
D
Daniel Kurtz 已提交
278
{
279
	writel(value, base + offset);
D
Daniel Kurtz 已提交
280 281 282 283
}

static void rk_iommu_command(struct rk_iommu *iommu, u32 command)
{
284 285 286 287
	int i;

	for (i = 0; i < iommu->num_mmu; i++)
		writel(command, iommu->bases[i] + RK_MMU_COMMAND);
D
Daniel Kurtz 已提交
288 289
}

290 291 292 293
static void rk_iommu_base_command(void __iomem *base, u32 command)
{
	writel(command, base + RK_MMU_COMMAND);
}
294
static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start,
D
Daniel Kurtz 已提交
295 296
			       size_t size)
{
297
	int i;
298
	dma_addr_t iova_end = iova_start + size;
D
Daniel Kurtz 已提交
299 300 301 302
	/*
	 * TODO(djkurtz): Figure out when it is more efficient to shootdown the
	 * entire iotlb rather than iterate over individual iovas.
	 */
303 304 305 306
	for (i = 0; i < iommu->num_mmu; i++) {
		dma_addr_t iova;

		for (iova = iova_start; iova < iova_end; iova += SPAGE_SIZE)
307
			rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova);
308
	}
D
Daniel Kurtz 已提交
309 310 311 312
}

static bool rk_iommu_is_stall_active(struct rk_iommu *iommu)
{
313 314 315 316
	bool active = true;
	int i;

	for (i = 0; i < iommu->num_mmu; i++)
317 318
		active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
					   RK_MMU_STATUS_STALL_ACTIVE);
319 320

	return active;
D
Daniel Kurtz 已提交
321 322 323 324
}

static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu)
{
325 326 327 328
	bool enable = true;
	int i;

	for (i = 0; i < iommu->num_mmu; i++)
329 330
		enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
					   RK_MMU_STATUS_PAGING_ENABLED);
331 332

	return enable;
D
Daniel Kurtz 已提交
333 334
}

335 336 337 338 339 340 341 342 343 344 345
static bool rk_iommu_is_reset_done(struct rk_iommu *iommu)
{
	bool done = true;
	int i;

	for (i = 0; i < iommu->num_mmu; i++)
		done &= rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0;

	return done;
}

D
Daniel Kurtz 已提交
346 347
static int rk_iommu_enable_stall(struct rk_iommu *iommu)
{
348
	int ret, i;
349
	bool val;
D
Daniel Kurtz 已提交
350 351 352 353 354 355 356 357 358 359

	if (rk_iommu_is_stall_active(iommu))
		return 0;

	/* Stall can only be enabled if paging is enabled */
	if (!rk_iommu_is_paging_enabled(iommu))
		return 0;

	rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL);

360 361 362
	ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val,
				 val, RK_MMU_POLL_PERIOD_US,
				 RK_MMU_POLL_TIMEOUT_US);
D
Daniel Kurtz 已提交
363
	if (ret)
364 365 366
		for (i = 0; i < iommu->num_mmu; i++)
			dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n",
				rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
D
Daniel Kurtz 已提交
367 368 369 370 371 372

	return ret;
}

static int rk_iommu_disable_stall(struct rk_iommu *iommu)
{
373
	int ret, i;
374
	bool val;
D
Daniel Kurtz 已提交
375 376 377 378 379 380

	if (!rk_iommu_is_stall_active(iommu))
		return 0;

	rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL);

381 382 383
	ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val,
				 !val, RK_MMU_POLL_PERIOD_US,
				 RK_MMU_POLL_TIMEOUT_US);
D
Daniel Kurtz 已提交
384
	if (ret)
385 386 387
		for (i = 0; i < iommu->num_mmu; i++)
			dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n",
				rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
D
Daniel Kurtz 已提交
388 389 390 391 392 393

	return ret;
}

static int rk_iommu_enable_paging(struct rk_iommu *iommu)
{
394
	int ret, i;
395
	bool val;
D
Daniel Kurtz 已提交
396 397 398 399 400 401

	if (rk_iommu_is_paging_enabled(iommu))
		return 0;

	rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING);

402 403 404
	ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val,
				 val, RK_MMU_POLL_PERIOD_US,
				 RK_MMU_POLL_TIMEOUT_US);
D
Daniel Kurtz 已提交
405
	if (ret)
406 407 408
		for (i = 0; i < iommu->num_mmu; i++)
			dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n",
				rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
D
Daniel Kurtz 已提交
409 410 411 412 413 414

	return ret;
}

static int rk_iommu_disable_paging(struct rk_iommu *iommu)
{
415
	int ret, i;
416
	bool val;
D
Daniel Kurtz 已提交
417 418 419 420 421 422

	if (!rk_iommu_is_paging_enabled(iommu))
		return 0;

	rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING);

423 424 425
	ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val,
				 !val, RK_MMU_POLL_PERIOD_US,
				 RK_MMU_POLL_TIMEOUT_US);
D
Daniel Kurtz 已提交
426
	if (ret)
427 428 429
		for (i = 0; i < iommu->num_mmu; i++)
			dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n",
				rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
D
Daniel Kurtz 已提交
430 431 432 433 434 435

	return ret;
}

static int rk_iommu_force_reset(struct rk_iommu *iommu)
{
436
	int ret, i;
D
Daniel Kurtz 已提交
437
	u32 dte_addr;
438
	bool val;
D
Daniel Kurtz 已提交
439

440 441 442
	if (iommu->reset_disabled)
		return 0;

D
Daniel Kurtz 已提交
443 444 445 446
	/*
	 * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY
	 * and verifying that upper 5 nybbles are read back.
	 */
447 448
	for (i = 0; i < iommu->num_mmu; i++) {
		rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, DTE_ADDR_DUMMY);
D
Daniel Kurtz 已提交
449

450 451 452 453 454
		dte_addr = rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR);
		if (dte_addr != (DTE_ADDR_DUMMY & RK_DTE_PT_ADDRESS_MASK)) {
			dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n");
			return -EFAULT;
		}
D
Daniel Kurtz 已提交
455 456 457 458
	}

	rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET);

459 460 461 462 463 464
	ret = readx_poll_timeout(rk_iommu_is_reset_done, iommu, val,
				 val, RK_MMU_FORCE_RESET_TIMEOUT_US,
				 RK_MMU_POLL_TIMEOUT_US);
	if (ret) {
		dev_err(iommu->dev, "FORCE_RESET command timed out\n");
		return ret;
465
	}
D
Daniel Kurtz 已提交
466

467
	return 0;
D
Daniel Kurtz 已提交
468 469
}

470
static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova)
D
Daniel Kurtz 已提交
471
{
472
	void __iomem *base = iommu->bases[index];
D
Daniel Kurtz 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	u32 dte_index, pte_index, page_offset;
	u32 mmu_dte_addr;
	phys_addr_t mmu_dte_addr_phys, dte_addr_phys;
	u32 *dte_addr;
	u32 dte;
	phys_addr_t pte_addr_phys = 0;
	u32 *pte_addr = NULL;
	u32 pte = 0;
	phys_addr_t page_addr_phys = 0;
	u32 page_flags = 0;

	dte_index = rk_iova_dte_index(iova);
	pte_index = rk_iova_pte_index(iova);
	page_offset = rk_iova_page_offset(iova);

488
	mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR);
D
Daniel Kurtz 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	mmu_dte_addr_phys = (phys_addr_t)mmu_dte_addr;

	dte_addr_phys = mmu_dte_addr_phys + (4 * dte_index);
	dte_addr = phys_to_virt(dte_addr_phys);
	dte = *dte_addr;

	if (!rk_dte_is_pt_valid(dte))
		goto print_it;

	pte_addr_phys = rk_dte_pt_address(dte) + (pte_index * 4);
	pte_addr = phys_to_virt(pte_addr_phys);
	pte = *pte_addr;

	if (!rk_pte_is_page_valid(pte))
		goto print_it;

	page_addr_phys = rk_pte_page_address(pte) + page_offset;
	page_flags = pte & RK_PTE_PAGE_FLAGS_MASK;

print_it:
	dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n",
		&iova, dte_index, pte_index, page_offset);
	dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n",
		&mmu_dte_addr_phys, &dte_addr_phys, dte,
		rk_dte_is_pt_valid(dte), &pte_addr_phys, pte,
		rk_pte_is_page_valid(pte), &page_addr_phys, page_flags);
}

static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
{
	struct rk_iommu *iommu = dev_id;
	u32 status;
	u32 int_status;
	dma_addr_t iova;
523
	irqreturn_t ret = IRQ_NONE;
524
	int i, err;
D
Daniel Kurtz 已提交
525

526 527 528
	err = pm_runtime_get_if_in_use(iommu->dev);
	if (WARN_ON_ONCE(err <= 0))
		return ret;
529 530 531

	if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks)))
		goto out;
532

533 534 535 536
	for (i = 0; i < iommu->num_mmu; i++) {
		int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
		if (int_status == 0)
			continue;
D
Daniel Kurtz 已提交
537

538 539
		ret = IRQ_HANDLED;
		iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
D
Daniel Kurtz 已提交
540

541 542
		if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
			int flags;
D
Daniel Kurtz 已提交
543

544 545 546
			status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
			flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ?
					IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
D
Daniel Kurtz 已提交
547

548 549 550
			dev_err(iommu->dev, "Page fault at %pad of type %s\n",
				&iova,
				(flags == IOMMU_FAULT_WRITE) ? "write" : "read");
D
Daniel Kurtz 已提交
551

552
			log_iova(iommu, i, iova);
D
Daniel Kurtz 已提交
553

554 555 556 557 558 559 560 561 562 563
			/*
			 * Report page fault to any installed handlers.
			 * Ignore the return code, though, since we always zap cache
			 * and clear the page fault anyway.
			 */
			if (iommu->domain)
				report_iommu_fault(iommu->domain, iommu->dev, iova,
						   flags);
			else
				dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
D
Daniel Kurtz 已提交
564

565 566 567
			rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
			rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
		}
D
Daniel Kurtz 已提交
568

569 570
		if (int_status & RK_MMU_IRQ_BUS_ERROR)
			dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
D
Daniel Kurtz 已提交
571

572 573 574
		if (int_status & ~RK_MMU_IRQ_MASK)
			dev_err(iommu->dev, "unexpected int_status: %#08x\n",
				int_status);
D
Daniel Kurtz 已提交
575

576 577
		rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
	}
D
Daniel Kurtz 已提交
578

579 580
	clk_bulk_disable(iommu->num_clocks, iommu->clocks);

581 582
out:
	pm_runtime_put(iommu->dev);
583
	return ret;
D
Daniel Kurtz 已提交
584 585 586 587 588
}

static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain,
					 dma_addr_t iova)
{
589
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
D
Daniel Kurtz 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	unsigned long flags;
	phys_addr_t pt_phys, phys = 0;
	u32 dte, pte;
	u32 *page_table;

	spin_lock_irqsave(&rk_domain->dt_lock, flags);

	dte = rk_domain->dt[rk_iova_dte_index(iova)];
	if (!rk_dte_is_pt_valid(dte))
		goto out;

	pt_phys = rk_dte_pt_address(dte);
	page_table = (u32 *)phys_to_virt(pt_phys);
	pte = page_table[rk_iova_pte_index(iova)];
	if (!rk_pte_is_page_valid(pte))
		goto out;

	phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
out:
	spin_unlock_irqrestore(&rk_domain->dt_lock, flags);

	return phys;
}

static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain,
			      dma_addr_t iova, size_t size)
{
	struct list_head *pos;
	unsigned long flags;

	/* shootdown these iova from all iommus using this domain */
	spin_lock_irqsave(&rk_domain->iommus_lock, flags);
	list_for_each(pos, &rk_domain->iommus) {
		struct rk_iommu *iommu;
624
		int ret;
625

D
Daniel Kurtz 已提交
626
		iommu = list_entry(pos, struct rk_iommu, node);
627 628

		/* Only zap TLBs of IOMMUs that are powered on. */
629 630 631 632
		ret = pm_runtime_get_if_in_use(iommu->dev);
		if (WARN_ON_ONCE(ret < 0))
			continue;
		if (ret) {
633 634 635 636 637 638
			WARN_ON(clk_bulk_enable(iommu->num_clocks,
						iommu->clocks));
			rk_iommu_zap_lines(iommu, iova, size);
			clk_bulk_disable(iommu->num_clocks, iommu->clocks);
			pm_runtime_put(iommu->dev);
		}
D
Daniel Kurtz 已提交
639 640 641 642
	}
	spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
}

643 644 645 646 647 648 649 650 651
static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
					 dma_addr_t iova, size_t size)
{
	rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
	if (size > SPAGE_SIZE)
		rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE,
					SPAGE_SIZE);
}

D
Daniel Kurtz 已提交
652 653 654 655
static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
				  dma_addr_t iova)
{
	u32 *page_table, *dte_addr;
656
	u32 dte_index, dte;
D
Daniel Kurtz 已提交
657
	phys_addr_t pt_phys;
658
	dma_addr_t pt_dma;
D
Daniel Kurtz 已提交
659 660 661

	assert_spin_locked(&rk_domain->dt_lock);

662 663
	dte_index = rk_iova_dte_index(iova);
	dte_addr = &rk_domain->dt[dte_index];
D
Daniel Kurtz 已提交
664 665 666 667 668 669 670 671
	dte = *dte_addr;
	if (rk_dte_is_pt_valid(dte))
		goto done;

	page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
	if (!page_table)
		return ERR_PTR(-ENOMEM);

672 673 674
	pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
	if (dma_mapping_error(dma_dev, pt_dma)) {
		dev_err(dma_dev, "DMA mapping error while allocating page table\n");
675 676 677
		free_page((unsigned long)page_table);
		return ERR_PTR(-ENOMEM);
	}
D
Daniel Kurtz 已提交
678

679 680
	dte = rk_mk_dte(pt_dma);
	*dte_addr = dte;
D
Daniel Kurtz 已提交
681

682 683 684
	rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
	rk_table_flush(rk_domain,
		       rk_domain->dt_dma + dte_index * sizeof(u32), 1);
D
Daniel Kurtz 已提交
685 686 687 688 689 690
done:
	pt_phys = rk_dte_pt_address(dte);
	return (u32 *)phys_to_virt(pt_phys);
}

static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
691 692
				  u32 *pte_addr, dma_addr_t pte_dma,
				  size_t size)
D
Daniel Kurtz 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706
{
	unsigned int pte_count;
	unsigned int pte_total = size / SPAGE_SIZE;

	assert_spin_locked(&rk_domain->dt_lock);

	for (pte_count = 0; pte_count < pte_total; pte_count++) {
		u32 pte = pte_addr[pte_count];
		if (!rk_pte_is_page_valid(pte))
			break;

		pte_addr[pte_count] = rk_mk_pte_invalid(pte);
	}

707
	rk_table_flush(rk_domain, pte_dma, pte_count);
D
Daniel Kurtz 已提交
708 709 710 711 712

	return pte_count * SPAGE_SIZE;
}

static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
713 714
			     dma_addr_t pte_dma, dma_addr_t iova,
			     phys_addr_t paddr, size_t size, int prot)
D
Daniel Kurtz 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
{
	unsigned int pte_count;
	unsigned int pte_total = size / SPAGE_SIZE;
	phys_addr_t page_phys;

	assert_spin_locked(&rk_domain->dt_lock);

	for (pte_count = 0; pte_count < pte_total; pte_count++) {
		u32 pte = pte_addr[pte_count];

		if (rk_pte_is_page_valid(pte))
			goto unwind;

		pte_addr[pte_count] = rk_mk_pte(paddr, prot);

		paddr += SPAGE_SIZE;
	}

733
	rk_table_flush(rk_domain, pte_dma, pte_total);
D
Daniel Kurtz 已提交
734

735 736 737 738 739 740 741 742
	/*
	 * Zap the first and last iova to evict from iotlb any previously
	 * mapped cachelines holding stale values for its dte and pte.
	 * We only zap the first and last iova, since only they could have
	 * dte or pte shared with an existing mapping.
	 */
	rk_iommu_zap_iova_first_last(rk_domain, iova, size);

D
Daniel Kurtz 已提交
743 744 745
	return 0;
unwind:
	/* Unmap the range of iovas that we just mapped */
746 747
	rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma,
			    pte_count * SPAGE_SIZE);
D
Daniel Kurtz 已提交
748 749 750 751 752 753 754 755 756 757 758 759

	iova += pte_count * SPAGE_SIZE;
	page_phys = rk_pte_page_address(pte_addr[pte_count]);
	pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
	       &iova, &page_phys, &paddr, prot);

	return -EADDRINUSE;
}

static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
			phys_addr_t paddr, size_t size, int prot)
{
760
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
D
Daniel Kurtz 已提交
761
	unsigned long flags;
762
	dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
D
Daniel Kurtz 已提交
763
	u32 *page_table, *pte_addr;
764
	u32 dte_index, pte_index;
D
Daniel Kurtz 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	int ret;

	spin_lock_irqsave(&rk_domain->dt_lock, flags);

	/*
	 * pgsize_bitmap specifies iova sizes that fit in one page table
	 * (1024 4-KiB pages = 4 MiB).
	 * So, size will always be 4096 <= size <= 4194304.
	 * Since iommu_map() guarantees that both iova and size will be
	 * aligned, we will always only be mapping from a single dte here.
	 */
	page_table = rk_dte_get_page_table(rk_domain, iova);
	if (IS_ERR(page_table)) {
		spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
		return PTR_ERR(page_table);
	}

782 783 784 785 786 787 788
	dte_index = rk_domain->dt[rk_iova_dte_index(iova)];
	pte_index = rk_iova_pte_index(iova);
	pte_addr = &page_table[pte_index];
	pte_dma = rk_dte_pt_address(dte_index) + pte_index * sizeof(u32);
	ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova,
				paddr, size, prot);

D
Daniel Kurtz 已提交
789 790 791 792 793 794 795 796
	spin_unlock_irqrestore(&rk_domain->dt_lock, flags);

	return ret;
}

static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
			     size_t size)
{
797
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
D
Daniel Kurtz 已提交
798
	unsigned long flags;
799
	dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
D
Daniel Kurtz 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	phys_addr_t pt_phys;
	u32 dte;
	u32 *pte_addr;
	size_t unmap_size;

	spin_lock_irqsave(&rk_domain->dt_lock, flags);

	/*
	 * pgsize_bitmap specifies iova sizes that fit in one page table
	 * (1024 4-KiB pages = 4 MiB).
	 * So, size will always be 4096 <= size <= 4194304.
	 * Since iommu_unmap() guarantees that both iova and size will be
	 * aligned, we will always only be unmapping from a single dte here.
	 */
	dte = rk_domain->dt[rk_iova_dte_index(iova)];
	/* Just return 0 if iova is unmapped */
	if (!rk_dte_is_pt_valid(dte)) {
		spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
		return 0;
	}

	pt_phys = rk_dte_pt_address(dte);
	pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
823 824
	pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
	unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
D
Daniel Kurtz 已提交
825 826 827 828 829 830 831 832 833 834 835

	spin_unlock_irqrestore(&rk_domain->dt_lock, flags);

	/* Shootdown iotlb entries for iova range that was just unmapped */
	rk_iommu_zap_iova(rk_domain, iova, unmap_size);

	return unmap_size;
}

static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
{
836
	struct rk_iommudata *data = dev->archdata.iommu;
D
Daniel Kurtz 已提交
837

838
	return data ? data->iommu : NULL;
D
Daniel Kurtz 已提交
839 840
}

841 842
/* Must be called with iommu powered on and attached */
static void rk_iommu_disable(struct rk_iommu *iommu)
D
Daniel Kurtz 已提交
843
{
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	int i;

	/* Ignore error while disabling, just keep going */
	WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
	rk_iommu_enable_stall(iommu);
	rk_iommu_disable_paging(iommu);
	for (i = 0; i < iommu->num_mmu; i++) {
		rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
		rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
	}
	rk_iommu_disable_stall(iommu);
	clk_bulk_disable(iommu->num_clocks, iommu->clocks);
}

/* Must be called with iommu powered on and attached */
static int rk_iommu_enable(struct rk_iommu *iommu)
{
	struct iommu_domain *domain = iommu->domain;
862
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
863
	int ret, i;
D
Daniel Kurtz 已提交
864

865
	ret = clk_bulk_enable(iommu->num_clocks, iommu->clocks);
D
Daniel Kurtz 已提交
866 867 868
	if (ret)
		return ret;

869 870 871 872
	ret = rk_iommu_enable_stall(iommu);
	if (ret)
		goto out_disable_clocks;

D
Daniel Kurtz 已提交
873 874
	ret = rk_iommu_force_reset(iommu);
	if (ret)
875
		goto out_disable_stall;
D
Daniel Kurtz 已提交
876

877
	for (i = 0; i < iommu->num_mmu; i++) {
878 879
		rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR,
			       rk_domain->dt_dma);
880
		rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
881 882
		rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
	}
D
Daniel Kurtz 已提交
883 884 885

	ret = rk_iommu_enable_paging(iommu);

886
out_disable_stall:
D
Daniel Kurtz 已提交
887
	rk_iommu_disable_stall(iommu);
888 889
out_disable_clocks:
	clk_bulk_disable(iommu->num_clocks, iommu->clocks);
890
	return ret;
D
Daniel Kurtz 已提交
891 892 893 894 895 896
}

static void rk_iommu_detach_device(struct iommu_domain *domain,
				   struct device *dev)
{
	struct rk_iommu *iommu;
897
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
D
Daniel Kurtz 已提交
898
	unsigned long flags;
899
	int ret;
D
Daniel Kurtz 已提交
900 901 902 903 904 905

	/* Allow 'virtual devices' (eg drm) to detach from domain */
	iommu = rk_iommu_from_dev(dev);
	if (!iommu)
		return;

906 907 908 909 910 911 912 913
	dev_dbg(dev, "Detaching from iommu domain\n");

	/* iommu already detached */
	if (iommu->domain != domain)
		return;

	iommu->domain = NULL;

D
Daniel Kurtz 已提交
914 915 916 917
	spin_lock_irqsave(&rk_domain->iommus_lock, flags);
	list_del_init(&iommu->node);
	spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);

918 919 920
	ret = pm_runtime_get_if_in_use(iommu->dev);
	WARN_ON_ONCE(ret < 0);
	if (ret > 0) {
921 922
		rk_iommu_disable(iommu);
		pm_runtime_put(iommu->dev);
923
	}
924
}
D
Daniel Kurtz 已提交
925

926 927 928 929 930 931 932
static int rk_iommu_attach_device(struct iommu_domain *domain,
		struct device *dev)
{
	struct rk_iommu *iommu;
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
	unsigned long flags;
	int ret;
D
Daniel Kurtz 已提交
933

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	/*
	 * Allow 'virtual devices' (e.g., drm) to attach to domain.
	 * Such a device does not belong to an iommu group.
	 */
	iommu = rk_iommu_from_dev(dev);
	if (!iommu)
		return 0;

	dev_dbg(dev, "Attaching to iommu domain\n");

	/* iommu already attached */
	if (iommu->domain == domain)
		return 0;

	if (iommu->domain)
		rk_iommu_detach_device(iommu->domain, dev);

	iommu->domain = domain;

	spin_lock_irqsave(&rk_domain->iommus_lock, flags);
	list_add_tail(&iommu->node, &rk_domain->iommus);
	spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);

957 958
	ret = pm_runtime_get_if_in_use(iommu->dev);
	if (!ret || WARN_ON_ONCE(ret < 0))
959 960 961 962 963 964 965 966 967
		return 0;

	ret = rk_iommu_enable(iommu);
	if (ret)
		rk_iommu_detach_device(iommu->domain, dev);

	pm_runtime_put(iommu->dev);

	return ret;
D
Daniel Kurtz 已提交
968 969
}

970
static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
D
Daniel Kurtz 已提交
971 972 973
{
	struct rk_iommu_domain *rk_domain;

974
	if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
975 976
		return NULL;

977
	if (!dma_dev)
978
		return NULL;
D
Daniel Kurtz 已提交
979

980
	rk_domain = devm_kzalloc(dma_dev, sizeof(*rk_domain), GFP_KERNEL);
981
	if (!rk_domain)
982
		return NULL;
983

984 985
	if (type == IOMMU_DOMAIN_DMA &&
	    iommu_get_dma_cookie(&rk_domain->domain))
986
		return NULL;
987

D
Daniel Kurtz 已提交
988 989 990 991 992 993 994
	/*
	 * rk32xx iommus use a 2 level pagetable.
	 * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
	 * Allocate one 4 KiB page for each table.
	 */
	rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
	if (!rk_domain->dt)
995 996
		goto err_put_cookie;

997
	rk_domain->dt_dma = dma_map_single(dma_dev, rk_domain->dt,
998
					   SPAGE_SIZE, DMA_TO_DEVICE);
999 1000
	if (dma_mapping_error(dma_dev, rk_domain->dt_dma)) {
		dev_err(dma_dev, "DMA map error for DT\n");
1001 1002
		goto err_free_dt;
	}
D
Daniel Kurtz 已提交
1003

1004
	rk_table_flush(rk_domain, rk_domain->dt_dma, NUM_DT_ENTRIES);
D
Daniel Kurtz 已提交
1005 1006 1007 1008 1009

	spin_lock_init(&rk_domain->iommus_lock);
	spin_lock_init(&rk_domain->dt_lock);
	INIT_LIST_HEAD(&rk_domain->iommus);

1010 1011 1012 1013
	rk_domain->domain.geometry.aperture_start = 0;
	rk_domain->domain.geometry.aperture_end   = DMA_BIT_MASK(32);
	rk_domain->domain.geometry.force_aperture = true;

1014
	return &rk_domain->domain;
D
Daniel Kurtz 已提交
1015

1016 1017 1018
err_free_dt:
	free_page((unsigned long)rk_domain->dt);
err_put_cookie:
1019 1020
	if (type == IOMMU_DOMAIN_DMA)
		iommu_put_dma_cookie(&rk_domain->domain);
1021

1022
	return NULL;
D
Daniel Kurtz 已提交
1023 1024
}

1025
static void rk_iommu_domain_free(struct iommu_domain *domain)
D
Daniel Kurtz 已提交
1026
{
1027
	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
D
Daniel Kurtz 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036
	int i;

	WARN_ON(!list_empty(&rk_domain->iommus));

	for (i = 0; i < NUM_DT_ENTRIES; i++) {
		u32 dte = rk_domain->dt[i];
		if (rk_dte_is_pt_valid(dte)) {
			phys_addr_t pt_phys = rk_dte_pt_address(dte);
			u32 *page_table = phys_to_virt(pt_phys);
1037
			dma_unmap_single(dma_dev, pt_phys,
1038
					 SPAGE_SIZE, DMA_TO_DEVICE);
D
Daniel Kurtz 已提交
1039 1040 1041 1042
			free_page((unsigned long)page_table);
		}
	}

1043
	dma_unmap_single(dma_dev, rk_domain->dt_dma,
1044
			 SPAGE_SIZE, DMA_TO_DEVICE);
D
Daniel Kurtz 已提交
1045
	free_page((unsigned long)rk_domain->dt);
1046

1047 1048
	if (domain->type == IOMMU_DOMAIN_DMA)
		iommu_put_dma_cookie(&rk_domain->domain);
D
Daniel Kurtz 已提交
1049 1050
}

1051
static int rk_iommu_add_device(struct device *dev)
D
Daniel Kurtz 已提交
1052
{
1053 1054
	struct iommu_group *group;
	struct rk_iommu *iommu;
1055
	struct rk_iommudata *data;
D
Daniel Kurtz 已提交
1056

1057 1058
	data = dev->archdata.iommu;
	if (!data)
1059
		return -ENODEV;
D
Daniel Kurtz 已提交
1060

1061 1062
	iommu = rk_iommu_from_dev(dev);

1063 1064 1065 1066
	group = iommu_group_get_for_dev(dev);
	if (IS_ERR(group))
		return PTR_ERR(group);
	iommu_group_put(group);
D
Daniel Kurtz 已提交
1067

1068
	iommu_device_link(&iommu->iommu, dev);
1069
	data->link = device_link_add(dev, iommu->dev, DL_FLAG_PM_RUNTIME);
D
Daniel Kurtz 已提交
1070 1071 1072 1073

	return 0;
}

1074
static void rk_iommu_remove_device(struct device *dev)
D
Daniel Kurtz 已提交
1075
{
1076
	struct rk_iommu *iommu;
1077
	struct rk_iommudata *data = dev->archdata.iommu;
D
Daniel Kurtz 已提交
1078

1079 1080
	iommu = rk_iommu_from_dev(dev);

1081
	device_link_del(data->link);
1082
	iommu_device_unlink(&iommu->iommu, dev);
D
Daniel Kurtz 已提交
1083 1084 1085
	iommu_group_remove_device(dev);
}

1086 1087 1088 1089 1090 1091 1092 1093 1094
static struct iommu_group *rk_iommu_device_group(struct device *dev)
{
	struct rk_iommu *iommu;

	iommu = rk_iommu_from_dev(dev);

	return iommu_group_ref_get(iommu->group);
}

1095 1096
static int rk_iommu_of_xlate(struct device *dev,
			     struct of_phandle_args *args)
D
Daniel Kurtz 已提交
1097
{
1098 1099
	struct platform_device *iommu_dev;
	struct rk_iommudata *data;
1100

1101 1102 1103
	data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;
D
Daniel Kurtz 已提交
1104

1105
	iommu_dev = of_find_device_by_node(args->np);
1106

1107 1108 1109
	data->iommu = platform_get_drvdata(iommu_dev);
	dev->archdata.iommu = data;

1110
	platform_device_put(iommu_dev);
1111 1112

	return 0;
D
Daniel Kurtz 已提交
1113 1114 1115
}

static const struct iommu_ops rk_iommu_ops = {
1116 1117
	.domain_alloc = rk_iommu_domain_alloc,
	.domain_free = rk_iommu_domain_free,
D
Daniel Kurtz 已提交
1118 1119 1120 1121 1122 1123 1124
	.attach_dev = rk_iommu_attach_device,
	.detach_dev = rk_iommu_detach_device,
	.map = rk_iommu_map,
	.unmap = rk_iommu_unmap,
	.add_device = rk_iommu_add_device,
	.remove_device = rk_iommu_remove_device,
	.iova_to_phys = rk_iommu_iova_to_phys,
1125
	.device_group = rk_iommu_device_group,
D
Daniel Kurtz 已提交
1126
	.pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1127
	.of_xlate = rk_iommu_of_xlate,
D
Daniel Kurtz 已提交
1128 1129 1130 1131 1132 1133 1134
};

static int rk_iommu_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct rk_iommu *iommu;
	struct resource *res;
1135
	int num_res = pdev->num_resources;
1136
	int err, i, irq;
D
Daniel Kurtz 已提交
1137 1138 1139 1140 1141 1142 1143

	iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
	if (!iommu)
		return -ENOMEM;

	platform_set_drvdata(pdev, iommu);
	iommu->dev = dev;
1144
	iommu->num_mmu = 0;
1145

1146
	iommu->bases = devm_kcalloc(dev, num_res, sizeof(*iommu->bases),
1147 1148 1149
				    GFP_KERNEL);
	if (!iommu->bases)
		return -ENOMEM;
D
Daniel Kurtz 已提交
1150

1151
	for (i = 0; i < num_res; i++) {
1152
		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1153 1154
		if (!res)
			continue;
1155 1156 1157 1158 1159 1160 1161
		iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
		if (IS_ERR(iommu->bases[i]))
			continue;
		iommu->num_mmu++;
	}
	if (iommu->num_mmu == 0)
		return PTR_ERR(iommu->bases[0]);
D
Daniel Kurtz 已提交
1162

1163 1164 1165
	iommu->reset_disabled = device_property_read_bool(dev,
					"rockchip,disable-mmu-reset");

1166 1167 1168 1169 1170 1171 1172 1173 1174
	iommu->num_clocks = ARRAY_SIZE(rk_iommu_clocks);
	iommu->clocks = devm_kcalloc(iommu->dev, iommu->num_clocks,
				     sizeof(*iommu->clocks), GFP_KERNEL);
	if (!iommu->clocks)
		return -ENOMEM;

	for (i = 0; i < iommu->num_clocks; ++i)
		iommu->clocks[i].id = rk_iommu_clocks[i];

1175 1176 1177 1178 1179
	/*
	 * iommu clocks should be present for all new devices and devicetrees
	 * but there are older devicetrees without clocks out in the wild.
	 * So clocks as optional for the time being.
	 */
1180
	err = devm_clk_bulk_get(iommu->dev, iommu->num_clocks, iommu->clocks);
1181 1182 1183
	if (err == -ENOENT)
		iommu->num_clocks = 0;
	else if (err)
1184 1185 1186
		return err;

	err = clk_bulk_prepare(iommu->num_clocks, iommu->clocks);
1187 1188 1189
	if (err)
		return err;

1190 1191 1192 1193 1194 1195
	iommu->group = iommu_group_alloc();
	if (IS_ERR(iommu->group)) {
		err = PTR_ERR(iommu->group);
		goto err_unprepare_clocks;
	}

1196 1197
	err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev));
	if (err)
1198
		goto err_put_group;
1199

1200
	iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops);
1201 1202
	iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode);

1203
	err = iommu_device_register(&iommu->iommu);
1204
	if (err)
1205
		goto err_remove_sysfs;
1206

1207 1208 1209 1210 1211 1212 1213 1214
	/*
	 * Use the first registered IOMMU device for domain to use with DMA
	 * API, since a domain might not physically correspond to a single
	 * IOMMU device..
	 */
	if (!dma_dev)
		dma_dev = &pdev->dev;

1215 1216
	bus_set_iommu(&platform_bus_type, &rk_iommu_ops);

1217 1218
	pm_runtime_enable(dev);

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	i = 0;
	while ((irq = platform_get_irq(pdev, i++)) != -ENXIO) {
		if (irq < 0)
			return irq;

		err = devm_request_irq(iommu->dev, irq, rk_iommu_irq,
				       IRQF_SHARED, dev_name(dev), iommu);
		if (err) {
			pm_runtime_disable(dev);
			goto err_remove_sysfs;
		}
	}

1232 1233 1234
	return 0;
err_remove_sysfs:
	iommu_device_sysfs_remove(&iommu->iommu);
1235 1236
err_put_group:
	iommu_group_put(iommu->group);
1237 1238
err_unprepare_clocks:
	clk_bulk_unprepare(iommu->num_clocks, iommu->clocks);
1239
	return err;
D
Daniel Kurtz 已提交
1240 1241
}

1242 1243
static void rk_iommu_shutdown(struct platform_device *pdev)
{
1244 1245 1246 1247 1248 1249
	struct rk_iommu *iommu = platform_get_drvdata(pdev);
	int i = 0, irq;

	while ((irq = platform_get_irq(pdev, i++)) != -ENXIO)
		devm_free_irq(iommu->dev, irq, iommu);

1250 1251
	pm_runtime_force_suspend(&pdev->dev);
}
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
static int __maybe_unused rk_iommu_suspend(struct device *dev)
{
	struct rk_iommu *iommu = dev_get_drvdata(dev);

	if (!iommu->domain)
		return 0;

	rk_iommu_disable(iommu);
	return 0;
}

static int __maybe_unused rk_iommu_resume(struct device *dev)
{
	struct rk_iommu *iommu = dev_get_drvdata(dev);

	if (!iommu->domain)
		return 0;

	return rk_iommu_enable(iommu);
1272 1273
}

1274 1275 1276 1277 1278 1279
static const struct dev_pm_ops rk_iommu_pm_ops = {
	SET_RUNTIME_PM_OPS(rk_iommu_suspend, rk_iommu_resume, NULL)
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
};

D
Daniel Kurtz 已提交
1280 1281 1282 1283 1284 1285 1286 1287
static const struct of_device_id rk_iommu_dt_ids[] = {
	{ .compatible = "rockchip,iommu" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);

static struct platform_driver rk_iommu_driver = {
	.probe = rk_iommu_probe,
1288
	.shutdown = rk_iommu_shutdown,
D
Daniel Kurtz 已提交
1289 1290
	.driver = {
		   .name = "rk_iommu",
1291
		   .of_match_table = rk_iommu_dt_ids,
1292
		   .pm = &rk_iommu_pm_ops,
1293
		   .suppress_bind_attrs = true,
D
Daniel Kurtz 已提交
1294 1295 1296 1297 1298
	},
};

static int __init rk_iommu_init(void)
{
1299
	return platform_driver_register(&rk_iommu_driver);
D
Daniel Kurtz 已提交
1300 1301 1302 1303 1304 1305 1306
}
subsys_initcall(rk_iommu_init);

MODULE_DESCRIPTION("IOMMU API for Rockchip");
MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
MODULE_ALIAS("platform:rockchip-iommu");
MODULE_LICENSE("GPL v2");