blob: 1dacca8bffe1aa7e8f8d963cf343f68957b58613 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0 OR MIT
2/* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
3
4#include <linux/interrupt.h>
5#include <linux/io.h>
6#include <linux/device.h>
7#include <linux/slab.h>
8
9#include <drm/lima_drm.h>
10
11#include "lima_device.h"
12#include "lima_pp.h"
13#include "lima_dlbu.h"
14#include "lima_bcast.h"
15#include "lima_vm.h"
16#include "lima_regs.h"
17
18#define pp_write(reg, data) writel(data, ip->iomem + reg)
19#define pp_read(reg) readl(ip->iomem + reg)
20
21static void lima_pp_handle_irq(struct lima_ip *ip, u32 state)
22{
23 struct lima_device *dev = ip->dev;
24 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_pp;
25
26 if (state & LIMA_PP_IRQ_MASK_ERROR) {
27 u32 status = pp_read(LIMA_PP_STATUS);
28
29 dev_err(dev->dev, "pp error irq state=%x status=%x\n",
30 state, status);
31
32 pipe->error = true;
33
34 /* mask all interrupts before hard reset */
35 pp_write(LIMA_PP_INT_MASK, 0);
36 }
37
38 pp_write(LIMA_PP_INT_CLEAR, state);
39}
40
41static irqreturn_t lima_pp_irq_handler(int irq, void *data)
42{
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);
47
48 /* for shared irq case */
49 if (!state)
50 return IRQ_NONE;
51
52 lima_pp_handle_irq(ip, state);
53
54 if (atomic_dec_and_test(&pipe->task))
55 lima_sched_pipe_task_done(pipe);
56
57 return IRQ_HANDLED;
58}
59
60static irqreturn_t lima_pp_bcast_irq_handler(int irq, void *data)
61{
62 int i;
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;
68
69 /* for shared irq case */
70 if (!pipe->current_task)
71 return IRQ_NONE;
72
73 frame = pipe->current_task->frame;
74
75 for (i = 0; i < frame->num_pp; i++) {
76 struct lima_ip *ip = pipe->processor[i];
77 u32 status, state;
78
79 if (pipe->done & (1 << i))
80 continue;
81
82 /* status read first in case int state change in the middle
83 * which may miss the interrupt handling
84 */
85 status = pp_read(LIMA_PP_STATUS);
86 state = pp_read(LIMA_PP_INT_STATUS);
87
88 if (state) {
89 lima_pp_handle_irq(ip, state);
90 ret = IRQ_HANDLED;
91 } else {
92 if (status & LIMA_PP_STATUS_RENDERING_ACTIVE)
93 continue;
94 }
95
96 pipe->done |= (1 << i);
97 if (atomic_dec_and_test(&pipe->task))
98 lima_sched_pipe_task_done(pipe);
99 }
100
101 return ret;
102}
103
104static void lima_pp_soft_reset_async(struct lima_ip *ip)
105{
106 if (ip->data.async_reset)
107 return;
108
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;
113}
114
115static int lima_pp_soft_reset_poll(struct lima_ip *ip)
116{
117 return !(pp_read(LIMA_PP_STATUS) & LIMA_PP_STATUS_RENDERING_ACTIVE) &&
118 pp_read(LIMA_PP_INT_RAWSTAT) == LIMA_PP_IRQ_RESET_COMPLETED;
119}
120
121static int lima_pp_soft_reset_async_wait_one(struct lima_ip *ip)
122{
123 struct lima_device *dev = ip->dev;
124 int ret;
125
126 ret = lima_poll_timeout(ip, lima_pp_soft_reset_poll, 0, 100);
127 if (ret) {
128 dev_err(dev->dev, "pp %s reset time out\n", lima_ip_name(ip));
129 return ret;
130 }
131
132 pp_write(LIMA_PP_INT_CLEAR, LIMA_PP_IRQ_MASK_ALL);
133 pp_write(LIMA_PP_INT_MASK, LIMA_PP_IRQ_MASK_USED);
134 return 0;
135}
136
137static int lima_pp_soft_reset_async_wait(struct lima_ip *ip)
138{
139 int i, err = 0;
140
141 if (!ip->data.async_reset)
142 return 0;
143
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;
148
149 for (i = 0; i < frame->num_pp; i++)
150 err |= lima_pp_soft_reset_async_wait_one(pipe->processor[i]);
151 } else
152 err = lima_pp_soft_reset_async_wait_one(ip);
153
154 ip->data.async_reset = false;
155 return err;
156}
157
158static void lima_pp_write_frame(struct lima_ip *ip, u32 *frame, u32 *wb)
159{
160 int i, j, n = 0;
161
162 for (i = 0; i < LIMA_PP_FRAME_REG_NUM; i++)
163 writel(frame[i], ip->iomem + LIMA_PP_FRAME + i * 4);
164
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);
168 }
169}
170
171static int lima_pp_hard_reset_poll(struct lima_ip *ip)
172{
173 pp_write(LIMA_PP_PERF_CNT_0_LIMIT, 0xC01A0000);
174 return pp_read(LIMA_PP_PERF_CNT_0_LIMIT) == 0xC01A0000;
175}
176
177static int lima_pp_hard_reset(struct lima_ip *ip)
178{
179 struct lima_device *dev = ip->dev;
180 int ret;
181
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);
186 if (ret) {
187 dev_err(dev->dev, "pp hard reset timeout\n");
188 return ret;
189 }
190
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);
194 return 0;
195}
196
197static void lima_pp_print_version(struct lima_ip *ip)
198{
199 u32 version, major, minor;
200 char *name;
201
202 version = pp_read(LIMA_PP_VERSION);
203 major = (version >> 8) & 0xFF;
204 minor = version & 0xFF;
205 switch (version >> 16) {
206 case 0xC807:
207 name = "mali200";
208 break;
209 case 0xCE07:
210 name = "mali300";
211 break;
212 case 0xCD07:
213 name = "mali400";
214 break;
215 case 0xCF07:
216 name = "mali450";
217 break;
218 default:
219 name = "unknown";
220 break;
221 }
222 dev_info(ip->dev->dev, "%s - %s version major %d minor %d\n",
223 lima_ip_name(ip), name, major, minor);
224}
225
226int lima_pp_init(struct lima_ip *ip)
227{
228 struct lima_device *dev = ip->dev;
229 int err;
230
231 lima_pp_print_version(ip);
232
233 ip->data.async_reset = false;
234 lima_pp_soft_reset_async(ip);
235 err = lima_pp_soft_reset_async_wait(ip);
236 if (err)
237 return err;
238
239 err = devm_request_irq(dev->dev, ip->irq, lima_pp_irq_handler,
240 IRQF_SHARED, lima_ip_name(ip), ip);
241 if (err) {
242 dev_err(dev->dev, "pp %s fail to request irq\n",
243 lima_ip_name(ip));
244 return err;
245 }
246
247 dev->pp_version = pp_read(LIMA_PP_VERSION);
248
249 return 0;
250}
251
252void lima_pp_fini(struct lima_ip *ip)
253{
254 struct lima_device *dev = ip->dev;
255
256 devm_free_irq(dev->dev, ip->irq, ip);
257}
258
259int lima_pp_bcast_init(struct lima_ip *ip)
260{
261 struct lima_device *dev = ip->dev;
262 int err;
263
264 err = devm_request_irq(dev->dev, ip->irq, lima_pp_bcast_irq_handler,
265 IRQF_SHARED, lima_ip_name(ip), ip);
266 if (err) {
267 dev_err(dev->dev, "pp %s fail to request irq\n",
268 lima_ip_name(ip));
269 return err;
270 }
271
272 return 0;
273}
274
275void lima_pp_bcast_fini(struct lima_ip *ip)
276{
277 struct lima_device *dev = ip->dev;
278
279 devm_free_irq(dev->dev, ip->irq, ip);
280}
281
282static int lima_pp_task_validate(struct lima_sched_pipe *pipe,
283 struct lima_sched_task *task)
284{
285 u32 num_pp;
286
287 if (pipe->bcast_processor) {
288 struct drm_lima_m450_pp_frame *f = task->frame;
289
290 num_pp = f->num_pp;
291
292 if (f->_pad)
293 return -EINVAL;
294 } else {
295 struct drm_lima_m400_pp_frame *f = task->frame;
296
297 num_pp = f->num_pp;
298 }
299
300 if (num_pp == 0 || num_pp > pipe->num_processor)
301 return -EINVAL;
302
303 return 0;
304}
305
306static void lima_pp_task_run(struct lima_sched_pipe *pipe,
307 struct lima_sched_task *task)
308{
309 if (pipe->bcast_processor) {
310 struct drm_lima_m450_pp_frame *frame = task->frame;
311 struct lima_device *dev = pipe->bcast_processor->dev;
312 struct lima_ip *ip = pipe->bcast_processor;
313 int i;
314
315 pipe->done = 0;
316 atomic_set(&pipe->task, frame->num_pp);
317
318 if (frame->use_dlbu) {
319 lima_dlbu_enable(dev, frame->num_pp);
320
321 frame->frame[LIMA_PP_FRAME >> 2] = LIMA_VA_RESERVE_DLBU;
322 lima_dlbu_set_reg(dev->ip + lima_ip_dlbu, frame->dlbu_regs);
323 } else
324 lima_dlbu_disable(dev);
325
326 lima_bcast_enable(dev, frame->num_pp);
327
328 lima_pp_soft_reset_async_wait(ip);
329
330 lima_pp_write_frame(ip, frame->frame, frame->wb);
331
332 for (i = 0; i < frame->num_pp; i++) {
333 struct lima_ip *ip = pipe->processor[i];
334
335 pp_write(LIMA_PP_STACK, frame->fragment_stack_address[i]);
336 if (!frame->use_dlbu)
337 pp_write(LIMA_PP_FRAME, frame->plbu_array_address[i]);
338 }
339
340 pp_write(LIMA_PP_CTRL, LIMA_PP_CTRL_START_RENDERING);
341 } else {
342 struct drm_lima_m400_pp_frame *frame = task->frame;
343 int i;
344
345 atomic_set(&pipe->task, frame->num_pp);
346
347 for (i = 0; i < frame->num_pp; i++) {
348 struct lima_ip *ip = pipe->processor[i];
349
350 frame->frame[LIMA_PP_FRAME >> 2] =
351 frame->plbu_array_address[i];
352 frame->frame[LIMA_PP_STACK >> 2] =
353 frame->fragment_stack_address[i];
354
355 lima_pp_soft_reset_async_wait(ip);
356
357 lima_pp_write_frame(ip, frame->frame, frame->wb);
358
359 pp_write(LIMA_PP_CTRL, LIMA_PP_CTRL_START_RENDERING);
360 }
361 }
362}
363
364static void lima_pp_task_fini(struct lima_sched_pipe *pipe)
365{
366 if (pipe->bcast_processor)
367 lima_pp_soft_reset_async(pipe->bcast_processor);
368 else {
369 int i;
370
371 for (i = 0; i < pipe->num_processor; i++)
372 lima_pp_soft_reset_async(pipe->processor[i]);
373 }
374}
375
376static void lima_pp_task_error(struct lima_sched_pipe *pipe)
377{
378 int i;
379
380 for (i = 0; i < pipe->num_processor; i++) {
381 struct lima_ip *ip = pipe->processor[i];
382
383 dev_err(ip->dev->dev, "pp task error %d int_state=%x status=%x\n",
384 i, pp_read(LIMA_PP_INT_STATUS), pp_read(LIMA_PP_STATUS));
385
386 lima_pp_hard_reset(ip);
387 }
388}
389
390static void lima_pp_task_mmu_error(struct lima_sched_pipe *pipe)
391{
392 if (atomic_dec_and_test(&pipe->task))
393 lima_sched_pipe_task_done(pipe);
394}
395
396static struct kmem_cache *lima_pp_task_slab;
397static int lima_pp_task_slab_refcnt;
398
399int lima_pp_pipe_init(struct lima_device *dev)
400{
401 int frame_size;
402 struct lima_sched_pipe *pipe = dev->pipe + lima_pipe_pp;
403
404 if (dev->id == lima_gpu_mali400)
405 frame_size = sizeof(struct drm_lima_m400_pp_frame);
406 else
407 frame_size = sizeof(struct drm_lima_m450_pp_frame);
408
409 if (!lima_pp_task_slab) {
410 lima_pp_task_slab = kmem_cache_create_usercopy(
411 "lima_pp_task", sizeof(struct lima_sched_task) + frame_size,
412 0, SLAB_HWCACHE_ALIGN, sizeof(struct lima_sched_task),
413 frame_size, NULL);
414 if (!lima_pp_task_slab)
415 return -ENOMEM;
416 }
417 lima_pp_task_slab_refcnt++;
418
419 pipe->frame_size = frame_size;
420 pipe->task_slab = lima_pp_task_slab;
421
422 pipe->task_validate = lima_pp_task_validate;
423 pipe->task_run = lima_pp_task_run;
424 pipe->task_fini = lima_pp_task_fini;
425 pipe->task_error = lima_pp_task_error;
426 pipe->task_mmu_error = lima_pp_task_mmu_error;
427
428 return 0;
429}
430
431void lima_pp_pipe_fini(struct lima_device *dev)
432{
433 if (!--lima_pp_task_slab_refcnt) {
434 kmem_cache_destroy(lima_pp_task_slab);
435 lima_pp_task_slab = NULL;
436 }
437}