1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
4 #include <linux/interrupt.h>
5 #include <linux/iopoll.h>
6 #include <linux/device.h>
8 #include "lima_device.h"
11 #include "lima_regs.h"
13 #define mmu_write(reg, data) writel(data, ip->iomem + reg)
14 #define mmu_read(reg) readl(ip->iomem + reg)
16 #define lima_mmu_send_command(cmd, addr, val, cond) \
20 mmu_write(LIMA_MMU_COMMAND, cmd); \
21 __ret = readl_poll_timeout(ip->iomem + (addr), val, \
25 "mmu command %x timeout\n", cmd); \
29 static irqreturn_t
lima_mmu_irq_handler(int irq
, void *data
)
31 struct lima_ip
*ip
= data
;
32 struct lima_device
*dev
= ip
->dev
;
33 u32 status
= mmu_read(LIMA_MMU_INT_STATUS
);
34 struct lima_sched_pipe
*pipe
;
36 /* for shared irq case */
40 if (status
& LIMA_MMU_INT_PAGE_FAULT
) {
41 u32 fault
= mmu_read(LIMA_MMU_PAGE_FAULT_ADDR
);
43 dev_err(dev
->dev
, "mmu page fault at 0x%x from bus id %d of type %s on %s\n",
44 fault
, LIMA_MMU_STATUS_BUS_ID(status
),
45 status
& LIMA_MMU_STATUS_PAGE_FAULT_IS_WRITE
? "write" : "read",
49 if (status
& LIMA_MMU_INT_READ_BUS_ERROR
)
50 dev_err(dev
->dev
, "mmu %s irq bus error\n", lima_ip_name(ip
));
52 /* mask all interrupts before resume */
53 mmu_write(LIMA_MMU_INT_MASK
, 0);
54 mmu_write(LIMA_MMU_INT_CLEAR
, status
);
56 pipe
= dev
->pipe
+ (ip
->id
== lima_ip_gpmmu
? lima_pipe_gp
: lima_pipe_pp
);
57 lima_sched_pipe_mmu_error(pipe
);
62 static int lima_mmu_hw_init(struct lima_ip
*ip
)
64 struct lima_device
*dev
= ip
->dev
;
68 mmu_write(LIMA_MMU_COMMAND
, LIMA_MMU_COMMAND_HARD_RESET
);
69 err
= lima_mmu_send_command(LIMA_MMU_COMMAND_HARD_RESET
,
70 LIMA_MMU_DTE_ADDR
, v
, v
== 0);
74 mmu_write(LIMA_MMU_INT_MASK
,
75 LIMA_MMU_INT_PAGE_FAULT
| LIMA_MMU_INT_READ_BUS_ERROR
);
76 mmu_write(LIMA_MMU_DTE_ADDR
, dev
->empty_vm
->pd
.dma
);
77 return lima_mmu_send_command(LIMA_MMU_COMMAND_ENABLE_PAGING
,
79 v
& LIMA_MMU_STATUS_PAGING_ENABLED
);
82 int lima_mmu_resume(struct lima_ip
*ip
)
84 if (ip
->id
== lima_ip_ppmmu_bcast
)
87 return lima_mmu_hw_init(ip
);
90 void lima_mmu_suspend(struct lima_ip
*ip
)
95 int lima_mmu_init(struct lima_ip
*ip
)
97 struct lima_device
*dev
= ip
->dev
;
100 if (ip
->id
== lima_ip_ppmmu_bcast
)
103 mmu_write(LIMA_MMU_DTE_ADDR
, 0xCAFEBABE);
104 if (mmu_read(LIMA_MMU_DTE_ADDR
) != 0xCAFEB000) {
105 dev_err(dev
->dev
, "mmu %s dte write test fail\n", lima_ip_name(ip
));
109 err
= devm_request_irq(dev
->dev
, ip
->irq
, lima_mmu_irq_handler
,
110 IRQF_SHARED
, lima_ip_name(ip
), ip
);
112 dev_err(dev
->dev
, "mmu %s fail to request irq\n", lima_ip_name(ip
));
116 return lima_mmu_hw_init(ip
);
119 void lima_mmu_fini(struct lima_ip
*ip
)
124 void lima_mmu_flush_tlb(struct lima_ip
*ip
)
126 mmu_write(LIMA_MMU_COMMAND
, LIMA_MMU_COMMAND_ZAP_CACHE
);
129 void lima_mmu_switch_vm(struct lima_ip
*ip
, struct lima_vm
*vm
)
131 struct lima_device
*dev
= ip
->dev
;
134 lima_mmu_send_command(LIMA_MMU_COMMAND_ENABLE_STALL
,
136 v
& LIMA_MMU_STATUS_STALL_ACTIVE
);
138 mmu_write(LIMA_MMU_DTE_ADDR
, vm
->pd
.dma
);
141 mmu_write(LIMA_MMU_COMMAND
, LIMA_MMU_COMMAND_ZAP_CACHE
);
143 lima_mmu_send_command(LIMA_MMU_COMMAND_DISABLE_STALL
,
145 !(v
& LIMA_MMU_STATUS_STALL_ACTIVE
));
148 void lima_mmu_page_fault_resume(struct lima_ip
*ip
)
150 struct lima_device
*dev
= ip
->dev
;
151 u32 status
= mmu_read(LIMA_MMU_STATUS
);
154 if (status
& LIMA_MMU_STATUS_PAGE_FAULT_ACTIVE
) {
155 dev_info(dev
->dev
, "mmu resume\n");
157 mmu_write(LIMA_MMU_INT_MASK
, 0);
158 mmu_write(LIMA_MMU_DTE_ADDR
, 0xCAFEBABE);
159 lima_mmu_send_command(LIMA_MMU_COMMAND_HARD_RESET
,
160 LIMA_MMU_DTE_ADDR
, v
, v
== 0);
161 mmu_write(LIMA_MMU_INT_MASK
, LIMA_MMU_INT_PAGE_FAULT
| LIMA_MMU_INT_READ_BUS_ERROR
);
162 mmu_write(LIMA_MMU_DTE_ADDR
, dev
->empty_vm
->pd
.dma
);
163 lima_mmu_send_command(LIMA_MMU_COMMAND_ENABLE_PAGING
,
165 v
& LIMA_MMU_STATUS_PAGING_ENABLED
);