1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 #include <linux/interrupt.h>
5 #include <linux/iopoll.h>
6 #include <linux/device.h>
7 #include <linux/slab.h>
9 #include <drm/lima_drm.h>
11 #include "lima_device.h"
13 #include "lima_regs.h"
17 #define gp_write(reg, data) writel(data, ip->iomem + reg)
18 #define gp_read(reg) readl(ip->iomem + reg)
20 static irqreturn_t lima_gp_irq_handler(int irq, void *data)
22 struct lima_ip *ip = data;
23 struct lima_device *dev = ip->dev;
24 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_gp;
25 struct lima_sched_task *task = pipe->current_task;
26 u32 state = gp_read(LIMA_GP_INT_STAT);
27 u32 status = gp_read(LIMA_GP_STATUS);
30 /* for shared irq case */
34 if (state & LIMA_GP_IRQ_MASK_ERROR) {
35 if ((state & LIMA_GP_IRQ_MASK_ERROR) ==
36 LIMA_GP_IRQ_PLBU_OUT_OF_MEM) {
37 dev_dbg(dev->dev, "%s out of heap irq status=%x\n",
38 lima_ip_name(ip), status);
40 dev_err(dev->dev, "%s error irq state=%x status=%x\n",
41 lima_ip_name(ip), state, status);
43 task->recoverable = false;
46 /* mask all interrupts before hard reset */
47 gp_write(LIMA_GP_INT_MASK, 0);
52 bool valid = state & (LIMA_GP_IRQ_VS_END_CMD_LST |
53 LIMA_GP_IRQ_PLBU_END_CMD_LST);
54 bool active = status & (LIMA_GP_STATUS_VS_ACTIVE |
55 LIMA_GP_STATUS_PLBU_ACTIVE);
56 done = valid && !active;
60 gp_write(LIMA_GP_INT_CLEAR, state);
63 lima_sched_pipe_task_done(pipe);
68 static void lima_gp_soft_reset_async(struct lima_ip *ip)
70 if (ip->data.async_reset)
73 gp_write(LIMA_GP_INT_MASK, 0);
74 gp_write(LIMA_GP_INT_CLEAR, LIMA_GP_IRQ_RESET_COMPLETED);
75 gp_write(LIMA_GP_CMD, LIMA_GP_CMD_SOFT_RESET);
76 ip->data.async_reset = true;
79 static int lima_gp_soft_reset_async_wait(struct lima_ip *ip)
81 struct lima_device *dev = ip->dev;
85 if (!ip->data.async_reset)
88 err = readl_poll_timeout(ip->iomem + LIMA_GP_INT_RAWSTAT, v,
89 v & LIMA_GP_IRQ_RESET_COMPLETED,
92 dev_err(dev->dev, "%s soft reset time out\n",
97 gp_write(LIMA_GP_INT_CLEAR, LIMA_GP_IRQ_MASK_ALL);
98 gp_write(LIMA_GP_INT_MASK, LIMA_GP_IRQ_MASK_USED);
100 ip->data.async_reset = false;
104 static int lima_gp_task_validate(struct lima_sched_pipe *pipe,
105 struct lima_sched_task *task)
107 struct drm_lima_gp_frame *frame = task->frame;
108 u32 *f = frame->frame;
111 if (f[LIMA_GP_VSCL_START_ADDR >> 2] >
112 f[LIMA_GP_VSCL_END_ADDR >> 2] ||
113 f[LIMA_GP_PLBUCL_START_ADDR >> 2] >
114 f[LIMA_GP_PLBUCL_END_ADDR >> 2] ||
115 f[LIMA_GP_PLBU_ALLOC_START_ADDR >> 2] >
116 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2])
119 if (f[LIMA_GP_VSCL_START_ADDR >> 2] ==
120 f[LIMA_GP_VSCL_END_ADDR >> 2] &&
121 f[LIMA_GP_PLBUCL_START_ADDR >> 2] ==
122 f[LIMA_GP_PLBUCL_END_ADDR >> 2])
128 static void lima_gp_task_run(struct lima_sched_pipe *pipe,
129 struct lima_sched_task *task)
131 struct lima_ip *ip = pipe->processor[0];
132 struct drm_lima_gp_frame *frame = task->frame;
133 u32 *f = frame->frame;
137 /* update real heap buffer size for GP */
138 for (i = 0; i < task->num_bos; i++) {
139 struct lima_bo *bo = task->bos[i];
142 lima_vm_get_va(task->vm, bo) ==
143 f[LIMA_GP_PLBU_ALLOC_START_ADDR >> 2]) {
144 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2] =
145 f[LIMA_GP_PLBU_ALLOC_START_ADDR >> 2] +
147 task->recoverable = true;
153 if (f[LIMA_GP_VSCL_START_ADDR >> 2] !=
154 f[LIMA_GP_VSCL_END_ADDR >> 2])
155 cmd |= LIMA_GP_CMD_START_VS;
156 if (f[LIMA_GP_PLBUCL_START_ADDR >> 2] !=
157 f[LIMA_GP_PLBUCL_END_ADDR >> 2])
158 cmd |= LIMA_GP_CMD_START_PLBU;
160 /* before any hw ops, wait last success task async soft reset */
161 lima_gp_soft_reset_async_wait(ip);
163 for (i = 0; i < LIMA_GP_FRAME_REG_NUM; i++)
164 writel(f[i], ip->iomem + LIMA_GP_VSCL_START_ADDR + i * 4);
166 gp_write(LIMA_GP_CMD, LIMA_GP_CMD_UPDATE_PLBU_ALLOC);
167 gp_write(LIMA_GP_CMD, cmd);
170 static int lima_gp_bus_stop_poll(struct lima_ip *ip)
172 return !!(gp_read(LIMA_GP_STATUS) & LIMA_GP_STATUS_BUS_STOPPED);
175 static int lima_gp_hard_reset_poll(struct lima_ip *ip)
177 gp_write(LIMA_GP_PERF_CNT_0_LIMIT, 0xC01A0000);
178 return gp_read(LIMA_GP_PERF_CNT_0_LIMIT) == 0xC01A0000;
181 static int lima_gp_hard_reset(struct lima_ip *ip)
183 struct lima_device *dev = ip->dev;
186 gp_write(LIMA_GP_PERF_CNT_0_LIMIT, 0xC0FFE000);
187 gp_write(LIMA_GP_INT_MASK, 0);
189 gp_write(LIMA_GP_CMD, LIMA_GP_CMD_STOP_BUS);
190 ret = lima_poll_timeout(ip, lima_gp_bus_stop_poll, 10, 100);
192 dev_err(dev->dev, "%s bus stop timeout\n", lima_ip_name(ip));
195 gp_write(LIMA_GP_CMD, LIMA_GP_CMD_RESET);
196 ret = lima_poll_timeout(ip, lima_gp_hard_reset_poll, 10, 100);
198 dev_err(dev->dev, "%s hard reset timeout\n", lima_ip_name(ip));
202 gp_write(LIMA_GP_PERF_CNT_0_LIMIT, 0);
203 gp_write(LIMA_GP_INT_CLEAR, LIMA_GP_IRQ_MASK_ALL);
204 gp_write(LIMA_GP_INT_MASK, LIMA_GP_IRQ_MASK_USED);
207 * if there was an async soft reset queued,
208 * don't wait for it in the next job
210 ip->data.async_reset = false;
215 static void lima_gp_task_fini(struct lima_sched_pipe *pipe)
217 lima_gp_soft_reset_async(pipe->processor[0]);
220 static void lima_gp_task_error(struct lima_sched_pipe *pipe)
222 struct lima_ip *ip = pipe->processor[0];
224 dev_err(ip->dev->dev, "%s task error int_state=%x status=%x\n",
225 lima_ip_name(ip), gp_read(LIMA_GP_INT_STAT),
226 gp_read(LIMA_GP_STATUS));
228 lima_gp_hard_reset(ip);
231 static void lima_gp_task_mmu_error(struct lima_sched_pipe *pipe)
233 lima_sched_pipe_task_done(pipe);
236 static void lima_gp_task_mask_irq(struct lima_sched_pipe *pipe)
238 struct lima_ip *ip = pipe->processor[0];
240 gp_write(LIMA_GP_INT_MASK, 0);
243 static int lima_gp_task_recover(struct lima_sched_pipe *pipe)
245 struct lima_ip *ip = pipe->processor[0];
246 struct lima_sched_task *task = pipe->current_task;
247 struct drm_lima_gp_frame *frame = task->frame;
248 u32 *f = frame->frame;
250 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2] -
251 f[LIMA_GP_PLBU_ALLOC_START_ADDR >> 2];
253 if (fail_size == task->heap->heap_size) {
256 ret = lima_heap_alloc(task->heap, task->vm);
261 gp_write(LIMA_GP_INT_MASK, LIMA_GP_IRQ_MASK_USED);
262 /* Resume from where we stopped, i.e. new start is old end */
263 gp_write(LIMA_GP_PLBU_ALLOC_START_ADDR,
264 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2]);
265 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2] =
266 f[LIMA_GP_PLBU_ALLOC_START_ADDR >> 2] + task->heap->heap_size;
267 gp_write(LIMA_GP_PLBU_ALLOC_END_ADDR,
268 f[LIMA_GP_PLBU_ALLOC_END_ADDR >> 2]);
269 gp_write(LIMA_GP_CMD, LIMA_GP_CMD_UPDATE_PLBU_ALLOC);
273 static void lima_gp_print_version(struct lima_ip *ip)
275 u32 version, major, minor;
278 version = gp_read(LIMA_GP_VERSION);
279 major = (version >> 8) & 0xFF;
280 minor = version & 0xFF;
281 switch (version >> 16) {
298 dev_info(ip->dev->dev, "%s - %s version major %d minor %d\n",
299 lima_ip_name(ip), name, major, minor);
302 static struct kmem_cache *lima_gp_task_slab;
303 static int lima_gp_task_slab_refcnt;
305 static int lima_gp_hw_init(struct lima_ip *ip)
307 ip->data.async_reset = false;
308 lima_gp_soft_reset_async(ip);
309 return lima_gp_soft_reset_async_wait(ip);
312 int lima_gp_resume(struct lima_ip *ip)
314 return lima_gp_hw_init(ip);
317 void lima_gp_suspend(struct lima_ip *ip)
322 int lima_gp_init(struct lima_ip *ip)
324 struct lima_device *dev = ip->dev;
327 lima_gp_print_version(ip);
329 err = lima_gp_hw_init(ip);
333 err = devm_request_irq(dev->dev, ip->irq, lima_gp_irq_handler,
334 IRQF_SHARED, lima_ip_name(ip), ip);
336 dev_err(dev->dev, "%s fail to request irq\n",
341 dev->gp_version = gp_read(LIMA_GP_VERSION);
346 void lima_gp_fini(struct lima_ip *ip)
348 struct lima_device *dev = ip->dev;
350 devm_free_irq(dev->dev, ip->irq, ip);
353 int lima_gp_pipe_init(struct lima_device *dev)
355 int frame_size = sizeof(struct drm_lima_gp_frame);
356 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_gp;
358 if (!lima_gp_task_slab) {
359 lima_gp_task_slab = kmem_cache_create_usercopy(
360 "lima_gp_task", sizeof(struct lima_sched_task) + frame_size,
361 0, SLAB_HWCACHE_ALIGN, sizeof(struct lima_sched_task),
363 if (!lima_gp_task_slab)
366 lima_gp_task_slab_refcnt++;
368 pipe->frame_size = frame_size;
369 pipe->task_slab = lima_gp_task_slab;
371 pipe->task_validate = lima_gp_task_validate;
372 pipe->task_run = lima_gp_task_run;
373 pipe->task_fini = lima_gp_task_fini;
374 pipe->task_error = lima_gp_task_error;
375 pipe->task_mmu_error = lima_gp_task_mmu_error;
376 pipe->task_recover = lima_gp_task_recover;
377 pipe->task_mask_irq = lima_gp_task_mask_irq;
382 void lima_gp_pipe_fini(struct lima_device *dev)
384 if (!--lima_gp_task_slab_refcnt) {
385 kmem_cache_destroy(lima_gp_task_slab);
386 lima_gp_task_slab = NULL;