blob: bfc5d0881f71f3a9c34d00d2090b8c8265198f6c [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2018 MediaTek Inc.
4
5#include <linux/bitops.h>
6#include <linux/clk.h>
7#include <linux/clk-provider.h>
8#include <linux/dma-mapping.h>
9#include <linux/errno.h>
10#include <linux/interrupt.h>
11#include <linux/iopoll.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/mailbox_controller.h>
16#include <linux/mailbox/mtk-cmdq-mailbox.h>
17#include <linux/of_device.h>
18
19#define CMDQ_OP_CODE_MASK (0xff << CMDQ_OP_CODE_SHIFT)
20#define CMDQ_NUM_CMD(t) (t->cmd_buf_size / CMDQ_INST_SIZE)
21
22#define CMDQ_CURR_IRQ_STATUS 0x10
23#define CMDQ_THR_SLOT_CYCLES 0x30
24#define CMDQ_THR_BASE 0x100
25#define CMDQ_THR_SIZE 0x80
26#define CMDQ_THR_WARM_RESET 0x00
27#define CMDQ_THR_ENABLE_TASK 0x04
28#define CMDQ_THR_SUSPEND_TASK 0x08
29#define CMDQ_THR_CURR_STATUS 0x0c
30#define CMDQ_THR_IRQ_STATUS 0x10
31#define CMDQ_THR_IRQ_ENABLE 0x14
32#define CMDQ_THR_CURR_ADDR 0x20
33#define CMDQ_THR_END_ADDR 0x24
34#define CMDQ_THR_WAIT_TOKEN 0x30
35#define CMDQ_THR_PRIORITY 0x40
36#define CMDQ_SYNC_TOKEN_UPDATE 0x68
37
38#define CMDQ_THR_ACTIVE_SLOT_CYCLES 0x3200
39#define CMDQ_THR_ENABLED 0x1
40#define CMDQ_THR_DISABLED 0x0
41#define CMDQ_THR_SUSPEND 0x1
42#define CMDQ_THR_RESUME 0x0
43#define CMDQ_THR_STATUS_SUSPENDED BIT(1)
44#define CMDQ_THR_DO_WARM_RESET BIT(0)
45#define CMDQ_THR_IRQ_DONE 0x1
46#define CMDQ_THR_IRQ_ERROR 0x12
47#define CMDQ_THR_IRQ_EN (CMDQ_THR_IRQ_ERROR | CMDQ_THR_IRQ_DONE)
48#define CMDQ_THR_IS_WAITING BIT(31)
49
50#define CMDQ_JUMP_BY_OFFSET 0x10000000
51#define CMDQ_JUMP_BY_PA 0x10000001
52
53struct cmdq_thread {
54 struct mbox_chan *chan;
55 void __iomem *base;
56 struct list_head task_busy_list;
57 u32 priority;
58 bool atomic_exec;
59};
60
61struct cmdq_task {
62 struct cmdq *cmdq;
63 struct list_head list_entry;
64 dma_addr_t pa_base;
65 struct cmdq_thread *thread;
66 struct cmdq_pkt *pkt; /* the packet sent from mailbox client */
67};
68
69struct cmdq {
70 struct mbox_controller mbox;
71 void __iomem *base;
72 u32 irq;
73 u32 thread_nr;
74 u32 irq_mask;
75 struct cmdq_thread *thread;
76 struct clk *clock;
77 struct clk *clock_timer;
78 bool suspended;
79};
80
81static int cmdq_thread_suspend(struct cmdq *cmdq, struct cmdq_thread *thread)
82{
83 u32 status;
84
85 writel(CMDQ_THR_SUSPEND, thread->base + CMDQ_THR_SUSPEND_TASK);
86
87 /* If already disabled, treat as suspended successful. */
88 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
89 return 0;
90
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));
95 return -EFAULT;
96 }
97
98 return 0;
99}
100
101static void cmdq_thread_resume(struct cmdq_thread *thread)
102{
103 writel(CMDQ_THR_RESUME, thread->base + CMDQ_THR_SUSPEND_TASK);
104}
105
106static void cmdq_init(struct cmdq *cmdq)
107{
108 int i;
109
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);
115}
116
117static int cmdq_thread_reset(struct cmdq *cmdq, struct cmdq_thread *thread)
118{
119 u32 warm_reset;
120
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),
124 0, 10)) {
125 dev_err(cmdq->mbox.dev, "reset GCE thread 0x%x failed\n",
126 (u32)(thread->base - cmdq->base));
127 return -EFAULT;
128 }
129
130 return 0;
131}
132
133static void cmdq_thread_disable(struct cmdq *cmdq, struct cmdq_thread *thread)
134{
135 cmdq_thread_reset(cmdq, thread);
136 writel(CMDQ_THR_DISABLED, thread->base + CMDQ_THR_ENABLE_TASK);
137}
138
139/* notify GCE to re-fetch commands by setting GCE thread PC */
140static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread *thread)
141{
142 writel(readl(thread->base + CMDQ_THR_CURR_ADDR),
143 thread->base + CMDQ_THR_CURR_ADDR);
144}
145
146static void cmdq_task_insert_into_thread(struct cmdq_task *task)
147{
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;
153
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);
161
162 cmdq_thread_invalidate_fetched_data(thread);
163}
164
165static bool cmdq_command_is_wfe(u64 cmd)
166{
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;
170
171 return ((cmd & wfe_mask) == (wfe_op | wfe_option));
172}
173
174/* we assume tasks in the same display GCE thread are waiting the same event. */
175static void cmdq_task_remove_wfe(struct cmdq_task *task)
176{
177 struct device *dev = task->cmdq->mbox.dev;
178 u64 *base = task->pkt->va_base;
179 int i;
180
181 dma_sync_single_for_cpu(dev, task->pa_base, task->pkt->cmd_buf_size,
182 DMA_TO_DEVICE);
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 |
186 CMDQ_JUMP_PASS;
187 dma_sync_single_for_device(dev, task->pa_base, task->pkt->cmd_buf_size,
188 DMA_TO_DEVICE);
189}
190
191static bool cmdq_thread_is_in_wfe(struct cmdq_thread *thread)
192{
193 return readl(thread->base + CMDQ_THR_WAIT_TOKEN) & CMDQ_THR_IS_WAITING;
194}
195
196static void cmdq_thread_wait_end(struct cmdq_thread *thread,
197 unsigned long end_pa)
198{
199 struct device *dev = thread->chan->mbox->dev;
200 unsigned long curr_pa;
201
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");
205}
206
207static void cmdq_task_exec_done(struct cmdq_task *task, enum cmdq_cb_status sta)
208{
209 struct cmdq_task_cb *cb = &task->pkt->async_cb;
210 struct cmdq_cb_data data;
211
212 WARN_ON(cb->cb == (cmdq_async_flush_cb)NULL);
213 data.sta = sta;
214 data.data = cb->data;
215 cb->cb(data);
216
217 list_del(&task->list_entry);
218}
219
220static void cmdq_task_handle_error(struct cmdq_task *task)
221{
222 struct cmdq_thread *thread = task->thread;
223 struct cmdq_task *next_task;
224
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);
229 if (next_task)
230 writel(next_task->pa_base, thread->base + CMDQ_THR_CURR_ADDR);
231 cmdq_thread_resume(thread);
232}
233
234static void cmdq_thread_irq_handler(struct cmdq *cmdq,
235 struct cmdq_thread *thread)
236{
237 struct cmdq_task *task, *tmp, *curr_task = NULL;
238 u32 curr_pa, irq_flag, task_end_pa;
239 bool err;
240
241 irq_flag = readl(thread->base + CMDQ_THR_IRQ_STATUS);
242 writel(~irq_flag, thread->base + CMDQ_THR_IRQ_STATUS);
243
244 /*
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.
249 */
250 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
251 return;
252
253 if (irq_flag & CMDQ_THR_IRQ_ERROR)
254 err = true;
255 else if (irq_flag & CMDQ_THR_IRQ_DONE)
256 err = false;
257 else
258 return;
259
260 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR);
261
262 if (list_empty(&thread->task_busy_list)) {
263 pr_err("%s, irq_flag=%x, pc=%x\n", __func__, irq_flag, curr_pa);
264 return;
265 }
266
267 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
268 list_entry) {
269 task_end_pa = task->pa_base + task->pkt->cmd_buf_size;
270 if (curr_pa >= task->pa_base && curr_pa < task_end_pa)
271 curr_task = task;
272
273 if (!curr_task || curr_pa == task_end_pa - CMDQ_INST_SIZE) {
274 cmdq_task_exec_done(task, CMDQ_CB_NORMAL);
275 kfree(task);
276 } else if (err) {
277 cmdq_task_exec_done(task, CMDQ_CB_ERROR);
278 cmdq_task_handle_error(curr_task);
279 kfree(task);
280 }
281
282 if (curr_task)
283 break;
284 }
285
286 if (list_empty(&thread->task_busy_list)) {
287 cmdq_thread_disable(cmdq, thread);
288 clk_disable(cmdq->clock);
289 }
290}
291
292static irqreturn_t cmdq_irq_handler(int irq, void *dev)
293{
294 struct cmdq *cmdq = dev;
295 unsigned long irq_status, flags = 0L;
296 int bit;
297
298 irq_status = readl(cmdq->base + CMDQ_CURR_IRQ_STATUS) & cmdq->irq_mask;
299 if (!(irq_status ^ cmdq->irq_mask))
300 return IRQ_NONE;
301
302 for_each_clear_bit(bit, &irq_status, cmdq->thread_nr) {
303 struct cmdq_thread *thread = &cmdq->thread[bit];
304
305 if (thread->chan)
306 spin_lock_irqsave(&thread->chan->lock, flags);
307 cmdq_thread_irq_handler(cmdq, thread);
308 if (thread->chan)
309 spin_unlock_irqrestore(&thread->chan->lock, flags);
310 }
311
312 return IRQ_HANDLED;
313}
314
315static int cmdq_suspend(struct device *dev)
316{
317 struct cmdq *cmdq = dev_get_drvdata(dev);
318 struct cmdq_thread *thread;
319 int i;
320 bool task_running = false;
321
322 cmdq->suspended = true;
323
324 for (i = 0; i < cmdq->thread_nr; i++) {
325 thread = &cmdq->thread[i];
326 if (!list_empty(&thread->task_busy_list)) {
327 task_running = true;
328 break;
329 }
330 }
331
332 if (task_running)
333 dev_warn(dev, "exist running task(s) in suspend\n");
334
335 /*
336 * clk_disable_unprepare(cmdq->clock_timer);
337 * clk_disable_unprepare(cmdq->clock);
338 */
339
340 return 0;
341}
342
343static int cmdq_resume(struct device *dev)
344{
345 struct cmdq *cmdq = dev_get_drvdata(dev);
346
347 /*
348 * WARN_ON(clk_prepare_enable(cmdq->clock) < 0);
349 * WARN_ON(clk_prepare_enable(cmdq->clock_timer) < 0);
350 */
351 cmdq->suspended = false;
352 return 0;
353}
354
355static int cmdq_remove(struct platform_device *pdev)
356{
357 struct cmdq *cmdq = platform_get_drvdata(pdev);
358
359 clk_disable_unprepare(cmdq->clock_timer);
360 clk_disable_unprepare(cmdq->clock);
361
362 return 0;
363}
364
365static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
366{
367 struct cmdq_pkt *pkt = (struct cmdq_pkt *)data;
368 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
369 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
370 struct cmdq_task *task;
371 unsigned long curr_pa, end_pa;
372
373 /* Client should not flush new tasks if suspended. */
374 WARN_ON(cmdq->suspended);
375
376 task = kzalloc(sizeof(*task), GFP_ATOMIC);
377 if (!task)
378 return -ENOMEM;
379
380 task->cmdq = cmdq;
381 INIT_LIST_HEAD(&task->list_entry);
382 task->pa_base = pkt->pa_base;
383 task->thread = thread;
384 task->pkt = pkt;
385
386 if (list_empty(&thread->task_busy_list)) {
387 WARN_ON(clk_enable(cmdq->clock) < 0);
388 WARN_ON(cmdq_thread_reset(cmdq, thread) < 0);
389
390 writel(task->pa_base, thread->base + CMDQ_THR_CURR_ADDR);
391 writel(task->pa_base + pkt->cmd_buf_size,
392 thread->base + CMDQ_THR_END_ADDR);
393 writel(thread->priority, thread->base + CMDQ_THR_PRIORITY);
394 writel(CMDQ_THR_IRQ_EN, thread->base + CMDQ_THR_IRQ_ENABLE);
395 writel(CMDQ_THR_ENABLED, thread->base + CMDQ_THR_ENABLE_TASK);
396 } else {
397 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
398 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR);
399 end_pa = readl(thread->base + CMDQ_THR_END_ADDR);
400
401 /*
402 * Atomic execution should remove the following wfe, i.e. only
403 * wait event at first task, and prevent to pause when running.
404 */
405 if (thread->atomic_exec) {
406 /* GCE is executing if command is not WFE */
407 if (!cmdq_thread_is_in_wfe(thread)) {
408 cmdq_thread_resume(thread);
409 cmdq_thread_wait_end(thread, end_pa);
410 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
411 /* set to this task directly */
412 writel(task->pa_base,
413 thread->base + CMDQ_THR_CURR_ADDR);
414 } else {
415 cmdq_task_insert_into_thread(task);
416 cmdq_task_remove_wfe(task);
417 smp_mb(); /* modify jump before enable thread */
418 }
419 } else {
420 /* check boundary */
421 if (curr_pa == end_pa - CMDQ_INST_SIZE ||
422 curr_pa == end_pa) {
423 /* set to this task directly */
424 writel(task->pa_base,
425 thread->base + CMDQ_THR_CURR_ADDR);
426 } else {
427 cmdq_task_insert_into_thread(task);
428 smp_mb(); /* modify jump before enable thread */
429 }
430 }
431 writel(task->pa_base + pkt->cmd_buf_size,
432 thread->base + CMDQ_THR_END_ADDR);
433 cmdq_thread_resume(thread);
434 }
435 list_move_tail(&task->list_entry, &thread->task_busy_list);
436
437 return 0;
438}
439
440static int cmdq_mbox_startup(struct mbox_chan *chan)
441{
442 return 0;
443}
444
445static void cmdq_mbox_shutdown(struct mbox_chan *chan)
446{
447}
448
449static const struct mbox_chan_ops cmdq_mbox_chan_ops = {
450 .send_data = cmdq_mbox_send_data,
451 .startup = cmdq_mbox_startup,
452 .shutdown = cmdq_mbox_shutdown,
453};
454
455static struct mbox_chan *cmdq_xlate(struct mbox_controller *mbox,
456 const struct of_phandle_args *sp)
457{
458 int ind = sp->args[0];
459 struct cmdq_thread *thread;
460
461 if (ind >= mbox->num_chans)
462 return ERR_PTR(-EINVAL);
463
464 thread = (struct cmdq_thread *)mbox->chans[ind].con_priv;
465 thread->priority = sp->args[1];
466 thread->atomic_exec = (sp->args[2] != 0);
467 thread->chan = &mbox->chans[ind];
468
469 return &mbox->chans[ind];
470}
471
472static int cmdq_probe(struct platform_device *pdev)
473{
474 struct device *dev = &pdev->dev;
475 struct resource *res;
476 struct cmdq *cmdq;
477 int err, i;
478
479 cmdq = devm_kzalloc(dev, sizeof(*cmdq), GFP_KERNEL);
480 if (!cmdq)
481 return -ENOMEM;
482
483 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
484 cmdq->base = devm_ioremap_resource(dev, res);
485 if (IS_ERR(cmdq->base)) {
486 dev_err(dev, "failed to ioremap gce\n");
487 return PTR_ERR(cmdq->base);
488 }
489
490 cmdq->clock = devm_clk_get(dev, "gce");
491 if (IS_ERR(cmdq->clock)) {
492 dev_err(dev, "failed to get gce clk\n");
493 return PTR_ERR(cmdq->clock);
494 }
495 cmdq->clock_timer = devm_clk_get(dev, "gce_26m");
496 if (IS_ERR(cmdq->clock_timer)) {
497 dev_err(dev, "failed to get gce timer clk\n");
498 cmdq->clock_timer = NULL;
499 }
500
501 cmdq->thread_nr = (u32)(unsigned long)of_device_get_match_data(dev);
502 cmdq->irq_mask = GENMASK(cmdq->thread_nr - 1, 0);
503 cmdq->mbox.dev = dev;
504 cmdq->mbox.chans = devm_kcalloc(dev, cmdq->thread_nr,
505 sizeof(*cmdq->mbox.chans), GFP_KERNEL);
506 if (!cmdq->mbox.chans)
507 return -ENOMEM;
508
509 cmdq->mbox.num_chans = cmdq->thread_nr;
510 cmdq->mbox.ops = &cmdq_mbox_chan_ops;
511 cmdq->mbox.of_xlate = cmdq_xlate;
512
513 /* make use of TXDONE_BY_ACK */
514 cmdq->mbox.txdone_irq = false;
515 cmdq->mbox.txdone_poll = false;
516
517 cmdq->thread = devm_kcalloc(dev, cmdq->thread_nr,
518 sizeof(*cmdq->thread), GFP_KERNEL);
519 if (!cmdq->thread)
520 return -ENOMEM;
521
522 for (i = 0; i < cmdq->thread_nr; i++) {
523 cmdq->thread[i].base = cmdq->base + CMDQ_THR_BASE +
524 CMDQ_THR_SIZE * i;
525 INIT_LIST_HEAD(&cmdq->thread[i].task_busy_list);
526 cmdq->mbox.chans[i].con_priv = (void *)&cmdq->thread[i];
527 }
528
529 err = devm_mbox_controller_register(dev, &cmdq->mbox);
530 if (err < 0) {
531 dev_err(dev, "failed to register mailbox: %d\n", err);
532 return err;
533 }
534
535 cmdq->irq = platform_get_irq(pdev, 0);
536 if (!cmdq->irq) {
537 dev_err(dev, "failed to get irq\n");
538 return -EINVAL;
539 }
540
541 err = devm_request_irq(dev, cmdq->irq, cmdq_irq_handler, IRQF_SHARED,
542 "mtk_cmdq", cmdq);
543 if (err < 0) {
544 dev_err(dev, "failed to register ISR (%d)\n", err);
545 return err;
546 }
547
548 dev_dbg(dev, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
549 dev, cmdq->base, cmdq->irq);
550
551 platform_set_drvdata(pdev, cmdq);
552 WARN_ON(clk_prepare_enable(cmdq->clock) < 0);
553 WARN_ON(clk_prepare_enable(cmdq->clock_timer) < 0);
554
555 cmdq_init(cmdq);
556
557 return 0;
558}
559
560static const struct dev_pm_ops cmdq_pm_ops = {
561 .suspend = cmdq_suspend,
562 .resume = cmdq_resume,
563};
564
565static const struct of_device_id cmdq_of_ids[] = {
566 {.compatible = "mediatek,mt8173-gce", .data = (void *)16},
567 {}
568};
569
570static struct platform_driver cmdq_drv = {
571 .probe = cmdq_probe,
572 .remove = cmdq_remove,
573 .driver = {
574 .name = "mtk_cmdq",
575 .pm = &cmdq_pm_ops,
576 .of_match_table = cmdq_of_ids,
577 }
578};
579
580static int __init cmdq_drv_init(void)
581{
582 return platform_driver_register(&cmdq_drv);
583}
584
585static void __exit cmdq_drv_exit(void)
586{
587 platform_driver_unregister(&cmdq_drv);
588}
589
590subsys_initcall(cmdq_drv_init);
591module_exit(cmdq_drv_exit);
592
593MODULE_LICENSE("GPL v2");