1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
4 #include <linux/interrupt.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_dlbu.h"
14 #include "lima_bcast.h"
16 #include "lima_regs.h"
18 #define pp_write(reg, data) writel(data, ip->iomem + reg)
19 #define pp_read(reg) readl(ip->iomem + reg)
21 static void lima_pp_handle_irq(struct lima_ip
*ip
, u32 state
)
23 struct lima_device
*dev
= ip
->dev
;
24 struct lima_sched_pipe
*pipe
= dev
->pipe
+ lima_pipe_pp
;
26 if (state
& LIMA_PP_IRQ_MASK_ERROR
) {
27 u32 status
= pp_read(LIMA_PP_STATUS
);
29 dev_err(dev
->dev
, "pp error irq state=%x status=%x\n",
34 /* mask all interrupts before hard reset */
35 pp_write(LIMA_PP_INT_MASK
, 0);
38 pp_write(LIMA_PP_INT_CLEAR
, state
);
41 static irqreturn_t
lima_pp_irq_handler(int irq
, void *data
)
43 struct lima_ip
*ip
= data
;
44 struct lima_device
*dev
= ip
->dev
;
45 struct lima_sched_pipe
*pipe
= dev
->pipe
+ lima_pipe_pp
;
46 u32 state
= pp_read(LIMA_PP_INT_STATUS
);
48 /* for shared irq case */
52 lima_pp_handle_irq(ip
, state
);
54 if (atomic_dec_and_test(&pipe
->task
))
55 lima_sched_pipe_task_done(pipe
);
60 static irqreturn_t
lima_pp_bcast_irq_handler(int irq
, void *data
)
63 irqreturn_t ret
= IRQ_NONE
;
64 struct lima_ip
*pp_bcast
= data
;
65 struct lima_device
*dev
= pp_bcast
->dev
;
66 struct lima_sched_pipe
*pipe
= dev
->pipe
+ lima_pipe_pp
;
67 struct drm_lima_m450_pp_frame
*frame
;
69 /* for shared irq case */
70 if (!pipe
->current_task
)
73 frame
= pipe
->current_task
->frame
;
75 for (i
= 0; i
< frame
->num_pp
; i
++) {
76 struct lima_ip
*ip
= pipe
->processor
[i
];
79 if (pipe
->done
& (1 << i
))
82 /* status read first in case int state change in the middle
83 * which may miss the interrupt handling
85 status
= pp_read(LIMA_PP_STATUS
);
86 state
= pp_read(LIMA_PP_INT_STATUS
);
89 lima_pp_handle_irq(ip
, state
);
92 if (status
& LIMA_PP_STATUS_RENDERING_ACTIVE
)
96 pipe
->done
|= (1 << i
);
97 if (atomic_dec_and_test(&pipe
->task
))
98 lima_sched_pipe_task_done(pipe
);
104 static void lima_pp_soft_reset_async(struct lima_ip
*ip
)
106 if (ip
->data
.async_reset
)
109 pp_write(LIMA_PP_INT_MASK
, 0);
110 pp_write(LIMA_PP_INT_RAWSTAT
, LIMA_PP_IRQ_MASK_ALL
);
111 pp_write(LIMA_PP_CTRL
, LIMA_PP_CTRL_SOFT_RESET
);
112 ip
->data
.async_reset
= true;
115 static int lima_pp_soft_reset_poll(struct lima_ip
*ip
)
117 return !(pp_read(LIMA_PP_STATUS
) & LIMA_PP_STATUS_RENDERING_ACTIVE
) &&
118 pp_read(LIMA_PP_INT_RAWSTAT
) == LIMA_PP_IRQ_RESET_COMPLETED
;
121 static int lima_pp_soft_reset_async_wait_one(struct lima_ip
*ip
)
123 struct lima_device
*dev
= ip
->dev
;
126 ret
= lima_poll_timeout(ip
, lima_pp_soft_reset_poll
, 0, 100);
128 dev_err(dev
->dev
, "pp %s reset time out\n", lima_ip_name(ip
));
132 pp_write(LIMA_PP_INT_CLEAR
, LIMA_PP_IRQ_MASK_ALL
);
133 pp_write(LIMA_PP_INT_MASK
, LIMA_PP_IRQ_MASK_USED
);
137 static int lima_pp_soft_reset_async_wait(struct lima_ip
*ip
)
141 if (!ip
->data
.async_reset
)
144 if (ip
->id
== lima_ip_pp_bcast
) {
145 struct lima_device
*dev
= ip
->dev
;
146 struct lima_sched_pipe
*pipe
= dev
->pipe
+ lima_pipe_pp
;
147 struct drm_lima_m450_pp_frame
*frame
= pipe
->current_task
->frame
;
149 for (i
= 0; i
< frame
->num_pp
; i
++)
150 err
|= lima_pp_soft_reset_async_wait_one(pipe
->processor
[i
]);
152 err
= lima_pp_soft_reset_async_wait_one(ip
);
154 ip
->data
.async_reset
= false;
158 static void lima_pp_write_frame(struct lima_ip
*ip
, u32
*frame
, u32
*wb
)
162 for (i
= 0; i
< LIMA_PP_FRAME_REG_NUM
; i
++)
163 writel(frame
[i
], ip
->iomem
+ LIMA_PP_FRAME
+ i
* 4);
165 for (i
= 0; i
< 3; i
++) {
166 for (j
= 0; j
< LIMA_PP_WB_REG_NUM
; j
++)
167 writel(wb
[n
++], ip
->iomem
+ LIMA_PP_WB(i
) + j
* 4);
171 static int lima_pp_hard_reset_poll(struct lima_ip
*ip
)
173 pp_write(LIMA_PP_PERF_CNT_0_LIMIT
, 0xC01A0000);
174 return pp_read(LIMA_PP_PERF_CNT_0_LIMIT
) == 0xC01A0000;
177 static int lima_pp_hard_reset(struct lima_ip
*ip
)
179 struct lima_device
*dev
= ip
->dev
;
182 pp_write(LIMA_PP_PERF_CNT_0_LIMIT
, 0xC0FFE000);
183 pp_write(LIMA_PP_INT_MASK
, 0);
184 pp_write(LIMA_PP_CTRL
, LIMA_PP_CTRL_FORCE_RESET
);
185 ret
= lima_poll_timeout(ip
, lima_pp_hard_reset_poll
, 10, 100);
187 dev_err(dev
->dev
, "pp hard reset timeout\n");
191 pp_write(LIMA_PP_PERF_CNT_0_LIMIT
, 0);
192 pp_write(LIMA_PP_INT_CLEAR
, LIMA_PP_IRQ_MASK_ALL
);
193 pp_write(LIMA_PP_INT_MASK
, LIMA_PP_IRQ_MASK_USED
);
197 static void lima_pp_print_version(struct lima_ip
*ip
)
199 u32 version
, major
, minor
;
202 version
= pp_read(LIMA_PP_VERSION
);
203 major
= (version
>> 8) & 0xFF;
204 minor
= version
& 0xFF;
205 switch (version
>> 16) {
222 dev_info(ip
->dev
->dev
, "%s - %s version major %d minor %d\n",
223 lima_ip_name(ip
), name
, major
, minor
);
226 static int lima_pp_hw_init(struct lima_ip
*ip
)
228 ip
->data
.async_reset
= false;
229 lima_pp_soft_reset_async(ip
);
230 return lima_pp_soft_reset_async_wait(ip
);
233 int lima_pp_resume(struct lima_ip
*ip
)
235 return lima_pp_hw_init(ip
);
238 void lima_pp_suspend(struct lima_ip
*ip
)
243 int lima_pp_init(struct lima_ip
*ip
)
245 struct lima_device
*dev
= ip
->dev
;
248 lima_pp_print_version(ip
);
250 err
= lima_pp_hw_init(ip
);
254 err
= devm_request_irq(dev
->dev
, ip
->irq
, lima_pp_irq_handler
,
255 IRQF_SHARED
, lima_ip_name(ip
), ip
);
257 dev_err(dev
->dev
, "pp %s fail to request irq\n",
262 dev
->pp_version
= pp_read(LIMA_PP_VERSION
);
267 void lima_pp_fini(struct lima_ip
*ip
)
272 int lima_pp_bcast_resume(struct lima_ip
*ip
)
274 /* PP has been reset by individual PP resume */
275 ip
->data
.async_reset
= false;
279 void lima_pp_bcast_suspend(struct lima_ip
*ip
)
284 int lima_pp_bcast_init(struct lima_ip
*ip
)
286 struct lima_device
*dev
= ip
->dev
;
289 err
= devm_request_irq(dev
->dev
, ip
->irq
, lima_pp_bcast_irq_handler
,
290 IRQF_SHARED
, lima_ip_name(ip
), ip
);
292 dev_err(dev
->dev
, "pp %s fail to request irq\n",
300 void lima_pp_bcast_fini(struct lima_ip
*ip
)
305 static int lima_pp_task_validate(struct lima_sched_pipe
*pipe
,
306 struct lima_sched_task
*task
)
310 if (pipe
->bcast_processor
) {
311 struct drm_lima_m450_pp_frame
*f
= task
->frame
;
318 struct drm_lima_m400_pp_frame
*f
= task
->frame
;
323 if (num_pp
== 0 || num_pp
> pipe
->num_processor
)
329 static void lima_pp_task_run(struct lima_sched_pipe
*pipe
,
330 struct lima_sched_task
*task
)
332 if (pipe
->bcast_processor
) {
333 struct drm_lima_m450_pp_frame
*frame
= task
->frame
;
334 struct lima_device
*dev
= pipe
->bcast_processor
->dev
;
335 struct lima_ip
*ip
= pipe
->bcast_processor
;
339 atomic_set(&pipe
->task
, frame
->num_pp
);
341 if (frame
->use_dlbu
) {
342 lima_dlbu_enable(dev
, frame
->num_pp
);
344 frame
->frame
[LIMA_PP_FRAME
>> 2] = LIMA_VA_RESERVE_DLBU
;
345 lima_dlbu_set_reg(dev
->ip
+ lima_ip_dlbu
, frame
->dlbu_regs
);
347 lima_dlbu_disable(dev
);
349 lima_bcast_enable(dev
, frame
->num_pp
);
351 lima_pp_soft_reset_async_wait(ip
);
353 lima_pp_write_frame(ip
, frame
->frame
, frame
->wb
);
355 for (i
= 0; i
< frame
->num_pp
; i
++) {
356 struct lima_ip
*ip
= pipe
->processor
[i
];
358 pp_write(LIMA_PP_STACK
, frame
->fragment_stack_address
[i
]);
359 if (!frame
->use_dlbu
)
360 pp_write(LIMA_PP_FRAME
, frame
->plbu_array_address
[i
]);
363 pp_write(LIMA_PP_CTRL
, LIMA_PP_CTRL_START_RENDERING
);
365 struct drm_lima_m400_pp_frame
*frame
= task
->frame
;
368 atomic_set(&pipe
->task
, frame
->num_pp
);
370 for (i
= 0; i
< frame
->num_pp
; i
++) {
371 struct lima_ip
*ip
= pipe
->processor
[i
];
373 frame
->frame
[LIMA_PP_FRAME
>> 2] =
374 frame
->plbu_array_address
[i
];
375 frame
->frame
[LIMA_PP_STACK
>> 2] =
376 frame
->fragment_stack_address
[i
];
378 lima_pp_soft_reset_async_wait(ip
);
380 lima_pp_write_frame(ip
, frame
->frame
, frame
->wb
);
382 pp_write(LIMA_PP_CTRL
, LIMA_PP_CTRL_START_RENDERING
);
387 static void lima_pp_task_fini(struct lima_sched_pipe
*pipe
)
389 if (pipe
->bcast_processor
)
390 lima_pp_soft_reset_async(pipe
->bcast_processor
);
394 for (i
= 0; i
< pipe
->num_processor
; i
++)
395 lima_pp_soft_reset_async(pipe
->processor
[i
]);
399 static void lima_pp_task_error(struct lima_sched_pipe
*pipe
)
403 for (i
= 0; i
< pipe
->num_processor
; i
++) {
404 struct lima_ip
*ip
= pipe
->processor
[i
];
406 dev_err(ip
->dev
->dev
, "pp task error %d int_state=%x status=%x\n",
407 i
, pp_read(LIMA_PP_INT_STATUS
), pp_read(LIMA_PP_STATUS
));
409 lima_pp_hard_reset(ip
);
413 static void lima_pp_task_mmu_error(struct lima_sched_pipe
*pipe
)
415 if (atomic_dec_and_test(&pipe
->task
))
416 lima_sched_pipe_task_done(pipe
);
419 static struct kmem_cache
*lima_pp_task_slab
;
420 static int lima_pp_task_slab_refcnt
;
422 int lima_pp_pipe_init(struct lima_device
*dev
)
425 struct lima_sched_pipe
*pipe
= dev
->pipe
+ lima_pipe_pp
;
427 if (dev
->id
== lima_gpu_mali400
)
428 frame_size
= sizeof(struct drm_lima_m400_pp_frame
);
430 frame_size
= sizeof(struct drm_lima_m450_pp_frame
);
432 if (!lima_pp_task_slab
) {
433 lima_pp_task_slab
= kmem_cache_create_usercopy(
434 "lima_pp_task", sizeof(struct lima_sched_task
) + frame_size
,
435 0, SLAB_HWCACHE_ALIGN
, sizeof(struct lima_sched_task
),
437 if (!lima_pp_task_slab
)
440 lima_pp_task_slab_refcnt
++;
442 pipe
->frame_size
= frame_size
;
443 pipe
->task_slab
= lima_pp_task_slab
;
445 pipe
->task_validate
= lima_pp_task_validate
;
446 pipe
->task_run
= lima_pp_task_run
;
447 pipe
->task_fini
= lima_pp_task_fini
;
448 pipe
->task_error
= lima_pp_task_error
;
449 pipe
->task_mmu_error
= lima_pp_task_mmu_error
;
454 void lima_pp_pipe_fini(struct lima_device
*dev
)
456 if (!--lima_pp_task_slab_refcnt
) {
457 kmem_cache_destroy(lima_pp_task_slab
);
458 lima_pp_task_slab
= NULL
;