1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * IOMMU API for Rockchip
4 *
5 * Module Authors:    Simon Xue <xxm@rock-chips.com>
6 *            Daniel Kurtz <djkurtz@chromium.org>
7 */
8
9#include <soc/rockchip/rockchip_iommu.h>
10
11#include <linux/clk.h>
12#include <linux/compiler.h>
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/dma-iommu.h>
16#include <linux/dma-mapping.h>
17#include <linux/errno.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/iommu.h>
21#include <linux/iopoll.h>
22#include <linux/list.h>
23#include <linux/mm.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/of.h>
27#include <linux/of_iommu.h>
28#include <linux/of_platform.h>
29#include <linux/platform_device.h>
30#include <linux/pm_runtime.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33
34/** MMU register offsets */
35#define RK_MMU_DTE_ADDR 0x00 /* Directory table address */
36#define RK_MMU_STATUS 0x04
37#define RK_MMU_COMMAND 0x08
38#define RK_MMU_PAGE_FAULT_ADDR 0x0C /* IOVA of last page fault */
39#define RK_MMU_ZAP_ONE_LINE 0x10    /* Shootdown one IOTLB entry */
40#define RK_MMU_INT_RAWSTAT 0x14     /* IRQ status ignoring mask */
41#define RK_MMU_INT_CLEAR 0x18       /* Acknowledge and re-arm irq */
42#define RK_MMU_INT_MASK 0x1C        /* IRQ enable */
43#define RK_MMU_INT_STATUS 0x20      /* IRQ status after masking */
44#define RK_MMU_AUTO_GATING 0x24
45
46#define DTE_ADDR_DUMMY 0xCAFEBABE
47
48#define RK_MMU_POLL_PERIOD_US 100
49#define RK_MMU_FORCE_RESET_TIMEOUT_US 100000
50#define RK_MMU_POLL_TIMEOUT_US 1000
51
52/* RK_MMU_STATUS fields */
53#define RK_MMU_STATUS_PAGING_ENABLED BIT(0)
54#define RK_MMU_STATUS_PAGE_FAULT_ACTIVE BIT(1)
55#define RK_MMU_STATUS_STALL_ACTIVE BIT(2)
56#define RK_MMU_STATUS_IDLE BIT(3)
57#define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY BIT(4)
58#define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE BIT(5)
59#define RK_MMU_STATUS_STALL_NOT_ACTIVE BIT(31)
60
61/* RK_MMU_COMMAND command values */
62#define RK_MMU_CMD_ENABLE_PAGING 0   /* Enable memory translation */
63#define RK_MMU_CMD_DISABLE_PAGING 1  /* Disable memory translation */
64#define RK_MMU_CMD_ENABLE_STALL 2    /* Stall paging to allow other cmds */
65#define RK_MMU_CMD_DISABLE_STALL 3   /* Stop stall re-enables paging */
66#define RK_MMU_CMD_ZAP_CACHE 4       /* Shoot down entire IOTLB */
67#define RK_MMU_CMD_PAGE_FAULT_DONE 5 /* Clear page fault */
68#define RK_MMU_CMD_FORCE_RESET 6     /* Reset all registers */
69
70/* RK_MMU_INT_* register fields */
71#define RK_MMU_IRQ_PAGE_FAULT 0x01 /* page fault */
72#define RK_MMU_IRQ_BUS_ERROR 0x02  /* bus read error */
73#define RK_MMU_IRQ_MASK (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR)
74
75#define NUM_DT_ENTRIES 1024
76#define NUM_PT_ENTRIES 1024
77
78#define SPAGE_ORDER 12
79#define SPAGE_SIZE (1 << SPAGE_ORDER)
80
81#define DISABLE_FETCH_DTE_TIME_LIMIT BIT(31)
82
83#define CMD_RETRY_COUNT 10
84
85/*
86 * Support mapping any size that fits in one page table:
87 *   4 KiB to 4 MiB
88 */
89#define RK_IOMMU_PGSIZE_BITMAP 0x007ff000
90
91#define DT_LO_MASK 0xfffff000
92#define DT_HI_MASK GENMASK_ULL(39, 32)
93#define DT_SHIFT 28
94
95#define DTE_BASE_HI_MASK GENMASK(11, 4)
96
97#define PAGE_DESC_LO_MASK 0xfffff000
98#define PAGE_DESC_HI1_LOWER 32
99#define PAGE_DESC_HI1_UPPER 35
100#define PAGE_DESC_HI2_LOWER 36
101#define PAGE_DESC_HI2_UPPER 39
102#define PAGE_DESC_HI_MASK1 GENMASK_ULL(PAGE_DESC_HI1_UPPER, PAGE_DESC_HI1_LOWER)
103#define PAGE_DESC_HI_MASK2 GENMASK_ULL(PAGE_DESC_HI2_UPPER, PAGE_DESC_HI2_LOWER)
104
105#define DTE_HI1_LOWER 8
106#define DTE_HI1_UPPER 11
107#define DTE_HI2_LOWER 4
108#define DTE_HI2_UPPER 7
109#define DTE_HI_MASK1 GENMASK(DTE_HI1_UPPER, DTE_HI1_LOWER)
110#define DTE_HI_MASK2 GENMASK(DTE_HI2_UPPER, DTE_HI2_LOWER)
111
112#define PAGE_DESC_HI_SHIFT1 (PAGE_DESC_HI1_LOWER - DTE_HI1_LOWER)
113#define PAGE_DESC_HI_SHIFT2 (PAGE_DESC_HI2_LOWER - DTE_HI2_LOWER)
114
115#define RK_IOMMU_VERSION_CMP 0x02
116#define RK_ADDR_PHYS_MUL 4
117#define RK_DMA_BIT_MASK 32
118
119struct rk_iommu_domain {
120    struct list_head iommus;
121    u32 *dt; /* page directory table */
122    dma_addr_t dt_dma;
123    spinlock_t iommus_lock; /* lock for iommus list */
124    spinlock_t dt_lock;     /* lock for modifying page directory table */
125    bool shootdown_entire;
126
127    struct iommu_domain domain;
128};
129
130struct rockchip_iommu_data {
131    u32 version;
132};
133
134struct rk_iommu {
135    struct device *dev;
136    void __iomem **bases;
137    int num_mmu;
138    int num_irq;
139    struct clk_bulk_data *clocks;
140    int num_clocks;
141    bool reset_disabled;
142    bool skip_read;   /* rk3126/rk3128 can't read vop iommu registers */
143    bool dlr_disable; /* avoid access iommu when runtime ops called */
144    bool cmd_retry;
145    struct iommu_device iommu;
146    struct list_head node;       /* entry in rk_iommu_domain.iommus */
147    struct iommu_domain *domain; /* domain to which iommu is attached */
148    struct iommu_group *group;
149    u32 version;
150    bool shootdown_entire;
151};
152
153struct rk_iommudata {
154    struct device_link *link; /* runtime PM link from IOMMU to master */
155    struct rk_iommu *iommu;
156    bool defer_attach;
157};
158
159static struct device *dma_dev;
160
161static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma, unsigned int count)
162{
163    size_t size = count * sizeof(u32); /* count of u32 entry */
164
165    dma_sync_single_for_device(dma_dev, dma, size, DMA_TO_DEVICE);
166}
167
168static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
169{
170    return container_of(dom, struct rk_iommu_domain, domain);
171}
172
173/*
174 * The Rockchip rk3288 iommu uses a 2-level page table.
175 * The first level is the "Directory Table" (DT).
176 * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing
177 * to a "Page Table".
178 * The second level is the 1024 Page Tables (PT).
179 * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to
180 * a 4 KB page of physical memory.
181 *
182 * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries).
183 * Each iommu device has a MMU_DTE_ADDR register that contains the physical
184 * address of the start of the DT page.
185 *
186 * The structure of the page table is as follows:
187 *
188 *                   DT
189 * MMU_DTE_ADDR -> +-----+
190 *                 |     |
191 *                 +-----+     PT
192 *                 | DTE | -> +-----+
193 *                 +-----+    |     |     Memory
194 *                 |     |    +-----+     Page
195 *                 |     |    | PTE | -> +-----+
196 *                 +-----+    +-----+    |     |
197 *                            |     |    |     |
198 *                            |     |    |     |
199 *                            +-----+    |     |
200 *                                       |     |
201 *                                       |     |
202 *                                       +-----+
203 */
204
205/*
206 * Each DTE has a PT address and a valid bit:
207 * +---------------------+-----------+-+
208 * | PT address          | Reserved  |V|
209 * +---------------------+-----------+-+
210 *  31:12 - PT address (PTs always starts on a 4 KB boundary)
211 *  11: 1 - Reserved
212 *      0 - 1 if PT @ PT address is valid
213 */
214#define RK_DTE_PT_ADDRESS_MASK 0xfffff000
215#define RK_DTE_PT_VALID BIT(0)
216
217/*
218 * In v2:
219 * 31:12 - PT address bit 31:0
220 * 11: 8 - PT address bit 35:32
221 *  7: 4 - PT address bit 39:36
222 *  3: 1 - Reserved
223 *     0 - 1 if PT @ PT address is valid
224 */
225#define RK_DTE_PT_ADDRESS_MASK_V2 0xfffffff0
226
227static inline phys_addr_t rk_dte_pt_address(u32 dte)
228{
229    return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK;
230}
231
232static inline phys_addr_t rk_dte_pt_address_v2(u32 dte)
233{
234    u64 dte_v2 = dte;
235
236    dte_v2 = ((dte_v2 & DTE_HI_MASK2) << PAGE_DESC_HI_SHIFT2) | ((dte_v2 & DTE_HI_MASK1) << PAGE_DESC_HI_SHIFT1) |
237             (dte_v2 & PAGE_DESC_LO_MASK);
238
239    return (phys_addr_t)dte_v2;
240}
241
242static inline bool rk_dte_is_pt_valid(u32 dte)
243{
244    return dte & RK_DTE_PT_VALID;
245}
246
247static inline u32 rk_mk_dte(dma_addr_t pt_dma)
248{
249    return (pt_dma & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID;
250}
251
252static inline u32 rk_mk_dte_v2(dma_addr_t pt_dma)
253{
254    pt_dma = (pt_dma & PAGE_DESC_LO_MASK) | ((pt_dma & PAGE_DESC_HI_MASK1) >> PAGE_DESC_HI_SHIFT1) |
255             ((pt_dma & PAGE_DESC_HI_MASK2) >> PAGE_DESC_HI_SHIFT2);
256
257    return (pt_dma & RK_DTE_PT_ADDRESS_MASK_V2) | RK_DTE_PT_VALID;
258}
259
260/*
261 * Each PTE has a Page address, some flags and a valid bit:
262 * +---------------------+---+-------+-+
263 * | Page address        |Rsv| Flags |V|
264 * +---------------------+---+-------+-+
265 *  31:12 - Page address (Pages always start on a 4 KB boundary)
266 *  11: 9 - Reserved
267 *   8: 1 - Flags
268 *      8 - Read allocate - allocate cache space on read misses
269 *      7 - Read cache - enable cache & prefetch of data
270 *      6 - Write buffer - enable delaying writes on their way to memory
271 *      5 - Write allocate - allocate cache space on write misses
272 *      4 - Write cache - different writes can be merged together
273 *      3 - Override cache attributes
274 *          if 1, bits 4-8 control cache attributes
275 *          if 0, the system bus defaults are used
276 *      2 - Writable
277 *      1 - Readable
278 *      0 - 1 if Page @ Page address is valid
279 */
280#define RK_PTE_PAGE_ADDRESS_MASK 0xfffff000
281#define RK_PTE_PAGE_FLAGS_MASK 0x000001fe
282#define RK_PTE_PAGE_WRITABLE BIT(2)
283#define RK_PTE_PAGE_READABLE BIT(1)
284#define RK_PTE_PAGE_VALID BIT(0)
285
286/*
287 * In v2:
288 * 31:12 - Page address bit 31:0
289 *  11:9 - Page address bit 34:32
290 *   8:4 - Page address bit 39:35
291 *     3 - Security
292 *     2 - Writable
293 *     1 - Readable
294 *     0 - 1 if Page @ Page address is valid
295 */
296#define RK_PTE_PAGE_ADDRESS_MASK_V2 0xfffffff0
297#define RK_PTE_PAGE_FLAGS_MASK_V2 0x0000000e
298#define RK_PTE_PAGE_READABLE_V2 BIT(1)
299#define RK_PTE_PAGE_WRITABLE_V2 BIT(2)
300
301static inline phys_addr_t rk_pte_page_address(u32 pte)
302{
303    return (phys_addr_t)pte & RK_PTE_PAGE_ADDRESS_MASK;
304}
305
306static inline phys_addr_t rk_pte_page_address_v2(u32 pte)
307{
308    u64 pte_v2 = pte;
309
310    pte_v2 = ((pte_v2 & DTE_HI_MASK2) << PAGE_DESC_HI_SHIFT2) | ((pte_v2 & DTE_HI_MASK1) << PAGE_DESC_HI_SHIFT1) |
311             (pte_v2 & PAGE_DESC_LO_MASK);
312
313    return (phys_addr_t)pte_v2;
314}
315
316static inline bool rk_pte_is_page_valid(u32 pte)
317{
318    return pte & RK_PTE_PAGE_VALID;
319}
320
321/* set cache flags per prot IOMMU_CACHE */
322static u32 rk_mk_pte(phys_addr_t page, int prot)
323{
324    u32 flags = 0;
325    flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0;
326    flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0;
327    page &= RK_PTE_PAGE_ADDRESS_MASK;
328    return page | flags | RK_PTE_PAGE_VALID;
329}
330
331static u32 rk_mk_pte_v2(phys_addr_t page, int prot)
332{
333    u32 flags = 0;
334
335    flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE_V2 : 0;
336    flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE_V2 : 0;
337    page = (page & PAGE_DESC_LO_MASK) | ((page & PAGE_DESC_HI_MASK1) >> PAGE_DESC_HI_SHIFT1) |
338           ((page & PAGE_DESC_HI_MASK2) >> PAGE_DESC_HI_SHIFT2);
339    page &= RK_PTE_PAGE_ADDRESS_MASK_V2;
340
341    return page | flags | RK_PTE_PAGE_VALID;
342}
343
344static u32 rk_mk_pte_invalid(u32 pte)
345{
346    return pte & ~RK_PTE_PAGE_VALID;
347}
348
349/*
350 * rk3288 iova (IOMMU Virtual Address) format
351 *  31       22.21       12.11          0
352 * +-----------+-----------+-------------+
353 * | DTE index | PTE index | Page offset |
354 * +-----------+-----------+-------------+
355 *  31:22 - DTE index   - index of DTE in DT
356 *  21:12 - PTE index   - index of PTE in PT @ DTE.pt_address
357 *  11: 0 - Page offset - offset into page @ PTE.page_address
358 */
359#define RK_IOVA_DTE_MASK 0xffc00000
360#define RK_IOVA_DTE_SHIFT 22
361#define RK_IOVA_PTE_MASK 0x003ff000
362#define RK_IOVA_PTE_SHIFT 12
363#define RK_IOVA_PAGE_MASK 0x00000fff
364#define RK_IOVA_PAGE_SHIFT 0
365
366static u32 rk_iova_dte_index(dma_addr_t iova)
367{
368    return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT;
369}
370
371static u32 rk_iova_pte_index(dma_addr_t iova)
372{
373    return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT;
374}
375
376static u32 rk_iova_page_offset(dma_addr_t iova)
377{
378    return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT;
379}
380
381static u32 rk_iommu_read(void __iomem *base, u32 offset)
382{
383    return readl(base + offset);
384}
385
386static void rk_iommu_write(void __iomem *base, u32 offset, u32 value)
387{
388    writel(value, base + offset);
389}
390
391static void rk_iommu_command(struct rk_iommu *iommu, u32 command)
392{
393    int i;
394
395    for (i = 0; i < iommu->num_mmu; i++) {
396        writel(command, iommu->bases[i] + RK_MMU_COMMAND);
397    }
398}
399
400static void rk_iommu_base_command(void __iomem *base, u32 command)
401{
402    writel(command, base + RK_MMU_COMMAND);
403}
404static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start, size_t size)
405{
406    int i;
407    dma_addr_t iova_end = iova_start + size;
408    /*
409     * (djkurtz): Figure out when it is more efficient to shootdown the
410     * entire iotlb rather than iterate over individual iovas.
411     */
412    for (i = 0; i < iommu->num_mmu; i++) {
413        dma_addr_t iova;
414
415        for (iova = iova_start; iova < iova_end; iova += SPAGE_SIZE) {
416            rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova);
417        }
418    }
419}
420
421static bool rk_iommu_is_stall_active(struct rk_iommu *iommu)
422{
423    bool active = true;
424    int i;
425
426    for (i = 0; i < iommu->num_mmu; i++) {
427        active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & RK_MMU_STATUS_STALL_ACTIVE);
428    }
429
430    return active;
431}
432
433static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu)
434{
435    bool enable = true;
436    int i;
437
438    for (i = 0; i < iommu->num_mmu; i++) {
439        enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & RK_MMU_STATUS_PAGING_ENABLED);
440    }
441
442    return enable;
443}
444
445static bool rk_iommu_is_reset_done(struct rk_iommu *iommu)
446{
447    bool done = true;
448    int i;
449
450    for (i = 0; i < iommu->num_mmu; i++) {
451        done &= rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0;
452    }
453
454    return done;
455}
456
457static int rk_iommu_enable_stall(struct rk_iommu *iommu)
458{
459    int ret, i;
460    bool val;
461    int retry_count = 0;
462
463    if (iommu->skip_read) {
464        goto read_wa;
465    }
466
467    if (rk_iommu_is_stall_active(iommu)) {
468        return 0;
469    }
470
471    /* Stall can only be enabled if paging is enabled */
472    if (!rk_iommu_is_paging_enabled(iommu)) {
473        return 0;
474    }
475
476read_wa:
477    while (1) {
478        rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL);
479        if (iommu->skip_read) {
480            return 0;
481        }
482
483        ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, val, RK_MMU_POLL_PERIOD_US,
484                                 RK_MMU_POLL_TIMEOUT_US);
485        if (ret) {
486            for (i = 0; i < iommu->num_mmu; i++) {
487                dev_err(iommu->dev, "Enable stall request timed out, retry_count = %d, status: %#08x\n", retry_count,
488                        rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
489            }
490            if (iommu->cmd_retry && (retry_count++ < CMD_RETRY_COUNT)) {
491                continue;
492            }
493        }
494        break;
495    }
496    return ret;
497}
498
499static int rk_iommu_disable_stall(struct rk_iommu *iommu)
500{
501    int ret, i;
502    bool val;
503    int retry_count = 0;
504
505    if (iommu->skip_read) {
506        goto read_wa;
507    }
508
509    if (!rk_iommu_is_stall_active(iommu)) {
510        return 0;
511    }
512
513read_wa:
514    while (1) {
515        rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL);
516        if (iommu->skip_read) {
517            return 0;
518        }
519
520        ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, !val, RK_MMU_POLL_PERIOD_US,
521                                 RK_MMU_POLL_TIMEOUT_US);
522        if (ret) {
523            for (i = 0; i < iommu->num_mmu; i++) {
524                dev_err(iommu->dev, "Disable stall request timed out, retry_count = %d, status: %#08x\n", retry_count,
525                        rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
526            }
527            if (iommu->cmd_retry && (retry_count++ < CMD_RETRY_COUNT)) {
528                continue;
529            }
530        }
531        break;
532    }
533    return ret;
534}
535
536static int rk_iommu_enable_paging(struct rk_iommu *iommu)
537{
538    int ret, i;
539    bool val;
540    int retry_count = 0;
541
542    if (iommu->skip_read) {
543        goto read_wa;
544    }
545
546    if (rk_iommu_is_paging_enabled(iommu)) {
547        return 0;
548    }
549
550read_wa:
551    while (1) {
552        rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING);
553        if (iommu->skip_read) {
554            return 0;
555        }
556
557        ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, val, RK_MMU_POLL_PERIOD_US,
558                                 RK_MMU_POLL_TIMEOUT_US);
559        if (ret) {
560            for (i = 0; i < iommu->num_mmu; i++) {
561                dev_err(iommu->dev, "Enable paging request timed out, retry_count = %d, status: %#08x\n", retry_count,
562                        rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
563            }
564            if (iommu->cmd_retry && (retry_count++ < CMD_RETRY_COUNT)) {
565                continue;
566            }
567        }
568        break;
569    }
570    return ret;
571}
572
573static int rk_iommu_disable_paging(struct rk_iommu *iommu)
574{
575    int ret, i;
576    bool val;
577    int retry_count = 0;
578
579    if (iommu->skip_read) {
580        goto read_wa;
581    }
582
583    if (!rk_iommu_is_paging_enabled(iommu)) {
584        return 0;
585    }
586
587read_wa:
588    while (1) {
589        rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING);
590        if (iommu->skip_read) {
591            return 0;
592        }
593
594        ret =
595            readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, !val, RK_MMU_POLL_PERIOD_US,
596                RK_MMU_POLL_TIMEOUT_US);
597        if (ret) {
598            for (i = 0; i < iommu->num_mmu; i++) {
599                dev_err(iommu->dev, "Disable paging request timed out, retry_count = %d, status: %#08x\n", retry_count,
600                        rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
601            }
602            if (iommu->cmd_retry && (retry_count++ < CMD_RETRY_COUNT)) {
603                continue;
604            }
605        }
606        break;
607    }
608    return ret;
609}
610
611static int rk_iommu_force_reset(struct rk_iommu *iommu)
612{
613    int ret, i;
614    u32 dte_addr;
615    bool val;
616    u32 address_mask;
617
618    if (iommu->reset_disabled) {
619        return 0;
620    }
621
622    if (iommu->skip_read) {
623        goto read_wa;
624    }
625
626    /*
627     * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY
628     * and verifying that upper 5 nybbles are read back.
629     */
630
631    /*
632     * In v2: upper 7 nybbles are read back.
633     */
634    for (i = 0; i < iommu->num_mmu; i++) {
635        rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, DTE_ADDR_DUMMY);
636
637        if (iommu->version >= 0x2) {
638            address_mask = RK_DTE_PT_ADDRESS_MASK_V2;
639        } else {
640            address_mask = RK_DTE_PT_ADDRESS_MASK;
641        }
642        dte_addr = rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR);
643        if (dte_addr != (DTE_ADDR_DUMMY & address_mask)) {
644            dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n");
645            return -EFAULT;
646        }
647    }
648
649read_wa:
650    rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET);
651    if (iommu->skip_read) {
652        return 0;
653    }
654
655    ret = readx_poll_timeout(rk_iommu_is_reset_done, iommu, val, val, RK_MMU_FORCE_RESET_TIMEOUT_US,
656                             RK_MMU_POLL_TIMEOUT_US);
657    if (ret) {
658        dev_err(iommu->dev, "FORCE_RESET command timed out\n");
659        return ret;
660    }
661
662    return 0;
663}
664
665static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova)
666{
667    void __iomem *base = iommu->bases[index];
668    u32 dte_index, pte_index, page_offset;
669    u32 mmu_dte_addr;
670    phys_addr_t mmu_dte_addr_phys, dte_addr_phys;
671    u32 *dte_addr;
672    u32 dte;
673    phys_addr_t pte_addr_phys = 0;
674    u32 *pte_addr = NULL;
675    u32 pte = 0;
676    phys_addr_t page_addr_phys = 0;
677    u32 page_flags = 0;
678
679    dte_index = rk_iova_dte_index(iova);
680    pte_index = rk_iova_pte_index(iova);
681    page_offset = rk_iova_page_offset(iova);
682
683    mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR);
684    mmu_dte_addr_phys = (phys_addr_t)mmu_dte_addr;
685    if (iommu->version >= RK_IOMMU_VERSION_CMP) {
686        mmu_dte_addr_phys = (mmu_dte_addr_phys & DT_LO_MASK) | ((mmu_dte_addr_phys & DTE_BASE_HI_MASK) << DT_SHIFT);
687    }
688
689    dte_addr_phys = mmu_dte_addr_phys + (RK_ADDR_PHYS_MUL * dte_index);
690    dte_addr = phys_to_virt(dte_addr_phys);
691    dte = *dte_addr;
692
693    if (!rk_dte_is_pt_valid(dte)) {
694        goto print_it;
695    }
696
697    if (iommu->version >= RK_IOMMU_VERSION_CMP) {
698        pte_addr_phys = rk_dte_pt_address_v2(dte) + (pte_index * RK_ADDR_PHYS_MUL);
699    } else {
700        pte_addr_phys = rk_dte_pt_address(dte) + (pte_index * RK_ADDR_PHYS_MUL);
701    }
702    pte_addr = phys_to_virt(pte_addr_phys);
703    pte = *pte_addr;
704
705    if (!rk_pte_is_page_valid(pte)) {
706        goto print_it;
707    }
708
709    if (iommu->version >= RK_IOMMU_VERSION_CMP) {
710        page_addr_phys = rk_pte_page_address_v2(pte) + page_offset;
711    } else {
712        page_addr_phys = rk_pte_page_address(pte) + page_offset;
713    }
714    page_flags = pte & RK_PTE_PAGE_FLAGS_MASK;
715
716print_it:
717    dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n", &iova, dte_index,
718            pte_index, page_offset);
719    dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n",
720            &mmu_dte_addr_phys, &dte_addr_phys, dte, rk_dte_is_pt_valid(dte), &pte_addr_phys, pte,
721            rk_pte_is_page_valid(pte), &page_addr_phys, page_flags);
722}
723
724static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
725{
726    struct rk_iommu *iommu = dev_id;
727    u32 status;
728    u32 int_status;
729    u32 int_mask;
730    dma_addr_t iova;
731    irqreturn_t ret = IRQ_NONE;
732    int i, err;
733
734    err = pm_runtime_get_if_in_use(iommu->dev);
735    if (!err || WARN_ON_ONCE(err < 0)) {
736        return ret;
737    }
738
739    if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks))) {
740        goto out;
741    }
742
743    for (i = 0; i < iommu->num_mmu; i++) {
744        int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
745        if (int_status == 0) {
746            continue;
747        }
748
749        ret = IRQ_HANDLED;
750        iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
751
752        if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
753            int flags;
754
755            status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
756            flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
757
758            dev_err(iommu->dev, "Page fault at %pad of type %s\n", &iova,
759                    (flags == IOMMU_FAULT_WRITE) ? "write" : "read");
760
761            log_iova(iommu, i, iova);
762
763            /*
764             * Report page fault to any installed handlers.
765             * Ignore the return code, though, since we always zap cache
766             * and clear the page fault anyway.
767             */
768            if (iommu->domain) {
769                report_iommu_fault(iommu->domain, iommu->dev, iova, status);
770            } else {
771                dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
772            }
773
774            rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
775
776            /*
777             * Master may clear the int_mask to prevent iommu
778             * re-enter interrupt when mapping. So we postpone
779             * sending PAGE_FAULT_DONE command to mapping finished.
780             */
781            int_mask = rk_iommu_read(iommu->bases[i], RK_MMU_INT_MASK);
782            if (int_mask != 0x0) {
783                rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
784            }
785        }
786
787        if (int_status & RK_MMU_IRQ_BUS_ERROR) {
788            dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
789        }
790
791        if (int_status & ~RK_MMU_IRQ_MASK) {
792            dev_err(iommu->dev, "unexpected int_status: %#08x\n", int_status);
793        }
794
795        rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
796    }
797
798    clk_bulk_disable(iommu->num_clocks, iommu->clocks);
799
800out:
801    pm_runtime_put(iommu->dev);
802    return ret;
803}
804
805static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
806{
807    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
808    unsigned long flags;
809    phys_addr_t pt_phys, phys = 0;
810    u32 dte, pte;
811    u32 *page_table;
812
813    spin_lock_irqsave(&rk_domain->dt_lock, flags);
814
815    dte = rk_domain->dt[rk_iova_dte_index(iova)];
816    if (!rk_dte_is_pt_valid(dte)) {
817        goto out;
818    }
819
820    pt_phys = rk_dte_pt_address(dte);
821    page_table = (u32 *)phys_to_virt(pt_phys);
822    pte = page_table[rk_iova_pte_index(iova)];
823    if (!rk_pte_is_page_valid(pte)) {
824        goto out;
825    }
826
827    phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
828out:
829    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
830
831    return phys;
832}
833
834static phys_addr_t rk_iommu_iova_to_phys_v2(struct iommu_domain *domain, dma_addr_t iova)
835{
836    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
837    unsigned long flags;
838    phys_addr_t pt_phys, phys = 0;
839    u32 dte, pte;
840    u32 *page_table;
841
842    spin_lock_irqsave(&rk_domain->dt_lock, flags);
843
844    dte = rk_domain->dt[rk_iova_dte_index(iova)];
845    if (!rk_dte_is_pt_valid(dte)) {
846        goto out;
847    }
848
849    pt_phys = rk_dte_pt_address_v2(dte);
850    page_table = (u32 *)phys_to_virt(pt_phys);
851    pte = page_table[rk_iova_pte_index(iova)];
852    if (!rk_pte_is_page_valid(pte)) {
853        goto out;
854    }
855
856    phys = rk_pte_page_address_v2(pte) + rk_iova_page_offset(iova);
857out:
858    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
859
860    return phys;
861}
862
863static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain, dma_addr_t iova, size_t size)
864{
865    struct list_head *pos;
866    unsigned long flags;
867
868    /* shootdown these iova from all iommus using this domain */
869    spin_lock_irqsave(&rk_domain->iommus_lock, flags);
870    list_for_each(pos, &rk_domain->iommus)
871    {
872        struct rk_iommu *iommu;
873        int ret;
874
875        iommu = list_entry(pos, struct rk_iommu, node);
876
877        /* Only zap TLBs of IOMMUs that are powered on. */
878        ret = pm_runtime_get_if_in_use(iommu->dev);
879        if (WARN_ON_ONCE(ret < 0)) {
880            continue;
881        }
882        if (ret) {
883            WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
884            rk_iommu_zap_lines(iommu, iova, size);
885            clk_bulk_disable(iommu->num_clocks, iommu->clocks);
886            pm_runtime_put(iommu->dev);
887        }
888    }
889    spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
890}
891
892static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain, dma_addr_t iova, size_t size)
893{
894    rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
895    if (size > SPAGE_SIZE) {
896        rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE, SPAGE_SIZE);
897    }
898}
899
900static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain, dma_addr_t iova)
901{
902    u32 *page_table, *dte_addr;
903    u32 dte_index, dte;
904    phys_addr_t pt_phys;
905    dma_addr_t pt_dma;
906
907    assert_spin_locked(&rk_domain->dt_lock);
908
909    dte_index = rk_iova_dte_index(iova);
910    dte_addr = &rk_domain->dt[dte_index];
911    dte = *dte_addr;
912    if (rk_dte_is_pt_valid(dte)) {
913        goto done;
914    }
915
916    page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
917    if (!page_table) {
918        return ERR_PTR(-ENOMEM);
919    }
920
921    pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
922    if (dma_mapping_error(dma_dev, pt_dma)) {
923        dev_err(dma_dev, "DMA mapping error while allocating page table\n");
924        free_page((unsigned long)page_table);
925        return ERR_PTR(-ENOMEM);
926    }
927
928    dte = rk_mk_dte(pt_dma);
929    *dte_addr = dte;
930
931    rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
932    rk_table_flush(rk_domain, rk_domain->dt_dma + dte_index * sizeof(u32), 1);
933done:
934    pt_phys = rk_dte_pt_address(dte);
935    return (u32 *)phys_to_virt(pt_phys);
936}
937
938static u32 *rk_dte_get_page_table_v2(struct rk_iommu_domain *rk_domain, dma_addr_t iova)
939{
940    u32 *page_table, *dte_addr;
941    u32 dte_index, dte;
942    phys_addr_t pt_phys;
943    dma_addr_t pt_dma;
944
945    assert_spin_locked(&rk_domain->dt_lock);
946
947    dte_index = rk_iova_dte_index(iova);
948    dte_addr = &rk_domain->dt[dte_index];
949    dte = *dte_addr;
950    if (rk_dte_is_pt_valid(dte)) {
951        goto done;
952    }
953
954    page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
955    if (!page_table) {
956        return ERR_PTR(-ENOMEM);
957    }
958
959    pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
960    if (dma_mapping_error(dma_dev, pt_dma)) {
961        dev_err(dma_dev, "DMA mapping error while allocating page table\n");
962        free_page((unsigned long)page_table);
963        return ERR_PTR(-ENOMEM);
964    }
965
966    dte = rk_mk_dte_v2(pt_dma);
967    *dte_addr = dte;
968
969    rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
970    rk_table_flush(rk_domain, rk_domain->dt_dma + dte_index * sizeof(u32), 1);
971done:
972    pt_phys = rk_dte_pt_address_v2(dte);
973    return (u32 *)phys_to_virt(pt_phys);
974}
975
976static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr, dma_addr_t pte_dma, size_t size)
977{
978    unsigned int pte_count;
979    unsigned int pte_total = size / SPAGE_SIZE;
980
981    assert_spin_locked(&rk_domain->dt_lock);
982
983    for (pte_count = 0; pte_count < pte_total; pte_count++) {
984        u32 pte = pte_addr[pte_count];
985        if (!rk_pte_is_page_valid(pte)) {
986            break;
987        }
988
989        pte_addr[pte_count] = rk_mk_pte_invalid(pte);
990    }
991
992    rk_table_flush(rk_domain, pte_dma, pte_count);
993
994    return pte_count * SPAGE_SIZE;
995}
996
997static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr, dma_addr_t pte_dma, dma_addr_t iova,
998                             phys_addr_t paddr, size_t size, int prot)
999{
1000    unsigned int pte_count;
1001    unsigned int pte_total = size / SPAGE_SIZE;
1002    phys_addr_t page_phys;
1003
1004    assert_spin_locked(&rk_domain->dt_lock);
1005
1006    for (pte_count = 0; pte_count < pte_total; pte_count++) {
1007        u32 pte = pte_addr[pte_count];
1008
1009        if (rk_pte_is_page_valid(pte)) {
1010            goto unwind;
1011        }
1012
1013        pte_addr[pte_count] = rk_mk_pte(paddr, prot);
1014
1015        paddr += SPAGE_SIZE;
1016    }
1017
1018    rk_table_flush(rk_domain, pte_dma, pte_total);
1019
1020    /*
1021     * Zap the first and last iova to evict from iotlb any previously
1022     * mapped cachelines holding stale values for its dte and pte.
1023     * We only zap the first and last iova, since only they could have
1024     * dte or pte shared with an existing mapping.
1025     */
1026    /* Do not zap tlb cache line if shootdown_entire set */
1027    if (!rk_domain->shootdown_entire) {
1028        rk_iommu_zap_iova_first_last(rk_domain, iova, size);
1029    }
1030
1031    return 0;
1032unwind:
1033    /* Unmap the range of iovas that we just mapped */
1034    rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, pte_count * SPAGE_SIZE);
1035
1036    iova += pte_count * SPAGE_SIZE;
1037    page_phys = rk_pte_page_address(pte_addr[pte_count]);
1038    pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n", &iova, &page_phys, &paddr, prot);
1039
1040    return -EADDRINUSE;
1041}
1042
1043static int rk_iommu_map_iova_v2(struct rk_iommu_domain *rk_domain, u32 *pte_addr, dma_addr_t pte_dma, dma_addr_t iova,
1044                                phys_addr_t paddr, size_t size, int prot)
1045{
1046    unsigned int pte_count;
1047    unsigned int pte_total = size / SPAGE_SIZE;
1048    phys_addr_t page_phys;
1049
1050    assert_spin_locked(&rk_domain->dt_lock);
1051
1052    for (pte_count = 0; pte_count < pte_total; pte_count++) {
1053        u32 pte = pte_addr[pte_count];
1054
1055        if (rk_pte_is_page_valid(pte)) {
1056            goto unwind;
1057        }
1058
1059        pte_addr[pte_count] = rk_mk_pte_v2(paddr, prot);
1060
1061        paddr += SPAGE_SIZE;
1062    }
1063
1064    rk_table_flush(rk_domain, pte_dma, pte_total);
1065
1066    /*
1067     * Zap the first and last iova to evict from iotlb any previously
1068     * mapped cachelines holding stale values for its dte and pte.
1069     * We only zap the first and last iova, since only they could have
1070     * dte or pte shared with an existing mapping.
1071     */
1072    /* Do not zap tlb cache line if shootdown_entire set */
1073    if (!rk_domain->shootdown_entire) {
1074        rk_iommu_zap_iova_first_last(rk_domain, iova, size);
1075    }
1076
1077    return 0;
1078unwind:
1079    /* Unmap the range of iovas that we just mapped */
1080    rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, pte_count * SPAGE_SIZE);
1081
1082    iova += pte_count * SPAGE_SIZE;
1083    page_phys = rk_pte_page_address_v2(pte_addr[pte_count]);
1084    pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n", &iova, &page_phys, &paddr, prot);
1085
1086    return -EADDRINUSE;
1087}
1088
1089static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova, phys_addr_t paddr, size_t size, int prot,
1090                        gfp_t gfp)
1091{
1092    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1093    unsigned long flags;
1094    dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
1095    u32 *page_table, *pte_addr;
1096    u32 dte, pte_index;
1097    int ret;
1098
1099    spin_lock_irqsave(&rk_domain->dt_lock, flags);
1100
1101    /*
1102     * pgsize_bitmap specifies iova sizes that fit in one page table
1103     * (1024 4-KiB pages = 4 MiB).
1104     * So, size will always be 4096 <= size <= 4194304.
1105     * Since iommu_map() guarantees that both iova and size will be
1106     * aligned, we will always only be mapping from a single dte here.
1107     */
1108    page_table = rk_dte_get_page_table(rk_domain, iova);
1109    if (IS_ERR(page_table)) {
1110        spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1111        return PTR_ERR(page_table);
1112    }
1113
1114    dte = rk_domain->dt[rk_iova_dte_index(iova)];
1115    pte_index = rk_iova_pte_index(iova);
1116    pte_addr = &page_table[pte_index];
1117    pte_dma = rk_dte_pt_address(dte) + pte_index * sizeof(u32);
1118    ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova, paddr, size, prot);
1119
1120    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1121
1122    return ret;
1123}
1124
1125static int rk_iommu_map_v2(struct iommu_domain *domain, unsigned long _iova, phys_addr_t paddr, size_t size, int prot,
1126                           gfp_t gfp)
1127{
1128    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1129    unsigned long flags;
1130    dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
1131    u32 *page_table, *pte_addr;
1132    u32 dte, pte_index;
1133    int ret;
1134
1135    spin_lock_irqsave(&rk_domain->dt_lock, flags);
1136
1137    /*
1138     * pgsize_bitmap specifies iova sizes that fit in one page table
1139     * (1024 4-KiB pages = 4 MiB).
1140     * So, size will always be 4096 <= size <= 4194304.
1141     * Since iommu_map() guarantees that both iova and size will be
1142     * aligned, we will always only be mapping from a single dte here.
1143     */
1144    page_table = rk_dte_get_page_table_v2(rk_domain, iova);
1145    if (IS_ERR(page_table)) {
1146        spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1147        return PTR_ERR(page_table);
1148    }
1149
1150    dte = rk_domain->dt[rk_iova_dte_index(iova)];
1151    pte_index = rk_iova_pte_index(iova);
1152    pte_addr = &page_table[pte_index];
1153    pte_dma = rk_dte_pt_address_v2(dte) + pte_index * sizeof(u32);
1154    ret = rk_iommu_map_iova_v2(rk_domain, pte_addr, pte_dma, iova, paddr, size, prot);
1155
1156    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1157
1158    return ret;
1159}
1160
1161static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova, size_t size,
1162                             struct iommu_iotlb_gather *gather)
1163{
1164    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1165    unsigned long flags;
1166    dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
1167    phys_addr_t pt_phys;
1168    u32 dte;
1169    u32 *pte_addr;
1170    size_t unmap_size;
1171
1172    spin_lock_irqsave(&rk_domain->dt_lock, flags);
1173
1174    /*
1175     * pgsize_bitmap specifies iova sizes that fit in one page table
1176     * (1024 4-KiB pages = 4 MiB).
1177     * So, size will always be 4096 <= size <= 4194304.
1178     * Since iommu_unmap() guarantees that both iova and size will be
1179     * aligned, we will always only be unmapping from a single dte here.
1180     */
1181    dte = rk_domain->dt[rk_iova_dte_index(iova)];
1182    /* Just return 0 if iova is unmapped */
1183    if (!rk_dte_is_pt_valid(dte)) {
1184        spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1185        return 0;
1186    }
1187
1188    pt_phys = rk_dte_pt_address(dte);
1189    pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
1190    pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
1191    unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
1192
1193    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1194
1195    /* Shootdown iotlb entries for iova range that was just unmapped */
1196    rk_iommu_zap_iova(rk_domain, iova, unmap_size);
1197
1198    return unmap_size;
1199}
1200
1201static size_t rk_iommu_unmap_v2(struct iommu_domain *domain, unsigned long _iova, size_t size,
1202                                struct iommu_iotlb_gather *gather)
1203{
1204    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1205    unsigned long flags;
1206    dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
1207    phys_addr_t pt_phys;
1208    u32 dte;
1209    u32 *pte_addr;
1210    size_t unmap_size;
1211
1212    spin_lock_irqsave(&rk_domain->dt_lock, flags);
1213
1214    /*
1215     * pgsize_bitmap specifies iova sizes that fit in one page table
1216     * (1024 4-KiB pages = 4 MiB).
1217     * So, size will always be 4096 <= size <= 4194304.
1218     * Since iommu_unmap() guarantees that both iova and size will be
1219     * aligned, we will always only be unmapping from a single dte here.
1220     */
1221    dte = rk_domain->dt[rk_iova_dte_index(iova)];
1222    /* Just return 0 if iova is unmapped */
1223    if (!rk_dte_is_pt_valid(dte)) {
1224        spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1225        return 0;
1226    }
1227
1228    pt_phys = rk_dte_pt_address_v2(dte);
1229    pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
1230    pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
1231    unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
1232
1233    spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
1234
1235    /* Shootdown iotlb entries for iova range that was just unmapped */
1236    /* Do not zap tlb cache line if shootdown_entire set */
1237    if (!rk_domain->shootdown_entire) {
1238        rk_iommu_zap_iova(rk_domain, iova, unmap_size);
1239    }
1240
1241    return unmap_size;
1242}
1243
1244static void rk_iommu_flush_tlb_all(struct iommu_domain *domain)
1245{
1246    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1247    struct list_head *pos;
1248    unsigned long flags;
1249    int i;
1250
1251    spin_lock_irqsave(&rk_domain->iommus_lock, flags);
1252    list_for_each(pos, &rk_domain->iommus)
1253    {
1254        struct rk_iommu *iommu;
1255        int ret;
1256
1257        iommu = list_entry(pos, struct rk_iommu, node);
1258
1259        ret = pm_runtime_get_if_in_use(iommu->dev);
1260        if (WARN_ON_ONCE(ret < 0)) {
1261            continue;
1262        }
1263        if (ret) {
1264            WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
1265            for (i = 0; i < iommu->num_mmu; i++) {
1266                rk_iommu_write(iommu->bases[i], RK_MMU_COMMAND, RK_MMU_CMD_ZAP_CACHE);
1267            }
1268            clk_bulk_disable(iommu->num_clocks, iommu->clocks);
1269            pm_runtime_put(iommu->dev);
1270        }
1271    }
1272    spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
1273}
1274
1275static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
1276{
1277    struct rk_iommudata *data = dev_iommu_priv_get(dev);
1278
1279    return data ? data->iommu : NULL;
1280}
1281
1282/* Must be called with iommu powered on and attached */
1283static void rk_iommu_disable(struct rk_iommu *iommu)
1284{
1285    int i;
1286
1287    /* Ignore error while disabling, just keep going */
1288    WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
1289    rk_iommu_enable_stall(iommu);
1290    rk_iommu_disable_paging(iommu);
1291    for (i = 0; i < iommu->num_mmu; i++) {
1292        rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
1293        rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
1294    }
1295    rk_iommu_disable_stall(iommu);
1296    clk_bulk_disable(iommu->num_clocks, iommu->clocks);
1297}
1298
1299int rockchip_iommu_disable(struct device *dev)
1300{
1301    struct rk_iommu *iommu;
1302
1303    iommu = rk_iommu_from_dev(dev);
1304    if (!iommu) {
1305        return -ENODEV;
1306    }
1307
1308    rk_iommu_disable(iommu);
1309
1310    return 0;
1311}
1312EXPORT_SYMBOL(rockchip_iommu_disable);
1313
1314/* Must be called with iommu powered on and attached */
1315static int rk_iommu_enable(struct rk_iommu *iommu)
1316{
1317    struct iommu_domain *domain = iommu->domain;
1318    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1319    int ret, i;
1320    u32 dt_v2;
1321    u32 auto_gate;
1322
1323    ret = clk_bulk_enable(iommu->num_clocks, iommu->clocks);
1324    if (ret) {
1325        return ret;
1326    }
1327
1328    ret = rk_iommu_enable_stall(iommu);
1329    if (ret) {
1330        goto out_disable_clocks;
1331    }
1332
1333    ret = rk_iommu_force_reset(iommu);
1334    if (ret) {
1335        goto out_disable_stall;
1336    }
1337
1338    for (i = 0; i < iommu->num_mmu; i++) {
1339        if (iommu->version >= 0x2) {
1340            dt_v2 = (rk_domain->dt_dma & DT_LO_MASK) | ((rk_domain->dt_dma & DT_HI_MASK) >> DT_SHIFT);
1341            rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, dt_v2);
1342        } else {
1343            rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, rk_domain->dt_dma);
1344        }
1345        rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
1346        rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
1347
1348        /* Workaround for iommu blocked, BIT(31) default to 1 */
1349        auto_gate = rk_iommu_read(iommu->bases[i], RK_MMU_AUTO_GATING);
1350        auto_gate |= DISABLE_FETCH_DTE_TIME_LIMIT;
1351        rk_iommu_write(iommu->bases[i], RK_MMU_AUTO_GATING, auto_gate);
1352    }
1353
1354    ret = rk_iommu_enable_paging(iommu);
1355
1356out_disable_stall:
1357    rk_iommu_disable_stall(iommu);
1358out_disable_clocks:
1359    clk_bulk_disable(iommu->num_clocks, iommu->clocks);
1360    return ret;
1361}
1362
1363int rockchip_iommu_enable(struct device *dev)
1364{
1365    struct rk_iommu *iommu;
1366
1367    iommu = rk_iommu_from_dev(dev);
1368    if (!iommu) {
1369        return -ENODEV;
1370    }
1371
1372    return rk_iommu_enable(iommu);
1373}
1374EXPORT_SYMBOL(rockchip_iommu_enable);
1375
1376static void rk_iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1377{
1378    struct rk_iommu *iommu;
1379    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1380    unsigned long flags;
1381    int ret;
1382
1383    /* Allow 'virtual devices' (eg drm) to detach from domain */
1384    iommu = rk_iommu_from_dev(dev);
1385    if (!iommu) {
1386        return;
1387    }
1388
1389    dev_dbg(dev, "Detaching from iommu domain\n");
1390
1391    if (!iommu->domain) {
1392        return;
1393    }
1394
1395    iommu->domain = NULL;
1396
1397    spin_lock_irqsave(&rk_domain->iommus_lock, flags);
1398    list_del_init(&iommu->node);
1399    spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
1400
1401    ret = pm_runtime_get_if_in_use(iommu->dev);
1402    WARN_ON_ONCE(ret < 0);
1403    if (ret > 0) {
1404        rk_iommu_disable(iommu);
1405        pm_runtime_put(iommu->dev);
1406    }
1407}
1408
1409static int rk_iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1410{
1411    struct rk_iommu *iommu;
1412    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1413    unsigned long flags;
1414    int ret;
1415
1416    /*
1417     * Allow 'virtual devices' (e.g., drm) to attach to domain.
1418     * Such a device does not belong to an iommu group.
1419     */
1420    iommu = rk_iommu_from_dev(dev);
1421    if (!iommu) {
1422        return 0;
1423    }
1424
1425    dev_dbg(dev, "Attaching to iommu domain\n");
1426
1427    if (iommu->domain) {
1428        rk_iommu_detach_device(iommu->domain, dev);
1429    }
1430
1431    iommu->domain = domain;
1432
1433    /* Attach NULL for disable iommu */
1434    if (!domain) {
1435        return 0;
1436    }
1437
1438    spin_lock_irqsave(&rk_domain->iommus_lock, flags);
1439    list_add_tail(&iommu->node, &rk_domain->iommus);
1440    spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
1441
1442    rk_domain->shootdown_entire = iommu->shootdown_entire;
1443    ret = pm_runtime_get_if_in_use(iommu->dev);
1444    if (!ret || WARN_ON_ONCE(ret < 0)) {
1445        return 0;
1446    }
1447
1448    ret = rk_iommu_enable(iommu);
1449    if (ret) {
1450        rk_iommu_detach_device(iommu->domain, dev);
1451    }
1452
1453    pm_runtime_put(iommu->dev);
1454
1455    return ret;
1456}
1457
1458static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
1459{
1460    struct rk_iommu_domain *rk_domain;
1461
1462    if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) {
1463        return NULL;
1464    }
1465
1466    if (!dma_dev) {
1467        return NULL;
1468    }
1469
1470    rk_domain = kzalloc(sizeof(*rk_domain), GFP_KERNEL);
1471    if (!rk_domain) {
1472        return NULL;
1473    }
1474
1475    if (type == IOMMU_DOMAIN_DMA && iommu_get_dma_cookie(&rk_domain->domain)) {
1476        goto err_free_domain;
1477    }
1478
1479    /*
1480     * rk32xx iommus use a 2 level pagetable.
1481     * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
1482     * Allocate one 4 KiB page for each table.
1483     */
1484    rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
1485    if (!rk_domain->dt) {
1486        goto err_put_cookie;
1487    }
1488
1489    rk_domain->dt_dma = dma_map_single(dma_dev, rk_domain->dt, SPAGE_SIZE, DMA_TO_DEVICE);
1490    if (dma_mapping_error(dma_dev, rk_domain->dt_dma)) {
1491        dev_err(dma_dev, "DMA map error for DT\n");
1492        goto err_free_dt;
1493    }
1494
1495    rk_table_flush(rk_domain, rk_domain->dt_dma, NUM_DT_ENTRIES);
1496
1497    spin_lock_init(&rk_domain->iommus_lock);
1498    spin_lock_init(&rk_domain->dt_lock);
1499    INIT_LIST_HEAD(&rk_domain->iommus);
1500
1501    rk_domain->domain.geometry.aperture_start = 0;
1502    rk_domain->domain.geometry.aperture_end = DMA_BIT_MASK(RK_DMA_BIT_MASK);
1503    rk_domain->domain.geometry.force_aperture = true;
1504
1505    return &rk_domain->domain;
1506
1507err_free_dt:
1508    free_page((unsigned long)rk_domain->dt);
1509err_put_cookie:
1510    if (type == IOMMU_DOMAIN_DMA) {
1511        iommu_put_dma_cookie(&rk_domain->domain);
1512    }
1513err_free_domain:
1514    kfree(rk_domain);
1515
1516    return NULL;
1517}
1518
1519static void rk_iommu_domain_free(struct iommu_domain *domain)
1520{
1521    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1522    int i;
1523
1524    WARN_ON(!list_empty(&rk_domain->iommus));
1525
1526    for (i = 0; i < NUM_DT_ENTRIES; i++) {
1527        u32 dte = rk_domain->dt[i];
1528        if (rk_dte_is_pt_valid(dte)) {
1529            phys_addr_t pt_phys = rk_dte_pt_address(dte);
1530            u32 *page_table = phys_to_virt(pt_phys);
1531            dma_unmap_single(dma_dev, pt_phys, SPAGE_SIZE, DMA_TO_DEVICE);
1532            free_page((unsigned long)page_table);
1533        }
1534    }
1535
1536    dma_unmap_single(dma_dev, rk_domain->dt_dma, SPAGE_SIZE, DMA_TO_DEVICE);
1537    free_page((unsigned long)rk_domain->dt);
1538
1539    if (domain->type == IOMMU_DOMAIN_DMA) {
1540        iommu_put_dma_cookie(&rk_domain->domain);
1541    }
1542    kfree(rk_domain);
1543}
1544
1545static void rk_iommu_domain_free_v2(struct iommu_domain *domain)
1546{
1547    struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
1548    int i;
1549
1550    WARN_ON(!list_empty(&rk_domain->iommus));
1551
1552    for (i = 0; i < NUM_DT_ENTRIES; i++) {
1553        u32 dte = rk_domain->dt[i];
1554
1555        if (rk_dte_is_pt_valid(dte)) {
1556            phys_addr_t pt_phys = rk_dte_pt_address_v2(dte);
1557            u32 *page_table = phys_to_virt(pt_phys);
1558
1559            dma_unmap_single(dma_dev, pt_phys, SPAGE_SIZE, DMA_TO_DEVICE);
1560            free_page((unsigned long)page_table);
1561        }
1562    }
1563
1564    dma_unmap_single(dma_dev, rk_domain->dt_dma, SPAGE_SIZE, DMA_TO_DEVICE);
1565    free_page((unsigned long)rk_domain->dt);
1566
1567    if (domain->type == IOMMU_DOMAIN_DMA) {
1568        iommu_put_dma_cookie(&rk_domain->domain);
1569    }
1570    kfree(rk_domain);
1571}
1572
1573static struct iommu_device *rk_iommu_probe_device(struct device *dev)
1574{
1575    struct rk_iommudata *data;
1576    struct rk_iommu *iommu;
1577
1578    data = dev_iommu_priv_get(dev);
1579    if (!data) {
1580        return ERR_PTR(-ENODEV);
1581    }
1582
1583    iommu = rk_iommu_from_dev(dev);
1584
1585    data->link = device_link_add(dev, iommu->dev, DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
1586
1587    data->defer_attach = false;
1588
1589    /* set max segment size for dev, needed for single chunk map */
1590    if (!dev->dma_parms) {
1591        dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
1592    }
1593    if (!dev->dma_parms) {
1594        return ERR_PTR(-ENOMEM);
1595    }
1596
1597    dma_set_max_seg_size(dev, DMA_BIT_MASK(RK_DMA_BIT_MASK));
1598
1599    return &iommu->iommu;
1600}
1601
1602static void rk_iommu_release_device(struct device *dev)
1603{
1604    struct rk_iommudata *data = dev_iommu_priv_get(dev);
1605
1606    device_link_del(data->link);
1607}
1608
1609static struct iommu_group *rk_iommu_device_group(struct device *dev)
1610{
1611    struct rk_iommu *iommu;
1612
1613    iommu = rk_iommu_from_dev(dev);
1614
1615    return iommu_group_ref_get(iommu->group);
1616}
1617
1618static bool rk_iommu_is_attach_deferred(struct iommu_domain *domain, struct device *dev)
1619{
1620    struct rk_iommudata *data = dev_iommu_priv_get(dev);
1621
1622    return data->defer_attach;
1623}
1624
1625static int rk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
1626{
1627    struct platform_device *iommu_dev;
1628    struct rk_iommudata *data;
1629
1630    data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL);
1631    if (!data) {
1632        return -ENOMEM;
1633    }
1634
1635    iommu_dev = of_find_device_by_node(args->np);
1636
1637    data->iommu = platform_get_drvdata(iommu_dev);
1638
1639    if (strstr(dev_name(dev), "vop")) {
1640        data->defer_attach = true;
1641    }
1642
1643    dev_iommu_priv_set(dev, data);
1644
1645    platform_device_put(iommu_dev);
1646
1647    return 0;
1648}
1649
1650void rk_iommu_mask_irq(struct device *dev)
1651{
1652    struct rk_iommu *iommu = rk_iommu_from_dev(dev);
1653    int i;
1654
1655    if (!iommu) {
1656        return;
1657    }
1658
1659    for (i = 0; i < iommu->num_mmu; i++) {
1660        rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
1661    }
1662}
1663EXPORT_SYMBOL(rk_iommu_mask_irq);
1664
1665void rk_iommu_unmask_irq(struct device *dev)
1666{
1667    struct rk_iommu *iommu = rk_iommu_from_dev(dev);
1668    int i;
1669
1670    if (!iommu) {
1671        return;
1672    }
1673
1674    for (i = 0; i < iommu->num_mmu; i++) {
1675        /* Need to zap tlb in case of mapping during pagefault */
1676        rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
1677        rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
1678        /* Leave iommu in pagefault state until mapping finished */
1679        rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
1680    }
1681}
1682EXPORT_SYMBOL(rk_iommu_unmask_irq);
1683
1684static struct iommu_ops rk_iommu_ops = {
1685    .domain_alloc = rk_iommu_domain_alloc,
1686    .domain_free = rk_iommu_domain_free,
1687    .attach_dev = rk_iommu_attach_device,
1688    .detach_dev = rk_iommu_detach_device,
1689    .map = rk_iommu_map,
1690    .unmap = rk_iommu_unmap,
1691    .flush_iotlb_all = rk_iommu_flush_tlb_all,
1692    .probe_device = rk_iommu_probe_device,
1693    .release_device = rk_iommu_release_device,
1694    .iova_to_phys = rk_iommu_iova_to_phys,
1695    .is_attach_deferred = rk_iommu_is_attach_deferred,
1696    .device_group = rk_iommu_device_group,
1697    .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1698    .of_xlate = rk_iommu_of_xlate,
1699};
1700
1701static struct iommu_ops rk_iommu_ops_v2 = {
1702    .domain_alloc = rk_iommu_domain_alloc,
1703    .domain_free = rk_iommu_domain_free_v2,
1704    .attach_dev = rk_iommu_attach_device,
1705    .detach_dev = rk_iommu_detach_device,
1706    .map = rk_iommu_map_v2,
1707    .unmap = rk_iommu_unmap_v2,
1708    .flush_iotlb_all = rk_iommu_flush_tlb_all,
1709    .probe_device = rk_iommu_probe_device,
1710    .release_device = rk_iommu_release_device,
1711    .iova_to_phys = rk_iommu_iova_to_phys_v2,
1712    .is_attach_deferred = rk_iommu_is_attach_deferred,
1713    .device_group = rk_iommu_device_group,
1714    .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1715    .of_xlate = rk_iommu_of_xlate,
1716};
1717
1718static const struct rockchip_iommu_data iommu_data_v1 = {
1719    .version = 0x1,
1720};
1721
1722static const struct rockchip_iommu_data iommu_data_v2 = {
1723    .version = 0x2,
1724};
1725
1726static const struct of_device_id rk_iommu_dt_ids[] = {
1727    {
1728        .compatible = "rockchip,iommu",
1729        .data = &iommu_data_v1,
1730    },
1731    {
1732        .compatible = "rockchip,iommu-v2",
1733        .data = &iommu_data_v2,
1734    },
1735    {}
1736};
1737
1738static int rk_iommu_probe(struct platform_device *pdev)
1739{
1740    struct device *dev = &pdev->dev;
1741    struct rk_iommu *iommu;
1742    struct resource *res;
1743    int num_res = pdev->num_resources;
1744    int err, i;
1745    const struct of_device_id *match;
1746    struct rockchip_iommu_data *data;
1747
1748    iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
1749    if (!iommu) {
1750        return -ENOMEM;
1751    }
1752
1753    match = of_match_device(rk_iommu_dt_ids, dev);
1754    if (!match) {
1755        return -EINVAL;
1756    }
1757
1758    data = (struct rockchip_iommu_data *)match->data;
1759    iommu->version = data->version;
1760    dev_info(dev, "version = %x\n", iommu->version);
1761
1762    platform_set_drvdata(pdev, iommu);
1763    iommu->dev = dev;
1764    iommu->num_mmu = 0;
1765
1766    iommu->bases = devm_kcalloc(dev, num_res, sizeof(*iommu->bases), GFP_KERNEL);
1767    if (!iommu->bases) {
1768        return -ENOMEM;
1769    }
1770
1771    for (i = 0; i < num_res; i++) {
1772        res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1773        if (!res) {
1774            continue;
1775        }
1776        iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
1777        if (IS_ERR(iommu->bases[i])) {
1778            continue;
1779        }
1780        iommu->num_mmu++;
1781    }
1782    if (iommu->num_mmu == 0) {
1783        return PTR_ERR(iommu->bases[0]);
1784    }
1785
1786    iommu->num_irq = platform_irq_count(pdev);
1787    if (iommu->num_irq < 0) {
1788        return iommu->num_irq;
1789    }
1790
1791    iommu->reset_disabled = device_property_read_bool(dev, "rockchip,disable-mmu-reset");
1792    iommu->skip_read = device_property_read_bool(dev, "rockchip,skip-mmu-read");
1793    iommu->dlr_disable = device_property_read_bool(dev, "rockchip,disable-device-link-resume");
1794    iommu->shootdown_entire = device_property_read_bool(dev, "rockchip,shootdown-entire");
1795
1796    if (of_machine_is_compatible("rockchip,rv1126") || of_machine_is_compatible("rockchip,rv1109")) {
1797        iommu->cmd_retry = device_property_read_bool(dev, "rockchip,enable-cmd-retry");
1798    }
1799
1800    /*
1801     * iommu clocks should be present for all new devices and devicetrees
1802     * but there are older devicetrees without clocks out in the wild.
1803     * So clocks as optional for the time being.
1804     */
1805    err = devm_clk_bulk_get_all(dev, &iommu->clocks);
1806    if (err == -ENOENT) {
1807        iommu->num_clocks = 0;
1808    } else if (err < 0) {
1809        return err;
1810    } else {
1811        iommu->num_clocks = err;
1812    }
1813
1814    err = clk_bulk_prepare(iommu->num_clocks, iommu->clocks);
1815    if (err) {
1816        return err;
1817    }
1818
1819    iommu->group = iommu_group_alloc();
1820    if (IS_ERR(iommu->group)) {
1821        err = PTR_ERR(iommu->group);
1822        goto err_unprepare_clocks;
1823    }
1824
1825    err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev));
1826    if (err) {
1827        goto err_put_group;
1828    }
1829
1830    if (iommu->version >= 0x2) {
1831        iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops_v2);
1832    } else {
1833        iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops);
1834    }
1835    iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode);
1836
1837    err = iommu_device_register(&iommu->iommu);
1838    if (err) {
1839        goto err_remove_sysfs;
1840    }
1841
1842    /*
1843     * Use the first registered IOMMU device for domain to use with DMA
1844     * API, since a domain might not physically correspond to a single
1845     * IOMMU device..
1846     */
1847    if (!dma_dev) {
1848        dma_dev = &pdev->dev;
1849    }
1850
1851    if (iommu->version >= 0x2) {
1852        bus_set_iommu(&platform_bus_type, &rk_iommu_ops_v2);
1853    } else {
1854        bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
1855    }
1856
1857    pm_runtime_enable(dev);
1858
1859    if (iommu->skip_read) {
1860        goto skip_request_irq;
1861    }
1862
1863    for (i = 0; i < iommu->num_irq; i++) {
1864        int irq = platform_get_irq(pdev, i);
1865        if (irq < 0) {
1866            return irq;
1867        }
1868
1869        err = devm_request_irq(iommu->dev, irq, rk_iommu_irq, IRQF_SHARED, dev_name(dev), iommu);
1870        if (err) {
1871            pm_runtime_disable(dev);
1872            goto err_remove_sysfs;
1873        }
1874    }
1875
1876skip_request_irq:
1877    return 0;
1878err_remove_sysfs:
1879    iommu_device_sysfs_remove(&iommu->iommu);
1880err_put_group:
1881    iommu_group_put(iommu->group);
1882err_unprepare_clocks:
1883    clk_bulk_unprepare(iommu->num_clocks, iommu->clocks);
1884    return err;
1885}
1886
1887static void rk_iommu_shutdown(struct platform_device *pdev)
1888{
1889    struct rk_iommu *iommu = platform_get_drvdata(pdev);
1890    int i;
1891
1892    for (i = 0; i < iommu->num_irq; i++) {
1893        int irq = platform_get_irq(pdev, i);
1894
1895        devm_free_irq(iommu->dev, irq, iommu);
1896    }
1897
1898    pm_runtime_force_suspend(&pdev->dev);
1899}
1900
1901static int __maybe_unused rk_iommu_suspend(struct device *dev)
1902{
1903    struct rk_iommu *iommu = dev_get_drvdata(dev);
1904
1905    if (!iommu->domain) {
1906        return 0;
1907    }
1908
1909    if (iommu->dlr_disable) {
1910        return 0;
1911    }
1912
1913    rk_iommu_disable(iommu);
1914    return 0;
1915}
1916
1917static int __maybe_unused rk_iommu_resume(struct device *dev)
1918{
1919    struct rk_iommu *iommu = dev_get_drvdata(dev);
1920
1921    if (!iommu->domain) {
1922        return 0;
1923    }
1924
1925    if (iommu->dlr_disable) {
1926        return 0;
1927    }
1928
1929    return rk_iommu_enable(iommu);
1930}
1931
1932static const struct dev_pm_ops rk_iommu_pm_ops = {
1933    SET_RUNTIME_PM_OPS(rk_iommu_suspend, rk_iommu_resume, NULL)
1934        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)};
1935
1936static struct platform_driver rk_iommu_driver = {
1937    .probe = rk_iommu_probe,
1938    .shutdown = rk_iommu_shutdown,
1939    .driver =
1940        {
1941            .name = "rk_iommu",
1942            .of_match_table = rk_iommu_dt_ids,
1943            .pm = &rk_iommu_pm_ops,
1944            .suppress_bind_attrs = true,
1945        },
1946};
1947
1948static int __init rk_iommu_init(void)
1949{
1950    return platform_driver_register(&rk_iommu_driver);
1951}
1952subsys_initcall(rk_iommu_init);
1953
1954MODULE_DESCRIPTION("IOMMU API for Rockchip");
1955MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
1956MODULE_ALIAS("platform:rockchip-iommu");
1957MODULE_LICENSE("GPL v2");
1958