ad914dd174439719617e6f9e92a345471d811b58
[firefly-linux-kernel-4.4.55.git] / drivers / iommu / rockchip-iommu.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  */
6
7 #include <linux/clk.h>
8 #include <linux/compiler.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/dma-iommu.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/iommu.h>
16 #include <linux/jiffies.h>
17 #include <linux/list.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26
27 /** MMU register offsets */
28 #define RK_MMU_DTE_ADDR         0x00    /* Directory table address */
29 #define RK_MMU_STATUS           0x04
30 #define RK_MMU_COMMAND          0x08
31 #define RK_MMU_PAGE_FAULT_ADDR  0x0C    /* IOVA of last page fault */
32 #define RK_MMU_ZAP_ONE_LINE     0x10    /* Shootdown one IOTLB entry */
33 #define RK_MMU_INT_RAWSTAT      0x14    /* IRQ status ignoring mask */
34 #define RK_MMU_INT_CLEAR        0x18    /* Acknowledge and re-arm irq */
35 #define RK_MMU_INT_MASK         0x1C    /* IRQ enable */
36 #define RK_MMU_INT_STATUS       0x20    /* IRQ status after masking */
37 #define RK_MMU_AUTO_GATING      0x24
38
39 #define DTE_ADDR_DUMMY          0xCAFEBABE
40 #define FORCE_RESET_TIMEOUT     100     /* ms */
41
42 /* RK_MMU_STATUS fields */
43 #define RK_MMU_STATUS_PAGING_ENABLED       BIT(0)
44 #define RK_MMU_STATUS_PAGE_FAULT_ACTIVE    BIT(1)
45 #define RK_MMU_STATUS_STALL_ACTIVE         BIT(2)
46 #define RK_MMU_STATUS_IDLE                 BIT(3)
47 #define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY  BIT(4)
48 #define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE  BIT(5)
49 #define RK_MMU_STATUS_STALL_NOT_ACTIVE     BIT(31)
50
51 /* RK_MMU_COMMAND command values */
52 #define RK_MMU_CMD_ENABLE_PAGING    0  /* Enable memory translation */
53 #define RK_MMU_CMD_DISABLE_PAGING   1  /* Disable memory translation */
54 #define RK_MMU_CMD_ENABLE_STALL     2  /* Stall paging to allow other cmds */
55 #define RK_MMU_CMD_DISABLE_STALL    3  /* Stop stall re-enables paging */
56 #define RK_MMU_CMD_ZAP_CACHE        4  /* Shoot down entire IOTLB */
57 #define RK_MMU_CMD_PAGE_FAULT_DONE  5  /* Clear page fault */
58 #define RK_MMU_CMD_FORCE_RESET      6  /* Reset all registers */
59
60 /* RK_MMU_INT_* register fields */
61 #define RK_MMU_IRQ_PAGE_FAULT    0x01  /* page fault */
62 #define RK_MMU_IRQ_BUS_ERROR     0x02  /* bus read error */
63 #define RK_MMU_IRQ_MASK          (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR)
64
65 #define NUM_DT_ENTRIES 1024
66 #define NUM_PT_ENTRIES 1024
67
68 #define SPAGE_ORDER 12
69 #define SPAGE_SIZE (1 << SPAGE_ORDER)
70
71  /*
72   * Support mapping any size that fits in one page table:
73   *   4 KiB to 4 MiB
74   */
75 #define RK_IOMMU_PGSIZE_BITMAP 0x007ff000
76
77 #define IOMMU_REG_POLL_COUNT_FAST 1000
78
79 struct rk_iommu_domain {
80         struct list_head iommus;
81         struct platform_device *pdev;
82         u32 *dt; /* page directory table */
83         dma_addr_t dt_dma;
84         struct mutex iommus_lock; /* lock for iommus list */
85         struct mutex dt_lock; /* lock for modifying page directory table */
86
87         struct iommu_domain domain;
88 };
89
90 struct rk_iommu {
91         struct device *dev;
92         void __iomem **bases;
93         int num_mmu;
94         int irq;
95         bool reset_disabled; /* isp iommu reset operation would failed */
96         struct list_head node; /* entry in rk_iommu_domain.iommus */
97         struct iommu_domain *domain; /* domain to which iommu is attached */
98         struct clk *aclk; /* aclock belong to master */
99         struct clk *hclk; /* hclock belong to master */
100 };
101
102 static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma,
103                                   unsigned int count)
104 {
105         size_t size = count * sizeof(u32); /* count of u32 entry */
106
107         dma_sync_single_for_device(&dom->pdev->dev, dma, size, DMA_TO_DEVICE);
108 }
109
110 static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
111 {
112         return container_of(dom, struct rk_iommu_domain, domain);
113 }
114
115 /**
116  * Inspired by _wait_for in intel_drv.h
117  * This is NOT safe for use in interrupt context.
118  *
119  * Note that it's important that we check the condition again after having
120  * timed out, since the timeout could be due to preemption or similar and
121  * we've never had a chance to check the condition before the timeout.
122  */
123 #define rk_wait_for(COND, MS) ({ \
124         unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;   \
125         int ret__ = 0;                                                  \
126         while (!(COND)) {                                               \
127                 if (time_after(jiffies, timeout__)) {                   \
128                         ret__ = (COND) ? 0 : -ETIMEDOUT;                \
129                         break;                                          \
130                 }                                                       \
131                 usleep_range(50, 100);                                  \
132         }                                                               \
133         ret__;                                                          \
134 })
135
136 /*
137  * The Rockchip rk3288 iommu uses a 2-level page table.
138  * The first level is the "Directory Table" (DT).
139  * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing
140  * to a "Page Table".
141  * The second level is the 1024 Page Tables (PT).
142  * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to
143  * a 4 KB page of physical memory.
144  *
145  * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries).
146  * Each iommu device has a MMU_DTE_ADDR register that contains the physical
147  * address of the start of the DT page.
148  *
149  * The structure of the page table is as follows:
150  *
151  *                   DT
152  * MMU_DTE_ADDR -> +-----+
153  *                 |     |
154  *                 +-----+     PT
155  *                 | DTE | -> +-----+
156  *                 +-----+    |     |     Memory
157  *                 |     |    +-----+     Page
158  *                 |     |    | PTE | -> +-----+
159  *                 +-----+    +-----+    |     |
160  *                            |     |    |     |
161  *                            |     |    |     |
162  *                            +-----+    |     |
163  *                                       |     |
164  *                                       |     |
165  *                                       +-----+
166  */
167
168 /*
169  * Each DTE has a PT address and a valid bit:
170  * +---------------------+-----------+-+
171  * | PT address          | Reserved  |V|
172  * +---------------------+-----------+-+
173  *  31:12 - PT address (PTs always starts on a 4 KB boundary)
174  *  11: 1 - Reserved
175  *      0 - 1 if PT @ PT address is valid
176  */
177 #define RK_DTE_PT_ADDRESS_MASK    0xfffff000
178 #define RK_DTE_PT_VALID           BIT(0)
179
180 static inline phys_addr_t rk_dte_pt_address(u32 dte)
181 {
182         return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK;
183 }
184
185 static inline bool rk_dte_is_pt_valid(u32 dte)
186 {
187         return dte & RK_DTE_PT_VALID;
188 }
189
190 static inline u32 rk_mk_dte(dma_addr_t pt_dma)
191 {
192         return (pt_dma & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID;
193 }
194
195 /*
196  * Each PTE has a Page address, some flags and a valid bit:
197  * +---------------------+---+-------+-+
198  * | Page address        |Rsv| Flags |V|
199  * +---------------------+---+-------+-+
200  *  31:12 - Page address (Pages always start on a 4 KB boundary)
201  *  11: 9 - Reserved
202  *   8: 1 - Flags
203  *      8 - Read allocate - allocate cache space on read misses
204  *      7 - Read cache - enable cache & prefetch of data
205  *      6 - Write buffer - enable delaying writes on their way to memory
206  *      5 - Write allocate - allocate cache space on write misses
207  *      4 - Write cache - different writes can be merged together
208  *      3 - Override cache attributes
209  *          if 1, bits 4-8 control cache attributes
210  *          if 0, the system bus defaults are used
211  *      2 - Writable
212  *      1 - Readable
213  *      0 - 1 if Page @ Page address is valid
214  */
215 #define RK_PTE_PAGE_ADDRESS_MASK  0xfffff000
216 #define RK_PTE_PAGE_FLAGS_MASK    0x000001fe
217 #define RK_PTE_PAGE_WRITABLE      BIT(2)
218 #define RK_PTE_PAGE_READABLE      BIT(1)
219 #define RK_PTE_PAGE_VALID         BIT(0)
220
221 static inline phys_addr_t rk_pte_page_address(u32 pte)
222 {
223         return (phys_addr_t)pte & RK_PTE_PAGE_ADDRESS_MASK;
224 }
225
226 static inline bool rk_pte_is_page_valid(u32 pte)
227 {
228         return pte & RK_PTE_PAGE_VALID;
229 }
230
231 /* TODO: set cache flags per prot IOMMU_CACHE */
232 static u32 rk_mk_pte(phys_addr_t page, int prot)
233 {
234         u32 flags = 0;
235         flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0;
236         flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0;
237         page &= RK_PTE_PAGE_ADDRESS_MASK;
238         return page | flags | RK_PTE_PAGE_VALID;
239 }
240
241 static u32 rk_mk_pte_invalid(u32 pte)
242 {
243         return pte & ~RK_PTE_PAGE_VALID;
244 }
245
246 /*
247  * rk3288 iova (IOMMU Virtual Address) format
248  *  31       22.21       12.11          0
249  * +-----------+-----------+-------------+
250  * | DTE index | PTE index | Page offset |
251  * +-----------+-----------+-------------+
252  *  31:22 - DTE index   - index of DTE in DT
253  *  21:12 - PTE index   - index of PTE in PT @ DTE.pt_address
254  *  11: 0 - Page offset - offset into page @ PTE.page_address
255  */
256 #define RK_IOVA_DTE_MASK    0xffc00000
257 #define RK_IOVA_DTE_SHIFT   22
258 #define RK_IOVA_PTE_MASK    0x003ff000
259 #define RK_IOVA_PTE_SHIFT   12
260 #define RK_IOVA_PAGE_MASK   0x00000fff
261 #define RK_IOVA_PAGE_SHIFT  0
262
263 static void rk_iommu_power_on(struct rk_iommu *iommu)
264 {
265         if (iommu->aclk && iommu->hclk) {
266                 clk_enable(iommu->aclk);
267                 clk_enable(iommu->hclk);
268         }
269
270         pm_runtime_get_sync(iommu->dev);
271 }
272
273 static void rk_iommu_power_off(struct rk_iommu *iommu)
274 {
275         pm_runtime_put_sync(iommu->dev);
276
277         if (iommu->aclk && iommu->hclk) {
278                 clk_disable(iommu->aclk);
279                 clk_disable(iommu->hclk);
280         }
281 }
282
283 static u32 rk_iova_dte_index(dma_addr_t iova)
284 {
285         return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT;
286 }
287
288 static u32 rk_iova_pte_index(dma_addr_t iova)
289 {
290         return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT;
291 }
292
293 static u32 rk_iova_page_offset(dma_addr_t iova)
294 {
295         return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT;
296 }
297
298 static u32 rk_iommu_read(void __iomem *base, u32 offset)
299 {
300         return readl(base + offset);
301 }
302
303 static void rk_iommu_write(void __iomem *base, u32 offset, u32 value)
304 {
305         writel(value, base + offset);
306 }
307
308 static void rk_iommu_command(struct rk_iommu *iommu, u32 command)
309 {
310         int i;
311
312         for (i = 0; i < iommu->num_mmu; i++)
313                 writel(command, iommu->bases[i] + RK_MMU_COMMAND);
314 }
315
316 static void rk_iommu_base_command(void __iomem *base, u32 command)
317 {
318         writel(command, base + RK_MMU_COMMAND);
319 }
320 static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start,
321                                size_t size)
322 {
323         int i;
324         dma_addr_t iova_end = iova_start + size;
325         /*
326          * TODO(djkurtz): Figure out when it is more efficient to shootdown the
327          * entire iotlb rather than iterate over individual iovas.
328          */
329
330         rk_iommu_power_on(iommu);
331
332         for (i = 0; i < iommu->num_mmu; i++) {
333                 dma_addr_t iova;
334
335                 for (iova = iova_start; iova < iova_end; iova += SPAGE_SIZE)
336                         rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova);
337         }
338
339         rk_iommu_power_off(iommu);
340 }
341
342 static bool rk_iommu_is_stall_active(struct rk_iommu *iommu)
343 {
344         bool active = true;
345         int i;
346
347         for (i = 0; i < iommu->num_mmu; i++)
348                 active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
349                                         RK_MMU_STATUS_STALL_ACTIVE);
350
351         return active;
352 }
353
354 static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu)
355 {
356         bool enable = true;
357         int i;
358
359         for (i = 0; i < iommu->num_mmu; i++)
360                 enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
361                                         RK_MMU_STATUS_PAGING_ENABLED);
362
363         return enable;
364 }
365
366 static int rk_iommu_enable_stall(struct rk_iommu *iommu)
367 {
368         int ret, i;
369
370         if (rk_iommu_is_stall_active(iommu))
371                 return 0;
372
373         /* Stall can only be enabled if paging is enabled */
374         if (!rk_iommu_is_paging_enabled(iommu))
375                 return 0;
376
377         rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL);
378
379         ret = rk_wait_for(rk_iommu_is_stall_active(iommu), 1);
380         if (ret)
381                 for (i = 0; i < iommu->num_mmu; i++)
382                         dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n",
383                                 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
384
385         return ret;
386 }
387
388 static int rk_iommu_disable_stall(struct rk_iommu *iommu)
389 {
390         int ret, i;
391
392         if (!rk_iommu_is_stall_active(iommu))
393                 return 0;
394
395         rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL);
396
397         ret = rk_wait_for(!rk_iommu_is_stall_active(iommu), 1);
398         if (ret)
399                 for (i = 0; i < iommu->num_mmu; i++)
400                         dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n",
401                                 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
402
403         return ret;
404 }
405
406 static int rk_iommu_enable_paging(struct rk_iommu *iommu)
407 {
408         int ret, i;
409
410         if (rk_iommu_is_paging_enabled(iommu))
411                 return 0;
412
413         rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING);
414
415         ret = rk_wait_for(rk_iommu_is_paging_enabled(iommu), 1);
416         if (ret)
417                 for (i = 0; i < iommu->num_mmu; i++)
418                         dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n",
419                                 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
420
421         return ret;
422 }
423
424 static int rk_iommu_disable_paging(struct rk_iommu *iommu)
425 {
426         int ret, i;
427
428         if (!rk_iommu_is_paging_enabled(iommu))
429                 return 0;
430
431         rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING);
432
433         ret = rk_wait_for(!rk_iommu_is_paging_enabled(iommu), 1);
434         if (ret)
435                 for (i = 0; i < iommu->num_mmu; i++)
436                         dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n",
437                                 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
438
439         return ret;
440 }
441
442 static int rk_iommu_force_reset(struct rk_iommu *iommu)
443 {
444         int ret, i;
445         u32 dte_addr;
446
447         /* Workaround for isp mmus */
448         if (iommu->reset_disabled)
449                 return 0;
450
451         /*
452          * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY
453          * and verifying that upper 5 nybbles are read back.
454          */
455         for (i = 0; i < iommu->num_mmu; i++) {
456                 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, DTE_ADDR_DUMMY);
457
458                 dte_addr = rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR);
459                 if (dte_addr != (DTE_ADDR_DUMMY & RK_DTE_PT_ADDRESS_MASK)) {
460                         dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n");
461                         return -EFAULT;
462                 }
463         }
464
465         rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET);
466
467         for (i = 0; i < iommu->num_mmu; i++) {
468                 ret = rk_wait_for(rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0x00000000,
469                                   FORCE_RESET_TIMEOUT);
470                 if (ret) {
471                         dev_err(iommu->dev, "FORCE_RESET command timed out\n");
472                         return ret;
473                 }
474         }
475
476         return 0;
477 }
478
479 static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova)
480 {
481         void __iomem *base = iommu->bases[index];
482         u32 dte_index, pte_index, page_offset;
483         u32 mmu_dte_addr;
484         phys_addr_t mmu_dte_addr_phys, dte_addr_phys;
485         u32 *dte_addr;
486         u32 dte;
487         phys_addr_t pte_addr_phys = 0;
488         u32 *pte_addr = NULL;
489         u32 pte = 0;
490         phys_addr_t page_addr_phys = 0;
491         u32 page_flags = 0;
492
493         dte_index = rk_iova_dte_index(iova);
494         pte_index = rk_iova_pte_index(iova);
495         page_offset = rk_iova_page_offset(iova);
496
497         mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR);
498         mmu_dte_addr_phys = (phys_addr_t)mmu_dte_addr;
499
500         dte_addr_phys = mmu_dte_addr_phys + (4 * dte_index);
501         dte_addr = phys_to_virt(dte_addr_phys);
502         dte = *dte_addr;
503
504         if (!rk_dte_is_pt_valid(dte))
505                 goto print_it;
506
507         pte_addr_phys = rk_dte_pt_address(dte) + (pte_index * 4);
508         pte_addr = phys_to_virt(pte_addr_phys);
509         pte = *pte_addr;
510
511         if (!rk_pte_is_page_valid(pte))
512                 goto print_it;
513
514         page_addr_phys = rk_pte_page_address(pte) + page_offset;
515         page_flags = pte & RK_PTE_PAGE_FLAGS_MASK;
516
517 print_it:
518         dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n",
519                 &iova, dte_index, pte_index, page_offset);
520         dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n",
521                 &mmu_dte_addr_phys, &dte_addr_phys, dte,
522                 rk_dte_is_pt_valid(dte), &pte_addr_phys, pte,
523                 rk_pte_is_page_valid(pte), &page_addr_phys, page_flags);
524 }
525
526 static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
527 {
528         struct rk_iommu *iommu = dev_id;
529         u32 status;
530         u32 int_status;
531         dma_addr_t iova;
532         irqreturn_t ret = IRQ_NONE;
533         int i;
534
535         for (i = 0; i < iommu->num_mmu; i++) {
536                 int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
537                 if (int_status == 0)
538                         continue;
539
540                 ret = IRQ_HANDLED;
541                 iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
542
543                 if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
544                         int flags;
545
546                         status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
547                         flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ?
548                                         IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
549
550                         dev_err(iommu->dev, "Page fault at %pad of type %s\n",
551                                 &iova,
552                                 (flags == IOMMU_FAULT_WRITE) ? "write" : "read");
553
554                         log_iova(iommu, i, iova);
555
556                         /*
557                          * Report page fault to any installed handlers.
558                          * Ignore the return code, though, since we always zap cache
559                          * and clear the page fault anyway.
560                          */
561                         if (iommu->domain)
562                                 report_iommu_fault(iommu->domain, iommu->dev, iova,
563                                                    flags);
564                         else
565                                 dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
566
567                         rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
568                         rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
569                 }
570
571                 if (int_status & RK_MMU_IRQ_BUS_ERROR)
572                         dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
573
574                 if (int_status & ~RK_MMU_IRQ_MASK)
575                         dev_err(iommu->dev, "unexpected int_status: %#08x\n",
576                                 int_status);
577
578                 rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
579         }
580
581         return ret;
582 }
583
584 static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain,
585                                          dma_addr_t iova)
586 {
587         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
588         phys_addr_t pt_phys, phys = 0;
589         u32 dte, pte;
590         u32 *page_table;
591
592         mutex_lock(&rk_domain->dt_lock);
593
594         dte = rk_domain->dt[rk_iova_dte_index(iova)];
595         if (!rk_dte_is_pt_valid(dte))
596                 goto out;
597
598         pt_phys = rk_dte_pt_address(dte);
599         page_table = (u32 *)phys_to_virt(pt_phys);
600         pte = page_table[rk_iova_pte_index(iova)];
601         if (!rk_pte_is_page_valid(pte))
602                 goto out;
603
604         phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
605 out:
606         mutex_unlock(&rk_domain->dt_lock);
607
608         return phys;
609 }
610
611 static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain,
612                               dma_addr_t iova, size_t size)
613 {
614         struct list_head *pos;
615
616         /* shootdown these iova from all iommus using this domain */
617         mutex_lock(&rk_domain->iommus_lock);
618         list_for_each(pos, &rk_domain->iommus) {
619                 struct rk_iommu *iommu;
620                 iommu = list_entry(pos, struct rk_iommu, node);
621                 rk_iommu_zap_lines(iommu, iova, size);
622         }
623         mutex_unlock(&rk_domain->iommus_lock);
624 }
625
626 static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
627                                          dma_addr_t iova, size_t size)
628 {
629         rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
630         if (size > SPAGE_SIZE)
631                 rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE,
632                                         SPAGE_SIZE);
633 }
634
635 static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
636                                   dma_addr_t iova)
637 {
638         struct device *dev = &rk_domain->pdev->dev;
639         u32 *page_table, *dte_addr;
640         u32 dte_index, dte;
641         phys_addr_t pt_phys;
642         dma_addr_t pt_dma;
643
644         WARN_ON(!mutex_is_locked(&rk_domain->dt_lock));
645
646         dte_index = rk_iova_dte_index(iova);
647         dte_addr = &rk_domain->dt[dte_index];
648         dte = *dte_addr;
649         if (rk_dte_is_pt_valid(dte))
650                 goto done;
651
652         page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
653         if (!page_table)
654                 return ERR_PTR(-ENOMEM);
655
656         pt_dma = dma_map_single(dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
657         if (dma_mapping_error(dev, pt_dma)) {
658                 dev_err(dev, "DMA mapping error while allocating page table\n");
659                 free_page((unsigned long)page_table);
660                 return ERR_PTR(-ENOMEM);
661         }
662
663         dte = rk_mk_dte(pt_dma);
664         *dte_addr = dte;
665
666         rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
667         rk_table_flush(rk_domain,
668                        rk_domain->dt_dma + dte_index * sizeof(u32), 1);
669 done:
670         pt_phys = rk_dte_pt_address(dte);
671         return (u32 *)phys_to_virt(pt_phys);
672 }
673
674 static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
675                                   u32 *pte_addr, dma_addr_t pte_dma,
676                                   size_t size)
677 {
678         unsigned int pte_count;
679         unsigned int pte_total = size / SPAGE_SIZE;
680
681         WARN_ON(!mutex_is_locked(&rk_domain->dt_lock));
682
683         for (pte_count = 0; pte_count < pte_total; pte_count++) {
684                 u32 pte = pte_addr[pte_count];
685                 if (!rk_pte_is_page_valid(pte))
686                         break;
687
688                 pte_addr[pte_count] = rk_mk_pte_invalid(pte);
689         }
690
691         rk_table_flush(rk_domain, pte_dma, pte_count);
692
693         return pte_count * SPAGE_SIZE;
694 }
695
696 static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
697                              dma_addr_t pte_dma, dma_addr_t iova,
698                              phys_addr_t paddr, size_t size, int prot)
699 {
700         unsigned int pte_count;
701         unsigned int pte_total = size / SPAGE_SIZE;
702         phys_addr_t page_phys;
703
704         WARN_ON(!mutex_is_locked(&rk_domain->dt_lock));
705
706         for (pte_count = 0; pte_count < pte_total; pte_count++) {
707                 u32 pte = pte_addr[pte_count];
708
709                 if (rk_pte_is_page_valid(pte))
710                         goto unwind;
711
712                 pte_addr[pte_count] = rk_mk_pte(paddr, prot);
713
714                 paddr += SPAGE_SIZE;
715         }
716
717         rk_table_flush(rk_domain, pte_dma, pte_total);
718
719         /*
720          * Zap the first and last iova to evict from iotlb any previously
721          * mapped cachelines holding stale values for its dte and pte.
722          * We only zap the first and last iova, since only they could have
723          * dte or pte shared with an existing mapping.
724          */
725         rk_iommu_zap_iova_first_last(rk_domain, iova, size);
726
727         return 0;
728 unwind:
729         /* Unmap the range of iovas that we just mapped */
730         rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma,
731                             pte_count * SPAGE_SIZE);
732
733         iova += pte_count * SPAGE_SIZE;
734         page_phys = rk_pte_page_address(pte_addr[pte_count]);
735         pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
736                &iova, &page_phys, &paddr, prot);
737
738         return -EADDRINUSE;
739 }
740
741 static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
742                         phys_addr_t paddr, size_t size, int prot)
743 {
744         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
745         dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
746         u32 *page_table, *pte_addr;
747         u32 dte_index, pte_index;
748         int ret;
749
750         mutex_lock(&rk_domain->dt_lock);
751
752         /*
753          * pgsize_bitmap specifies iova sizes that fit in one page table
754          * (1024 4-KiB pages = 4 MiB).
755          * So, size will always be 4096 <= size <= 4194304.
756          * Since iommu_map() guarantees that both iova and size will be
757          * aligned, we will always only be mapping from a single dte here.
758          */
759         page_table = rk_dte_get_page_table(rk_domain, iova);
760         if (IS_ERR(page_table)) {
761                 mutex_unlock(&rk_domain->dt_lock);
762                 return PTR_ERR(page_table);
763         }
764
765         dte_index = rk_domain->dt[rk_iova_dte_index(iova)];
766         pte_index = rk_iova_pte_index(iova);
767         pte_addr = &page_table[pte_index];
768         pte_dma = rk_dte_pt_address(dte_index) + pte_index * sizeof(u32);
769         ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova,
770                                 paddr, size, prot);
771
772         mutex_unlock(&rk_domain->dt_lock);
773
774         return ret;
775 }
776
777 static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
778                              size_t size)
779 {
780         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
781         dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
782         phys_addr_t pt_phys;
783         u32 dte;
784         u32 *pte_addr;
785         size_t unmap_size;
786
787         mutex_lock(&rk_domain->dt_lock);
788
789         /*
790          * pgsize_bitmap specifies iova sizes that fit in one page table
791          * (1024 4-KiB pages = 4 MiB).
792          * So, size will always be 4096 <= size <= 4194304.
793          * Since iommu_unmap() guarantees that both iova and size will be
794          * aligned, we will always only be unmapping from a single dte here.
795          */
796         dte = rk_domain->dt[rk_iova_dte_index(iova)];
797         /* Just return 0 if iova is unmapped */
798         if (!rk_dte_is_pt_valid(dte)) {
799                 mutex_unlock(&rk_domain->dt_lock);
800                 return 0;
801         }
802
803         pt_phys = rk_dte_pt_address(dte);
804         pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
805         pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
806         unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
807
808         mutex_unlock(&rk_domain->dt_lock);
809
810         /* Shootdown iotlb entries for iova range that was just unmapped */
811         rk_iommu_zap_iova(rk_domain, iova, unmap_size);
812
813         return unmap_size;
814 }
815
816 static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
817 {
818         struct iommu_group *group;
819         struct device *iommu_dev;
820         struct rk_iommu *rk_iommu;
821
822         group = iommu_group_get(dev);
823         if (!group)
824                 return NULL;
825         iommu_dev = iommu_group_get_iommudata(group);
826         if (!iommu_dev) {
827                 dev_info(dev, "Possibly a virtual device\n");
828                 return NULL;
829         }
830
831         rk_iommu = dev_get_drvdata(iommu_dev);
832         iommu_group_put(group);
833
834         return rk_iommu;
835 }
836
837 static int rk_iommu_attach_device(struct iommu_domain *domain,
838                                   struct device *dev)
839 {
840         struct rk_iommu *iommu;
841         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
842         int ret, i;
843
844         /*
845          * Allow 'virtual devices' (e.g., drm) to attach to domain.
846          * Such a device does not belong to an iommu group.
847          */
848         iommu = rk_iommu_from_dev(dev);
849         if (!iommu)
850                 return 0;
851
852         rk_iommu_power_on(iommu);
853
854         ret = rk_iommu_enable_stall(iommu);
855         if (ret)
856                 return ret;
857
858         ret = rk_iommu_force_reset(iommu);
859         if (ret)
860                 return ret;
861
862         iommu->domain = domain;
863
864         ret = devm_request_irq(iommu->dev, iommu->irq, rk_iommu_irq,
865                                IRQF_SHARED, dev_name(dev), iommu);
866         if (ret)
867                 return ret;
868
869         for (i = 0; i < iommu->num_mmu; i++) {
870                 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR,
871                                rk_domain->dt_dma);
872                 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
873                 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
874         }
875
876         ret = rk_iommu_enable_paging(iommu);
877         if (ret)
878                 return ret;
879
880         mutex_lock(&rk_domain->iommus_lock);
881         list_add_tail(&iommu->node, &rk_domain->iommus);
882         mutex_unlock(&rk_domain->iommus_lock);
883
884         dev_dbg(dev, "Attached to iommu domain\n");
885
886         rk_iommu_disable_stall(iommu);
887
888         return 0;
889 }
890
891 static void rk_iommu_detach_device(struct iommu_domain *domain,
892                                    struct device *dev)
893 {
894         struct rk_iommu *iommu;
895         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
896         int i;
897
898         /* Allow 'virtual devices' (eg drm) to detach from domain */
899         iommu = rk_iommu_from_dev(dev);
900         if (!iommu)
901                 return;
902
903         mutex_lock(&rk_domain->iommus_lock);
904         list_del_init(&iommu->node);
905         mutex_unlock(&rk_domain->iommus_lock);
906
907         /* Ignore error while disabling, just keep going */
908         rk_iommu_enable_stall(iommu);
909         rk_iommu_disable_paging(iommu);
910         for (i = 0; i < iommu->num_mmu; i++) {
911                 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
912                 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
913         }
914         rk_iommu_disable_stall(iommu);
915
916         devm_free_irq(iommu->dev, iommu->irq, iommu);
917
918         iommu->domain = NULL;
919
920         rk_iommu_power_off(iommu);
921
922         dev_dbg(dev, "Detached from iommu domain\n");
923 }
924
925 static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
926 {
927         struct rk_iommu_domain *rk_domain;
928         struct platform_device *pdev;
929         struct device *iommu_dev;
930
931         if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
932                 return NULL;
933
934         /* Register a pdev per domain, so DMA API can base on this *dev
935          * even some virtual master doesn't have an iommu slave
936          */
937         pdev = platform_device_register_simple("rk_iommu_domain",
938                                                PLATFORM_DEVID_AUTO, NULL, 0);
939         if (IS_ERR(pdev))
940                 return NULL;
941
942         rk_domain = devm_kzalloc(&pdev->dev, sizeof(*rk_domain), GFP_KERNEL);
943         if (!rk_domain)
944                 goto err_unreg_pdev;
945
946         rk_domain->pdev = pdev;
947
948         if (type == IOMMU_DOMAIN_DMA &&
949             iommu_get_dma_cookie(&rk_domain->domain))
950                 goto err_unreg_pdev;
951
952         /*
953          * rk32xx iommus use a 2 level pagetable.
954          * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
955          * Allocate one 4 KiB page for each table.
956          */
957         rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
958         if (!rk_domain->dt)
959                 goto err_put_cookie;
960
961         iommu_dev = &pdev->dev;
962         rk_domain->dt_dma = dma_map_single(iommu_dev, rk_domain->dt,
963                                            SPAGE_SIZE, DMA_TO_DEVICE);
964         if (dma_mapping_error(iommu_dev, rk_domain->dt_dma)) {
965                 dev_err(iommu_dev, "DMA map error for DT\n");
966                 goto err_free_dt;
967         }
968
969         rk_table_flush(rk_domain, rk_domain->dt_dma, NUM_DT_ENTRIES);
970
971         mutex_init(&rk_domain->iommus_lock);
972         mutex_init(&rk_domain->dt_lock);
973         INIT_LIST_HEAD(&rk_domain->iommus);
974
975         rk_domain->domain.geometry.aperture_start = 0;
976         rk_domain->domain.geometry.aperture_end   = DMA_BIT_MASK(32);
977         rk_domain->domain.geometry.force_aperture = true;
978
979         return &rk_domain->domain;
980
981 err_free_dt:
982         free_page((unsigned long)rk_domain->dt);
983 err_put_cookie:
984         if (type == IOMMU_DOMAIN_DMA)
985                 iommu_put_dma_cookie(&rk_domain->domain);
986 err_unreg_pdev:
987         platform_device_unregister(pdev);
988
989         return NULL;
990 }
991
992 static void rk_iommu_domain_free(struct iommu_domain *domain)
993 {
994         struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
995         int i;
996
997         WARN_ON(!list_empty(&rk_domain->iommus));
998
999         for (i = 0; i < NUM_DT_ENTRIES; i++) {
1000                 u32 dte = rk_domain->dt[i];
1001                 if (rk_dte_is_pt_valid(dte)) {
1002                         phys_addr_t pt_phys = rk_dte_pt_address(dte);
1003                         u32 *page_table = phys_to_virt(pt_phys);
1004                         dma_unmap_single(&rk_domain->pdev->dev, pt_phys,
1005                                          SPAGE_SIZE, DMA_TO_DEVICE);
1006                         free_page((unsigned long)page_table);
1007                 }
1008         }
1009
1010         dma_unmap_single(&rk_domain->pdev->dev, rk_domain->dt_dma,
1011                          SPAGE_SIZE, DMA_TO_DEVICE);
1012         free_page((unsigned long)rk_domain->dt);
1013
1014         if (domain->type == IOMMU_DOMAIN_DMA)
1015                 iommu_put_dma_cookie(&rk_domain->domain);
1016
1017         platform_device_unregister(rk_domain->pdev);
1018 }
1019
1020 static bool rk_iommu_is_dev_iommu_master(struct device *dev)
1021 {
1022         struct device_node *np = dev->of_node;
1023         int ret;
1024
1025         /*
1026          * An iommu master has an iommus property containing a list of phandles
1027          * to iommu nodes, each with an #iommu-cells property with value 0.
1028          */
1029         ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells");
1030         return (ret > 0);
1031 }
1032
1033 static int rk_iommu_group_set_iommudata(struct iommu_group *group,
1034                                         struct device *dev)
1035 {
1036         struct device_node *np = dev->of_node;
1037         struct platform_device *pd;
1038         int ret;
1039         struct of_phandle_args args;
1040
1041         /*
1042          * An iommu master has an iommus property containing a list of phandles
1043          * to iommu nodes, each with an #iommu-cells property with value 0.
1044          */
1045         ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0,
1046                                          &args);
1047         if (ret) {
1048                 dev_err(dev, "of_parse_phandle_with_args(%s) => %d\n",
1049                         np->full_name, ret);
1050                 return ret;
1051         }
1052         if (args.args_count != 0) {
1053                 dev_err(dev, "incorrect number of iommu params found for %s (found %d, expected 0)\n",
1054                         args.np->full_name, args.args_count);
1055                 return -EINVAL;
1056         }
1057
1058         pd = of_find_device_by_node(args.np);
1059         of_node_put(args.np);
1060         if (!pd) {
1061                 dev_err(dev, "iommu %s not found\n", args.np->full_name);
1062                 return -EPROBE_DEFER;
1063         }
1064
1065         /* TODO(djkurtz): handle multiple slave iommus for a single master */
1066         iommu_group_set_iommudata(group, &pd->dev, NULL);
1067
1068         return 0;
1069 }
1070
1071 static int rk_iommu_add_device(struct device *dev)
1072 {
1073         struct iommu_group *group;
1074         int ret;
1075
1076         if (!rk_iommu_is_dev_iommu_master(dev))
1077                 return -ENODEV;
1078
1079         group = iommu_group_get(dev);
1080         if (!group) {
1081                 group = iommu_group_alloc();
1082                 if (IS_ERR(group)) {
1083                         dev_err(dev, "Failed to allocate IOMMU group\n");
1084                         return PTR_ERR(group);
1085                 }
1086         }
1087
1088         ret = iommu_group_add_device(group, dev);
1089         if (ret)
1090                 goto err_put_group;
1091
1092         ret = rk_iommu_group_set_iommudata(group, dev);
1093         if (ret)
1094                 goto err_remove_device;
1095
1096         iommu_group_put(group);
1097
1098         return 0;
1099
1100 err_remove_device:
1101         iommu_group_remove_device(dev);
1102 err_put_group:
1103         iommu_group_put(group);
1104         return ret;
1105 }
1106
1107 static void rk_iommu_remove_device(struct device *dev)
1108 {
1109         if (!rk_iommu_is_dev_iommu_master(dev))
1110                 return;
1111
1112         iommu_group_remove_device(dev);
1113 }
1114
1115 static const struct iommu_ops rk_iommu_ops = {
1116         .domain_alloc = rk_iommu_domain_alloc,
1117         .domain_free = rk_iommu_domain_free,
1118         .attach_dev = rk_iommu_attach_device,
1119         .detach_dev = rk_iommu_detach_device,
1120         .map = rk_iommu_map,
1121         .unmap = rk_iommu_unmap,
1122         .map_sg = default_iommu_map_sg,
1123         .add_device = rk_iommu_add_device,
1124         .remove_device = rk_iommu_remove_device,
1125         .iova_to_phys = rk_iommu_iova_to_phys,
1126         .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1127 };
1128
1129 static int rk_iommu_domain_probe(struct platform_device *pdev)
1130 {
1131         struct device *dev = &pdev->dev;
1132
1133         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), GFP_KERNEL);
1134         if (!dev->dma_parms)
1135                 return -ENOMEM;
1136
1137         /* Set dma_ops for dev, otherwise it would be dummy_dma_ops */
1138         arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), NULL, false);
1139
1140         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
1141         dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1142
1143         return 0;
1144 }
1145
1146 static struct platform_driver rk_iommu_domain_driver = {
1147         .probe = rk_iommu_domain_probe,
1148         .driver = {
1149                    .name = "rk_iommu_domain",
1150         },
1151 };
1152
1153 static int rk_iommu_probe(struct platform_device *pdev)
1154 {
1155         struct device *dev = &pdev->dev;
1156         struct rk_iommu *iommu;
1157         struct resource *res;
1158         int num_res = pdev->num_resources;
1159         int i;
1160
1161         iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
1162         if (!iommu)
1163                 return -ENOMEM;
1164
1165         platform_set_drvdata(pdev, iommu);
1166         iommu->dev = dev;
1167         iommu->num_mmu = 0;
1168
1169         iommu->bases = devm_kzalloc(dev, sizeof(*iommu->bases) * num_res,
1170                                     GFP_KERNEL);
1171         if (!iommu->bases)
1172                 return -ENOMEM;
1173
1174         for (i = 0; i < num_res; i++) {
1175                 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1176                 if (!res)
1177                         continue;
1178                 iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
1179                 if (IS_ERR(iommu->bases[i]))
1180                         continue;
1181                 iommu->num_mmu++;
1182         }
1183         if (iommu->num_mmu == 0)
1184                 return PTR_ERR(iommu->bases[0]);
1185
1186         iommu->irq = platform_get_irq(pdev, 0);
1187         if (iommu->irq < 0) {
1188                 dev_err(dev, "Failed to get IRQ, %d\n", iommu->irq);
1189                 return -ENXIO;
1190         }
1191
1192         iommu->reset_disabled = device_property_read_bool(dev,
1193                                 "rk_iommu,disable_reset_quirk");
1194
1195         iommu->aclk = devm_clk_get(dev, "aclk");
1196         if (IS_ERR(iommu->aclk)) {
1197                 dev_info(dev, "can't get aclk\n");
1198                 iommu->aclk = NULL;
1199         }
1200
1201         iommu->hclk = devm_clk_get(dev, "hclk");
1202         if (IS_ERR(iommu->hclk)) {
1203                 dev_info(dev, "can't get hclk\n");
1204                 iommu->hclk = NULL;
1205         }
1206
1207         if (iommu->aclk && iommu->hclk) {
1208                 clk_prepare(iommu->aclk);
1209                 clk_prepare(iommu->hclk);
1210         }
1211
1212         pm_runtime_enable(dev);
1213
1214         return 0;
1215 }
1216
1217 static int rk_iommu_remove(struct platform_device *pdev)
1218 {
1219         pm_runtime_put(&pdev->dev);
1220
1221         return 0;
1222 }
1223
1224 static const struct of_device_id rk_iommu_dt_ids[] = {
1225         { .compatible = "rockchip,iommu" },
1226         { /* sentinel */ }
1227 };
1228 MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);
1229
1230 static struct platform_driver rk_iommu_driver = {
1231         .probe = rk_iommu_probe,
1232         .remove = rk_iommu_remove,
1233         .driver = {
1234                    .name = "rk_iommu",
1235                    .of_match_table = rk_iommu_dt_ids,
1236         },
1237 };
1238
1239 static int __init rk_iommu_init(void)
1240 {
1241         struct device_node *np;
1242         int ret;
1243
1244         np = of_find_matching_node(NULL, rk_iommu_dt_ids);
1245         if (!np)
1246                 return 0;
1247
1248         of_node_put(np);
1249
1250         ret = bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
1251         if (ret)
1252                 return ret;
1253
1254         ret = platform_driver_register(&rk_iommu_domain_driver);
1255         if (ret)
1256                 return ret;
1257
1258         ret = platform_driver_register(&rk_iommu_driver);
1259         if (ret)
1260                 platform_driver_unregister(&rk_iommu_domain_driver);
1261         return ret;
1262 }
1263 static void __exit rk_iommu_exit(void)
1264 {
1265         platform_driver_unregister(&rk_iommu_driver);
1266         platform_driver_unregister(&rk_iommu_domain_driver);
1267 }
1268
1269 subsys_initcall(rk_iommu_init);
1270 module_exit(rk_iommu_exit);
1271
1272 MODULE_DESCRIPTION("IOMMU API for Rockchip");
1273 MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
1274 MODULE_ALIAS("platform:rockchip-iommu");
1275 MODULE_LICENSE("GPL v2");