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>
20 #define CMDQ_OP_CODE_MASK (0xff << CMDQ_OP_CODE_SHIFT)
21 #define CMDQ_NUM_CMD(t) (t->cmd_buf_size / CMDQ_INST_SIZE)
22 #define CMDQ_GCE_NUM_MAX (2)
24 #define CMDQ_CURR_IRQ_STATUS 0x10
25 #define CMDQ_SYNC_TOKEN_UPDATE 0x68
26 #define CMDQ_THR_SLOT_CYCLES 0x30
27 #define CMDQ_THR_BASE 0x100
28 #define CMDQ_THR_SIZE 0x80
29 #define CMDQ_THR_WARM_RESET 0x00
30 #define CMDQ_THR_ENABLE_TASK 0x04
31 #define CMDQ_THR_SUSPEND_TASK 0x08
32 #define CMDQ_THR_CURR_STATUS 0x0c
33 #define CMDQ_THR_IRQ_STATUS 0x10
34 #define CMDQ_THR_IRQ_ENABLE 0x14
35 #define CMDQ_THR_CURR_ADDR 0x20
36 #define CMDQ_THR_END_ADDR 0x24
37 #define CMDQ_THR_WAIT_TOKEN 0x30
38 #define CMDQ_THR_PRIORITY 0x40
40 #define GCE_GCTL_VALUE 0x48
41 #define GCE_CTRL_BY_SW GENMASK(2, 0)
42 #define GCE_DDR_EN GENMASK(18, 16)
44 #define CMDQ_THR_ACTIVE_SLOT_CYCLES 0x3200
45 #define CMDQ_THR_ENABLED 0x1
46 #define CMDQ_THR_DISABLED 0x0
47 #define CMDQ_THR_SUSPEND 0x1
48 #define CMDQ_THR_RESUME 0x0
49 #define CMDQ_THR_STATUS_SUSPENDED BIT(1)
50 #define CMDQ_THR_DO_WARM_RESET BIT(0)
51 #define CMDQ_THR_IRQ_DONE 0x1
52 #define CMDQ_THR_IRQ_ERROR 0x12
53 #define CMDQ_THR_IRQ_EN (CMDQ_THR_IRQ_ERROR | CMDQ_THR_IRQ_DONE)
54 #define CMDQ_THR_IS_WAITING BIT(31)
56 #define CMDQ_JUMP_BY_OFFSET 0x10000000
57 #define CMDQ_JUMP_BY_PA 0x10000001
60 struct mbox_chan *chan;
62 struct list_head task_busy_list;
68 struct list_head list_entry;
70 struct cmdq_thread *thread;
71 struct cmdq_pkt *pkt; /* the packet sent from mailbox client */
75 struct mbox_controller mbox;
79 const struct gce_plat *pdata;
80 struct cmdq_thread *thread;
81 struct clk_bulk_data clocks[CMDQ_GCE_NUM_MAX];
93 static void cmdq_sw_ddr_enable(struct cmdq *cmdq, bool enable)
95 WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
98 writel(GCE_DDR_EN | GCE_CTRL_BY_SW, cmdq->base + GCE_GCTL_VALUE);
100 writel(GCE_CTRL_BY_SW, cmdq->base + GCE_GCTL_VALUE);
102 clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
105 u8 cmdq_get_shift_pa(struct mbox_chan *chan)
107 struct cmdq *cmdq = container_of(chan->mbox, struct cmdq, mbox);
109 return cmdq->pdata->shift;
111 EXPORT_SYMBOL(cmdq_get_shift_pa);
113 static int cmdq_thread_suspend(struct cmdq *cmdq, struct cmdq_thread *thread)
117 writel(CMDQ_THR_SUSPEND, thread->base + CMDQ_THR_SUSPEND_TASK);
119 /* If already disabled, treat as suspended successful. */
120 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
123 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_CURR_STATUS,
124 status, status & CMDQ_THR_STATUS_SUSPENDED, 0, 10)) {
125 dev_err(cmdq->mbox.dev, "suspend GCE thread 0x%x failed\n",
126 (u32)(thread->base - cmdq->base));
133 static void cmdq_thread_resume(struct cmdq_thread *thread)
135 writel(CMDQ_THR_RESUME, thread->base + CMDQ_THR_SUSPEND_TASK);
138 static void cmdq_init(struct cmdq *cmdq)
143 WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
144 if (cmdq->pdata->control_by_sw)
145 gctl_regval = GCE_CTRL_BY_SW;
146 if (cmdq->pdata->sw_ddr_en)
147 gctl_regval |= GCE_DDR_EN;
150 writel(gctl_regval, cmdq->base + GCE_GCTL_VALUE);
152 writel(CMDQ_THR_ACTIVE_SLOT_CYCLES, cmdq->base + CMDQ_THR_SLOT_CYCLES);
153 for (i = 0; i <= CMDQ_MAX_EVENT; i++)
154 writel(i, cmdq->base + CMDQ_SYNC_TOKEN_UPDATE);
155 clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
158 static int cmdq_thread_reset(struct cmdq *cmdq, struct cmdq_thread *thread)
162 writel(CMDQ_THR_DO_WARM_RESET, thread->base + CMDQ_THR_WARM_RESET);
163 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_WARM_RESET,
164 warm_reset, !(warm_reset & CMDQ_THR_DO_WARM_RESET),
166 dev_err(cmdq->mbox.dev, "reset GCE thread 0x%x failed\n",
167 (u32)(thread->base - cmdq->base));
174 static void cmdq_thread_disable(struct cmdq *cmdq, struct cmdq_thread *thread)
176 cmdq_thread_reset(cmdq, thread);
177 writel(CMDQ_THR_DISABLED, thread->base + CMDQ_THR_ENABLE_TASK);
180 /* notify GCE to re-fetch commands by setting GCE thread PC */
181 static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread *thread)
183 writel(readl(thread->base + CMDQ_THR_CURR_ADDR),
184 thread->base + CMDQ_THR_CURR_ADDR);
187 static void cmdq_task_insert_into_thread(struct cmdq_task *task)
189 struct device *dev = task->cmdq->mbox.dev;
190 struct cmdq_thread *thread = task->thread;
191 struct cmdq_task *prev_task = list_last_entry(
192 &thread->task_busy_list, typeof(*task), list_entry);
193 u64 *prev_task_base = prev_task->pkt->va_base;
195 /* let previous task jump to this task */
196 dma_sync_single_for_cpu(dev, prev_task->pa_base,
197 prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
198 prev_task_base[CMDQ_NUM_CMD(prev_task->pkt) - 1] =
199 (u64)CMDQ_JUMP_BY_PA << 32 |
200 (task->pa_base >> task->cmdq->pdata->shift);
201 dma_sync_single_for_device(dev, prev_task->pa_base,
202 prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
204 cmdq_thread_invalidate_fetched_data(thread);
207 static bool cmdq_thread_is_in_wfe(struct cmdq_thread *thread)
209 return readl(thread->base + CMDQ_THR_WAIT_TOKEN) & CMDQ_THR_IS_WAITING;
212 static void cmdq_task_exec_done(struct cmdq_task *task, int sta)
214 struct cmdq_cb_data data;
217 data.pkt = task->pkt;
218 mbox_chan_received_data(task->thread->chan, &data);
220 list_del(&task->list_entry);
223 static void cmdq_task_handle_error(struct cmdq_task *task)
225 struct cmdq_thread *thread = task->thread;
226 struct cmdq_task *next_task;
227 struct cmdq *cmdq = task->cmdq;
229 dev_err(cmdq->mbox.dev, "task 0x%p error\n", task);
230 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
231 next_task = list_first_entry_or_null(&thread->task_busy_list,
232 struct cmdq_task, list_entry);
234 writel(next_task->pa_base >> cmdq->pdata->shift,
235 thread->base + CMDQ_THR_CURR_ADDR);
236 cmdq_thread_resume(thread);
239 static void cmdq_thread_irq_handler(struct cmdq *cmdq,
240 struct cmdq_thread *thread)
242 struct cmdq_task *task, *tmp, *curr_task = NULL;
243 u32 curr_pa, irq_flag, task_end_pa;
246 irq_flag = readl(thread->base + CMDQ_THR_IRQ_STATUS);
247 writel(~irq_flag, thread->base + CMDQ_THR_IRQ_STATUS);
250 * When ISR call this function, another CPU core could run
251 * "release task" right before we acquire the spin lock, and thus
252 * reset / disable this GCE thread, so we need to check the enable
253 * bit of this GCE thread.
255 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
258 if (irq_flag & CMDQ_THR_IRQ_ERROR)
260 else if (irq_flag & CMDQ_THR_IRQ_DONE)
265 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) << cmdq->pdata->shift;
267 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
269 task_end_pa = task->pa_base + task->pkt->cmd_buf_size;
270 if (curr_pa >= task->pa_base && curr_pa < task_end_pa)
273 if (!curr_task || curr_pa == task_end_pa - CMDQ_INST_SIZE) {
274 cmdq_task_exec_done(task, 0);
277 cmdq_task_exec_done(task, -ENOEXEC);
278 cmdq_task_handle_error(curr_task);
286 if (list_empty(&thread->task_busy_list)) {
287 cmdq_thread_disable(cmdq, thread);
288 clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
292 static irqreturn_t cmdq_irq_handler(int irq, void *dev)
294 struct cmdq *cmdq = dev;
295 unsigned long irq_status, flags = 0L;
298 irq_status = readl(cmdq->base + CMDQ_CURR_IRQ_STATUS) & cmdq->irq_mask;
299 if (!(irq_status ^ cmdq->irq_mask))
302 for_each_clear_bit(bit, &irq_status, cmdq->pdata->thread_nr) {
303 struct cmdq_thread *thread = &cmdq->thread[bit];
305 spin_lock_irqsave(&thread->chan->lock, flags);
306 cmdq_thread_irq_handler(cmdq, thread);
307 spin_unlock_irqrestore(&thread->chan->lock, flags);
313 static int cmdq_suspend(struct device *dev)
315 struct cmdq *cmdq = dev_get_drvdata(dev);
316 struct cmdq_thread *thread;
318 bool task_running = false;
320 cmdq->suspended = true;
322 for (i = 0; i < cmdq->pdata->thread_nr; i++) {
323 thread = &cmdq->thread[i];
324 if (!list_empty(&thread->task_busy_list)) {
331 dev_warn(dev, "exist running task(s) in suspend\n");
333 if (cmdq->pdata->sw_ddr_en)
334 cmdq_sw_ddr_enable(cmdq, false);
336 clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);
341 static int cmdq_resume(struct device *dev)
343 struct cmdq *cmdq = dev_get_drvdata(dev);
345 WARN_ON(clk_bulk_prepare(cmdq->pdata->gce_num, cmdq->clocks));
346 cmdq->suspended = false;
348 if (cmdq->pdata->sw_ddr_en)
349 cmdq_sw_ddr_enable(cmdq, true);
354 static int cmdq_remove(struct platform_device *pdev)
356 struct cmdq *cmdq = platform_get_drvdata(pdev);
358 if (cmdq->pdata->sw_ddr_en)
359 cmdq_sw_ddr_enable(cmdq, false);
361 clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);
365 static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
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;
373 /* Client should not flush new tasks if suspended. */
374 WARN_ON(cmdq->suspended);
376 task = kzalloc(sizeof(*task), GFP_ATOMIC);
381 INIT_LIST_HEAD(&task->list_entry);
382 task->pa_base = pkt->pa_base;
383 task->thread = thread;
386 if (list_empty(&thread->task_busy_list)) {
387 WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));
390 * The thread reset will clear thread related register to 0,
391 * including pc, end, priority, irq, suspend and enable. Thus
392 * set CMDQ_THR_ENABLED to CMDQ_THR_ENABLE_TASK will enable
393 * thread and make it running.
395 WARN_ON(cmdq_thread_reset(cmdq, thread) < 0);
397 writel(task->pa_base >> cmdq->pdata->shift,
398 thread->base + CMDQ_THR_CURR_ADDR);
399 writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->pdata->shift,
400 thread->base + CMDQ_THR_END_ADDR);
402 writel(thread->priority, thread->base + CMDQ_THR_PRIORITY);
403 writel(CMDQ_THR_IRQ_EN, thread->base + CMDQ_THR_IRQ_ENABLE);
404 writel(CMDQ_THR_ENABLED, thread->base + CMDQ_THR_ENABLE_TASK);
406 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
407 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) <<
409 end_pa = readl(thread->base + CMDQ_THR_END_ADDR) <<
412 if (curr_pa == end_pa - CMDQ_INST_SIZE ||
414 /* set to this task directly */
415 writel(task->pa_base >> cmdq->pdata->shift,
416 thread->base + CMDQ_THR_CURR_ADDR);
418 cmdq_task_insert_into_thread(task);
419 smp_mb(); /* modify jump before enable thread */
421 writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->pdata->shift,
422 thread->base + CMDQ_THR_END_ADDR);
423 cmdq_thread_resume(thread);
425 list_move_tail(&task->list_entry, &thread->task_busy_list);
430 static int cmdq_mbox_startup(struct mbox_chan *chan)
435 static void cmdq_mbox_shutdown(struct mbox_chan *chan)
437 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
438 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
439 struct cmdq_task *task, *tmp;
442 spin_lock_irqsave(&thread->chan->lock, flags);
443 if (list_empty(&thread->task_busy_list))
446 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
448 /* make sure executed tasks have success callback */
449 cmdq_thread_irq_handler(cmdq, thread);
450 if (list_empty(&thread->task_busy_list))
453 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
455 cmdq_task_exec_done(task, -ECONNABORTED);
459 cmdq_thread_disable(cmdq, thread);
460 clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
464 * The thread->task_busy_list empty means thread already disable. The
465 * cmdq_mbox_send_data() always reset thread which clear disable and
466 * suspend statue when first pkt send to channel, so there is no need
467 * to do any operation here, only unlock and leave.
469 spin_unlock_irqrestore(&thread->chan->lock, flags);
472 static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
474 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
475 struct cmdq_cb_data data;
476 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
477 struct cmdq_task *task, *tmp;
481 spin_lock_irqsave(&thread->chan->lock, flags);
482 if (list_empty(&thread->task_busy_list))
485 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
486 if (!cmdq_thread_is_in_wfe(thread))
489 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
491 data.sta = -ECONNABORTED;
492 data.pkt = task->pkt;
493 mbox_chan_received_data(task->thread->chan, &data);
494 list_del(&task->list_entry);
498 cmdq_thread_resume(thread);
499 cmdq_thread_disable(cmdq, thread);
500 clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
503 spin_unlock_irqrestore(&thread->chan->lock, flags);
507 cmdq_thread_resume(thread);
508 spin_unlock_irqrestore(&thread->chan->lock, flags);
509 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_ENABLE_TASK,
510 enable, enable == 0, 1, timeout)) {
511 dev_err(cmdq->mbox.dev, "Fail to wait GCE thread 0x%x done\n",
512 (u32)(thread->base - cmdq->base));
519 static const struct mbox_chan_ops cmdq_mbox_chan_ops = {
520 .send_data = cmdq_mbox_send_data,
521 .startup = cmdq_mbox_startup,
522 .shutdown = cmdq_mbox_shutdown,
523 .flush = cmdq_mbox_flush,
526 static struct mbox_chan *cmdq_xlate(struct mbox_controller *mbox,
527 const struct of_phandle_args *sp)
529 int ind = sp->args[0];
530 struct cmdq_thread *thread;
532 if (ind >= mbox->num_chans)
533 return ERR_PTR(-EINVAL);
535 thread = (struct cmdq_thread *)mbox->chans[ind].con_priv;
536 thread->priority = sp->args[1];
537 thread->chan = &mbox->chans[ind];
539 return &mbox->chans[ind];
542 static int cmdq_probe(struct platform_device *pdev)
544 struct device *dev = &pdev->dev;
547 struct device_node *phandle = dev->of_node;
548 struct device_node *node;
550 static const char * const clk_name = "gce";
551 static const char * const clk_names[] = { "gce0", "gce1" };
553 cmdq = devm_kzalloc(dev, sizeof(*cmdq), GFP_KERNEL);
557 cmdq->base = devm_platform_ioremap_resource(pdev, 0);
558 if (IS_ERR(cmdq->base))
559 return PTR_ERR(cmdq->base);
561 cmdq->irq = platform_get_irq(pdev, 0);
565 cmdq->pdata = device_get_match_data(dev);
567 dev_err(dev, "failed to get match data\n");
571 cmdq->irq_mask = GENMASK(cmdq->pdata->thread_nr - 1, 0);
573 dev_dbg(dev, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
574 dev, cmdq->base, cmdq->irq);
576 if (cmdq->pdata->gce_num > 1) {
577 for_each_child_of_node(phandle->parent, node) {
578 alias_id = of_alias_get_id(node, clk_name);
579 if (alias_id >= 0 && alias_id < cmdq->pdata->gce_num) {
580 cmdq->clocks[alias_id].id = clk_names[alias_id];
581 cmdq->clocks[alias_id].clk = of_clk_get(node, 0);
582 if (IS_ERR(cmdq->clocks[alias_id].clk)) {
584 return dev_err_probe(dev,
585 PTR_ERR(cmdq->clocks[alias_id].clk),
586 "failed to get gce clk: %d\n",
592 cmdq->clocks[alias_id].id = clk_name;
593 cmdq->clocks[alias_id].clk = devm_clk_get(&pdev->dev, clk_name);
594 if (IS_ERR(cmdq->clocks[alias_id].clk)) {
595 return dev_err_probe(dev, PTR_ERR(cmdq->clocks[alias_id].clk),
596 "failed to get gce clk\n");
600 cmdq->mbox.dev = dev;
601 cmdq->mbox.chans = devm_kcalloc(dev, cmdq->pdata->thread_nr,
602 sizeof(*cmdq->mbox.chans), GFP_KERNEL);
603 if (!cmdq->mbox.chans)
606 cmdq->mbox.num_chans = cmdq->pdata->thread_nr;
607 cmdq->mbox.ops = &cmdq_mbox_chan_ops;
608 cmdq->mbox.of_xlate = cmdq_xlate;
610 /* make use of TXDONE_BY_ACK */
611 cmdq->mbox.txdone_irq = false;
612 cmdq->mbox.txdone_poll = false;
614 cmdq->thread = devm_kcalloc(dev, cmdq->pdata->thread_nr,
615 sizeof(*cmdq->thread), GFP_KERNEL);
619 for (i = 0; i < cmdq->pdata->thread_nr; i++) {
620 cmdq->thread[i].base = cmdq->base + CMDQ_THR_BASE +
622 INIT_LIST_HEAD(&cmdq->thread[i].task_busy_list);
623 cmdq->mbox.chans[i].con_priv = (void *)&cmdq->thread[i];
626 err = devm_mbox_controller_register(dev, &cmdq->mbox);
628 dev_err(dev, "failed to register mailbox: %d\n", err);
632 platform_set_drvdata(pdev, cmdq);
634 WARN_ON(clk_bulk_prepare(cmdq->pdata->gce_num, cmdq->clocks));
638 err = devm_request_irq(dev, cmdq->irq, cmdq_irq_handler, IRQF_SHARED,
641 dev_err(dev, "failed to register ISR (%d)\n", err);
648 static const struct dev_pm_ops cmdq_pm_ops = {
649 .suspend = cmdq_suspend,
650 .resume = cmdq_resume,
653 static const struct gce_plat gce_plat_v2 = {
656 .control_by_sw = false,
660 static const struct gce_plat gce_plat_v3 = {
663 .control_by_sw = false,
667 static const struct gce_plat gce_plat_v4 = {
670 .control_by_sw = false,
674 static const struct gce_plat gce_plat_v5 = {
677 .control_by_sw = true,
681 static const struct gce_plat gce_plat_v6 = {
684 .control_by_sw = true,
688 static const struct gce_plat gce_plat_v7 = {
691 .control_by_sw = true,
696 static const struct of_device_id cmdq_of_ids[] = {
697 {.compatible = "mediatek,mt8173-gce", .data = (void *)&gce_plat_v2},
698 {.compatible = "mediatek,mt8183-gce", .data = (void *)&gce_plat_v3},
699 {.compatible = "mediatek,mt8186-gce", .data = (void *)&gce_plat_v7},
700 {.compatible = "mediatek,mt6779-gce", .data = (void *)&gce_plat_v4},
701 {.compatible = "mediatek,mt8192-gce", .data = (void *)&gce_plat_v5},
702 {.compatible = "mediatek,mt8195-gce", .data = (void *)&gce_plat_v6},
706 static struct platform_driver cmdq_drv = {
708 .remove = cmdq_remove,
712 .of_match_table = cmdq_of_ids,
716 static int __init cmdq_drv_init(void)
718 return platform_driver_register(&cmdq_drv);
721 static void __exit cmdq_drv_exit(void)
723 platform_driver_unregister(&cmdq_drv);
726 subsys_initcall(cmdq_drv_init);
727 module_exit(cmdq_drv_exit);
729 MODULE_LICENSE("GPL v2");