1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2018 MediaTek Inc.
5 #include <linux/bitops.h>
7 #include <linux/clk-provider.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/mailbox_controller.h>
17 #include <linux/mailbox/mtk-cmdq-mailbox.h>
18 #include <linux/of_device.h>
20 #define CMDQ_OP_CODE_MASK (0xff << CMDQ_OP_CODE_SHIFT)
21 #define CMDQ_NUM_CMD(t) (t->cmd_buf_size / CMDQ_INST_SIZE)
23 #define CMDQ_CURR_IRQ_STATUS 0x10
24 #define CMDQ_SYNC_TOKEN_UPDATE 0x68
25 #define CMDQ_THR_SLOT_CYCLES 0x30
26 #define CMDQ_THR_BASE 0x100
27 #define CMDQ_THR_SIZE 0x80
28 #define CMDQ_THR_WARM_RESET 0x00
29 #define CMDQ_THR_ENABLE_TASK 0x04
30 #define CMDQ_THR_SUSPEND_TASK 0x08
31 #define CMDQ_THR_CURR_STATUS 0x0c
32 #define CMDQ_THR_IRQ_STATUS 0x10
33 #define CMDQ_THR_IRQ_ENABLE 0x14
34 #define CMDQ_THR_CURR_ADDR 0x20
35 #define CMDQ_THR_END_ADDR 0x24
36 #define CMDQ_THR_WAIT_TOKEN 0x30
37 #define CMDQ_THR_PRIORITY 0x40
39 #define CMDQ_THR_ACTIVE_SLOT_CYCLES 0x3200
40 #define CMDQ_THR_ENABLED 0x1
41 #define CMDQ_THR_DISABLED 0x0
42 #define CMDQ_THR_SUSPEND 0x1
43 #define CMDQ_THR_RESUME 0x0
44 #define CMDQ_THR_STATUS_SUSPENDED BIT(1)
45 #define CMDQ_THR_DO_WARM_RESET BIT(0)
46 #define CMDQ_THR_IRQ_DONE 0x1
47 #define CMDQ_THR_IRQ_ERROR 0x12
48 #define CMDQ_THR_IRQ_EN (CMDQ_THR_IRQ_ERROR | CMDQ_THR_IRQ_DONE)
49 #define CMDQ_THR_IS_WAITING BIT(31)
51 #define CMDQ_JUMP_BY_OFFSET 0x10000000
52 #define CMDQ_JUMP_BY_PA 0x10000001
55 struct mbox_chan
*chan
;
57 struct list_head task_busy_list
;
64 struct list_head list_entry
;
66 struct cmdq_thread
*thread
;
67 struct cmdq_pkt
*pkt
; /* the packet sent from mailbox client */
71 struct mbox_controller mbox
;
76 struct cmdq_thread
*thread
;
81 static int cmdq_thread_suspend(struct cmdq
*cmdq
, struct cmdq_thread
*thread
)
85 writel(CMDQ_THR_SUSPEND
, thread
->base
+ CMDQ_THR_SUSPEND_TASK
);
87 /* If already disabled, treat as suspended successful. */
88 if (!(readl(thread
->base
+ CMDQ_THR_ENABLE_TASK
) & CMDQ_THR_ENABLED
))
91 if (readl_poll_timeout_atomic(thread
->base
+ CMDQ_THR_CURR_STATUS
,
92 status
, status
& CMDQ_THR_STATUS_SUSPENDED
, 0, 10)) {
93 dev_err(cmdq
->mbox
.dev
, "suspend GCE thread 0x%x failed\n",
94 (u32
)(thread
->base
- cmdq
->base
));
101 static void cmdq_thread_resume(struct cmdq_thread
*thread
)
103 writel(CMDQ_THR_RESUME
, thread
->base
+ CMDQ_THR_SUSPEND_TASK
);
106 static void cmdq_init(struct cmdq
*cmdq
)
110 WARN_ON(clk_enable(cmdq
->clock
) < 0);
111 writel(CMDQ_THR_ACTIVE_SLOT_CYCLES
, cmdq
->base
+ CMDQ_THR_SLOT_CYCLES
);
112 for (i
= 0; i
<= CMDQ_MAX_EVENT
; i
++)
113 writel(i
, cmdq
->base
+ CMDQ_SYNC_TOKEN_UPDATE
);
114 clk_disable(cmdq
->clock
);
117 static int cmdq_thread_reset(struct cmdq
*cmdq
, struct cmdq_thread
*thread
)
121 writel(CMDQ_THR_DO_WARM_RESET
, thread
->base
+ CMDQ_THR_WARM_RESET
);
122 if (readl_poll_timeout_atomic(thread
->base
+ CMDQ_THR_WARM_RESET
,
123 warm_reset
, !(warm_reset
& CMDQ_THR_DO_WARM_RESET
),
125 dev_err(cmdq
->mbox
.dev
, "reset GCE thread 0x%x failed\n",
126 (u32
)(thread
->base
- cmdq
->base
));
133 static void cmdq_thread_disable(struct cmdq
*cmdq
, struct cmdq_thread
*thread
)
135 cmdq_thread_reset(cmdq
, thread
);
136 writel(CMDQ_THR_DISABLED
, thread
->base
+ CMDQ_THR_ENABLE_TASK
);
139 /* notify GCE to re-fetch commands by setting GCE thread PC */
140 static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread
*thread
)
142 writel(readl(thread
->base
+ CMDQ_THR_CURR_ADDR
),
143 thread
->base
+ CMDQ_THR_CURR_ADDR
);
146 static void cmdq_task_insert_into_thread(struct cmdq_task
*task
)
148 struct device
*dev
= task
->cmdq
->mbox
.dev
;
149 struct cmdq_thread
*thread
= task
->thread
;
150 struct cmdq_task
*prev_task
= list_last_entry(
151 &thread
->task_busy_list
, typeof(*task
), list_entry
);
152 u64
*prev_task_base
= prev_task
->pkt
->va_base
;
154 /* let previous task jump to this task */
155 dma_sync_single_for_cpu(dev
, prev_task
->pa_base
,
156 prev_task
->pkt
->cmd_buf_size
, DMA_TO_DEVICE
);
157 prev_task_base
[CMDQ_NUM_CMD(prev_task
->pkt
) - 1] =
158 (u64
)CMDQ_JUMP_BY_PA
<< 32 | task
->pa_base
;
159 dma_sync_single_for_device(dev
, prev_task
->pa_base
,
160 prev_task
->pkt
->cmd_buf_size
, DMA_TO_DEVICE
);
162 cmdq_thread_invalidate_fetched_data(thread
);
165 static bool cmdq_command_is_wfe(u64 cmd
)
167 u64 wfe_option
= CMDQ_WFE_UPDATE
| CMDQ_WFE_WAIT
| CMDQ_WFE_WAIT_VALUE
;
168 u64 wfe_op
= (u64
)(CMDQ_CODE_WFE
<< CMDQ_OP_CODE_SHIFT
) << 32;
169 u64 wfe_mask
= (u64
)CMDQ_OP_CODE_MASK
<< 32 | 0xffffffff;
171 return ((cmd
& wfe_mask
) == (wfe_op
| wfe_option
));
174 /* we assume tasks in the same display GCE thread are waiting the same event. */
175 static void cmdq_task_remove_wfe(struct cmdq_task
*task
)
177 struct device
*dev
= task
->cmdq
->mbox
.dev
;
178 u64
*base
= task
->pkt
->va_base
;
181 dma_sync_single_for_cpu(dev
, task
->pa_base
, task
->pkt
->cmd_buf_size
,
183 for (i
= 0; i
< CMDQ_NUM_CMD(task
->pkt
); i
++)
184 if (cmdq_command_is_wfe(base
[i
]))
185 base
[i
] = (u64
)CMDQ_JUMP_BY_OFFSET
<< 32 |
187 dma_sync_single_for_device(dev
, task
->pa_base
, task
->pkt
->cmd_buf_size
,
191 static bool cmdq_thread_is_in_wfe(struct cmdq_thread
*thread
)
193 return readl(thread
->base
+ CMDQ_THR_WAIT_TOKEN
) & CMDQ_THR_IS_WAITING
;
196 static void cmdq_thread_wait_end(struct cmdq_thread
*thread
,
197 unsigned long end_pa
)
199 struct device
*dev
= thread
->chan
->mbox
->dev
;
200 unsigned long curr_pa
;
202 if (readl_poll_timeout_atomic(thread
->base
+ CMDQ_THR_CURR_ADDR
,
203 curr_pa
, curr_pa
== end_pa
, 1, 20))
204 dev_err(dev
, "GCE thread cannot run to end.\n");
207 static void cmdq_task_exec_done(struct cmdq_task
*task
, enum cmdq_cb_status sta
)
209 struct cmdq_task_cb
*cb
= &task
->pkt
->async_cb
;
210 struct cmdq_cb_data data
;
212 WARN_ON(cb
->cb
== (cmdq_async_flush_cb
)NULL
);
214 data
.data
= cb
->data
;
217 list_del(&task
->list_entry
);
220 static void cmdq_task_handle_error(struct cmdq_task
*task
)
222 struct cmdq_thread
*thread
= task
->thread
;
223 struct cmdq_task
*next_task
;
225 dev_err(task
->cmdq
->mbox
.dev
, "task 0x%p error\n", task
);
226 WARN_ON(cmdq_thread_suspend(task
->cmdq
, thread
) < 0);
227 next_task
= list_first_entry_or_null(&thread
->task_busy_list
,
228 struct cmdq_task
, list_entry
);
230 writel(next_task
->pa_base
, thread
->base
+ CMDQ_THR_CURR_ADDR
);
231 cmdq_thread_resume(thread
);
234 static void cmdq_thread_irq_handler(struct cmdq
*cmdq
,
235 struct cmdq_thread
*thread
)
237 struct cmdq_task
*task
, *tmp
, *curr_task
= NULL
;
238 u32 curr_pa
, irq_flag
, task_end_pa
;
241 irq_flag
= readl(thread
->base
+ CMDQ_THR_IRQ_STATUS
);
242 writel(~irq_flag
, thread
->base
+ CMDQ_THR_IRQ_STATUS
);
245 * When ISR call this function, another CPU core could run
246 * "release task" right before we acquire the spin lock, and thus
247 * reset / disable this GCE thread, so we need to check the enable
248 * bit of this GCE thread.
250 if (!(readl(thread
->base
+ CMDQ_THR_ENABLE_TASK
) & CMDQ_THR_ENABLED
))
253 if (irq_flag
& CMDQ_THR_IRQ_ERROR
)
255 else if (irq_flag
& CMDQ_THR_IRQ_DONE
)
260 curr_pa
= readl(thread
->base
+ CMDQ_THR_CURR_ADDR
);
262 list_for_each_entry_safe(task
, tmp
, &thread
->task_busy_list
,
264 task_end_pa
= task
->pa_base
+ task
->pkt
->cmd_buf_size
;
265 if (curr_pa
>= task
->pa_base
&& curr_pa
< task_end_pa
)
268 if (!curr_task
|| curr_pa
== task_end_pa
- CMDQ_INST_SIZE
) {
269 cmdq_task_exec_done(task
, CMDQ_CB_NORMAL
);
272 cmdq_task_exec_done(task
, CMDQ_CB_ERROR
);
273 cmdq_task_handle_error(curr_task
);
281 if (list_empty(&thread
->task_busy_list
)) {
282 cmdq_thread_disable(cmdq
, thread
);
283 clk_disable(cmdq
->clock
);
287 static irqreturn_t
cmdq_irq_handler(int irq
, void *dev
)
289 struct cmdq
*cmdq
= dev
;
290 unsigned long irq_status
, flags
= 0L;
293 irq_status
= readl(cmdq
->base
+ CMDQ_CURR_IRQ_STATUS
) & cmdq
->irq_mask
;
294 if (!(irq_status
^ cmdq
->irq_mask
))
297 for_each_clear_bit(bit
, &irq_status
, cmdq
->thread_nr
) {
298 struct cmdq_thread
*thread
= &cmdq
->thread
[bit
];
300 spin_lock_irqsave(&thread
->chan
->lock
, flags
);
301 cmdq_thread_irq_handler(cmdq
, thread
);
302 spin_unlock_irqrestore(&thread
->chan
->lock
, flags
);
308 static int cmdq_suspend(struct device
*dev
)
310 struct cmdq
*cmdq
= dev_get_drvdata(dev
);
311 struct cmdq_thread
*thread
;
313 bool task_running
= false;
315 cmdq
->suspended
= true;
317 for (i
= 0; i
< cmdq
->thread_nr
; i
++) {
318 thread
= &cmdq
->thread
[i
];
319 if (!list_empty(&thread
->task_busy_list
)) {
326 dev_warn(dev
, "exist running task(s) in suspend\n");
328 clk_unprepare(cmdq
->clock
);
333 static int cmdq_resume(struct device
*dev
)
335 struct cmdq
*cmdq
= dev_get_drvdata(dev
);
337 WARN_ON(clk_prepare(cmdq
->clock
) < 0);
338 cmdq
->suspended
= false;
342 static int cmdq_remove(struct platform_device
*pdev
)
344 struct cmdq
*cmdq
= platform_get_drvdata(pdev
);
346 clk_unprepare(cmdq
->clock
);
351 static int cmdq_mbox_send_data(struct mbox_chan
*chan
, void *data
)
353 struct cmdq_pkt
*pkt
= (struct cmdq_pkt
*)data
;
354 struct cmdq_thread
*thread
= (struct cmdq_thread
*)chan
->con_priv
;
355 struct cmdq
*cmdq
= dev_get_drvdata(chan
->mbox
->dev
);
356 struct cmdq_task
*task
;
357 unsigned long curr_pa
, end_pa
;
359 /* Client should not flush new tasks if suspended. */
360 WARN_ON(cmdq
->suspended
);
362 task
= kzalloc(sizeof(*task
), GFP_ATOMIC
);
367 INIT_LIST_HEAD(&task
->list_entry
);
368 task
->pa_base
= pkt
->pa_base
;
369 task
->thread
= thread
;
372 if (list_empty(&thread
->task_busy_list
)) {
373 WARN_ON(clk_enable(cmdq
->clock
) < 0);
374 WARN_ON(cmdq_thread_reset(cmdq
, thread
) < 0);
376 writel(task
->pa_base
, thread
->base
+ CMDQ_THR_CURR_ADDR
);
377 writel(task
->pa_base
+ pkt
->cmd_buf_size
,
378 thread
->base
+ CMDQ_THR_END_ADDR
);
379 writel(thread
->priority
, thread
->base
+ CMDQ_THR_PRIORITY
);
380 writel(CMDQ_THR_IRQ_EN
, thread
->base
+ CMDQ_THR_IRQ_ENABLE
);
381 writel(CMDQ_THR_ENABLED
, thread
->base
+ CMDQ_THR_ENABLE_TASK
);
383 WARN_ON(cmdq_thread_suspend(cmdq
, thread
) < 0);
384 curr_pa
= readl(thread
->base
+ CMDQ_THR_CURR_ADDR
);
385 end_pa
= readl(thread
->base
+ CMDQ_THR_END_ADDR
);
388 * Atomic execution should remove the following wfe, i.e. only
389 * wait event at first task, and prevent to pause when running.
391 if (thread
->atomic_exec
) {
392 /* GCE is executing if command is not WFE */
393 if (!cmdq_thread_is_in_wfe(thread
)) {
394 cmdq_thread_resume(thread
);
395 cmdq_thread_wait_end(thread
, end_pa
);
396 WARN_ON(cmdq_thread_suspend(cmdq
, thread
) < 0);
397 /* set to this task directly */
398 writel(task
->pa_base
,
399 thread
->base
+ CMDQ_THR_CURR_ADDR
);
401 cmdq_task_insert_into_thread(task
);
402 cmdq_task_remove_wfe(task
);
403 smp_mb(); /* modify jump before enable thread */
407 if (curr_pa
== end_pa
- CMDQ_INST_SIZE
||
409 /* set to this task directly */
410 writel(task
->pa_base
,
411 thread
->base
+ CMDQ_THR_CURR_ADDR
);
413 cmdq_task_insert_into_thread(task
);
414 smp_mb(); /* modify jump before enable thread */
417 writel(task
->pa_base
+ pkt
->cmd_buf_size
,
418 thread
->base
+ CMDQ_THR_END_ADDR
);
419 cmdq_thread_resume(thread
);
421 list_move_tail(&task
->list_entry
, &thread
->task_busy_list
);
426 static int cmdq_mbox_startup(struct mbox_chan
*chan
)
431 static void cmdq_mbox_shutdown(struct mbox_chan
*chan
)
435 static const struct mbox_chan_ops cmdq_mbox_chan_ops
= {
436 .send_data
= cmdq_mbox_send_data
,
437 .startup
= cmdq_mbox_startup
,
438 .shutdown
= cmdq_mbox_shutdown
,
441 static struct mbox_chan
*cmdq_xlate(struct mbox_controller
*mbox
,
442 const struct of_phandle_args
*sp
)
444 int ind
= sp
->args
[0];
445 struct cmdq_thread
*thread
;
447 if (ind
>= mbox
->num_chans
)
448 return ERR_PTR(-EINVAL
);
450 thread
= (struct cmdq_thread
*)mbox
->chans
[ind
].con_priv
;
451 thread
->priority
= sp
->args
[1];
452 thread
->atomic_exec
= (sp
->args
[2] != 0);
453 thread
->chan
= &mbox
->chans
[ind
];
455 return &mbox
->chans
[ind
];
458 static int cmdq_probe(struct platform_device
*pdev
)
460 struct device
*dev
= &pdev
->dev
;
461 struct resource
*res
;
465 cmdq
= devm_kzalloc(dev
, sizeof(*cmdq
), GFP_KERNEL
);
469 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
470 cmdq
->base
= devm_ioremap_resource(dev
, res
);
471 if (IS_ERR(cmdq
->base
)) {
472 dev_err(dev
, "failed to ioremap gce\n");
473 return PTR_ERR(cmdq
->base
);
476 cmdq
->irq
= platform_get_irq(pdev
, 0);
478 dev_err(dev
, "failed to get irq\n");
482 cmdq
->thread_nr
= (u32
)(unsigned long)of_device_get_match_data(dev
);
483 cmdq
->irq_mask
= GENMASK(cmdq
->thread_nr
- 1, 0);
484 err
= devm_request_irq(dev
, cmdq
->irq
, cmdq_irq_handler
, IRQF_SHARED
,
487 dev_err(dev
, "failed to register ISR (%d)\n", err
);
491 dev_dbg(dev
, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
492 dev
, cmdq
->base
, cmdq
->irq
);
494 cmdq
->clock
= devm_clk_get(dev
, "gce");
495 if (IS_ERR(cmdq
->clock
)) {
496 dev_err(dev
, "failed to get gce clk\n");
497 return PTR_ERR(cmdq
->clock
);
500 cmdq
->mbox
.dev
= dev
;
501 cmdq
->mbox
.chans
= devm_kcalloc(dev
, cmdq
->thread_nr
,
502 sizeof(*cmdq
->mbox
.chans
), GFP_KERNEL
);
503 if (!cmdq
->mbox
.chans
)
506 cmdq
->mbox
.num_chans
= cmdq
->thread_nr
;
507 cmdq
->mbox
.ops
= &cmdq_mbox_chan_ops
;
508 cmdq
->mbox
.of_xlate
= cmdq_xlate
;
510 /* make use of TXDONE_BY_ACK */
511 cmdq
->mbox
.txdone_irq
= false;
512 cmdq
->mbox
.txdone_poll
= false;
514 cmdq
->thread
= devm_kcalloc(dev
, cmdq
->thread_nr
,
515 sizeof(*cmdq
->thread
), GFP_KERNEL
);
519 for (i
= 0; i
< cmdq
->thread_nr
; i
++) {
520 cmdq
->thread
[i
].base
= cmdq
->base
+ CMDQ_THR_BASE
+
522 INIT_LIST_HEAD(&cmdq
->thread
[i
].task_busy_list
);
523 cmdq
->mbox
.chans
[i
].con_priv
= (void *)&cmdq
->thread
[i
];
526 err
= devm_mbox_controller_register(dev
, &cmdq
->mbox
);
528 dev_err(dev
, "failed to register mailbox: %d\n", err
);
532 platform_set_drvdata(pdev
, cmdq
);
533 WARN_ON(clk_prepare(cmdq
->clock
) < 0);
540 static const struct dev_pm_ops cmdq_pm_ops
= {
541 .suspend
= cmdq_suspend
,
542 .resume
= cmdq_resume
,
545 static const struct of_device_id cmdq_of_ids
[] = {
546 {.compatible
= "mediatek,mt8173-gce", .data
= (void *)16},
547 {.compatible
= "mediatek,mt8183-gce", .data
= (void *)24},
551 static struct platform_driver cmdq_drv
= {
553 .remove
= cmdq_remove
,
557 .of_match_table
= cmdq_of_ids
,
561 static int __init
cmdq_drv_init(void)
563 return platform_driver_register(&cmdq_drv
);
566 static void __exit
cmdq_drv_exit(void)
568 platform_driver_unregister(&cmdq_drv
);
571 subsys_initcall(cmdq_drv_init
);
572 module_exit(cmdq_drv_exit
);
574 MODULE_LICENSE("GPL v2");