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