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;
63 struct list_head list_entry;
65 struct cmdq_thread *thread;
66 struct cmdq_pkt *pkt; /* the packet sent from mailbox client */
70 struct mbox_controller mbox;
75 struct cmdq_thread *thread;
86 u8 cmdq_get_shift_pa(struct mbox_chan *chan)
88 struct cmdq *cmdq = container_of(chan->mbox, struct cmdq, mbox);
90 return cmdq->shift_pa;
92 EXPORT_SYMBOL(cmdq_get_shift_pa);
94 static int cmdq_thread_suspend(struct cmdq *cmdq, struct cmdq_thread *thread)
98 writel(CMDQ_THR_SUSPEND, thread->base + CMDQ_THR_SUSPEND_TASK);
100 /* If already disabled, treat as suspended successful. */
101 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
104 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_CURR_STATUS,
105 status, status & CMDQ_THR_STATUS_SUSPENDED, 0, 10)) {
106 dev_err(cmdq->mbox.dev, "suspend GCE thread 0x%x failed\n",
107 (u32)(thread->base - cmdq->base));
114 static void cmdq_thread_resume(struct cmdq_thread *thread)
116 writel(CMDQ_THR_RESUME, thread->base + CMDQ_THR_SUSPEND_TASK);
119 static void cmdq_init(struct cmdq *cmdq)
123 WARN_ON(clk_enable(cmdq->clock) < 0);
124 writel(CMDQ_THR_ACTIVE_SLOT_CYCLES, cmdq->base + CMDQ_THR_SLOT_CYCLES);
125 for (i = 0; i <= CMDQ_MAX_EVENT; i++)
126 writel(i, cmdq->base + CMDQ_SYNC_TOKEN_UPDATE);
127 clk_disable(cmdq->clock);
130 static int cmdq_thread_reset(struct cmdq *cmdq, struct cmdq_thread *thread)
134 writel(CMDQ_THR_DO_WARM_RESET, thread->base + CMDQ_THR_WARM_RESET);
135 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_WARM_RESET,
136 warm_reset, !(warm_reset & CMDQ_THR_DO_WARM_RESET),
138 dev_err(cmdq->mbox.dev, "reset GCE thread 0x%x failed\n",
139 (u32)(thread->base - cmdq->base));
146 static void cmdq_thread_disable(struct cmdq *cmdq, struct cmdq_thread *thread)
148 cmdq_thread_reset(cmdq, thread);
149 writel(CMDQ_THR_DISABLED, thread->base + CMDQ_THR_ENABLE_TASK);
152 /* notify GCE to re-fetch commands by setting GCE thread PC */
153 static void cmdq_thread_invalidate_fetched_data(struct cmdq_thread *thread)
155 writel(readl(thread->base + CMDQ_THR_CURR_ADDR),
156 thread->base + CMDQ_THR_CURR_ADDR);
159 static void cmdq_task_insert_into_thread(struct cmdq_task *task)
161 struct device *dev = task->cmdq->mbox.dev;
162 struct cmdq_thread *thread = task->thread;
163 struct cmdq_task *prev_task = list_last_entry(
164 &thread->task_busy_list, typeof(*task), list_entry);
165 u64 *prev_task_base = prev_task->pkt->va_base;
167 /* let previous task jump to this task */
168 dma_sync_single_for_cpu(dev, prev_task->pa_base,
169 prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
170 prev_task_base[CMDQ_NUM_CMD(prev_task->pkt) - 1] =
171 (u64)CMDQ_JUMP_BY_PA << 32 |
172 (task->pa_base >> task->cmdq->shift_pa);
173 dma_sync_single_for_device(dev, prev_task->pa_base,
174 prev_task->pkt->cmd_buf_size, DMA_TO_DEVICE);
176 cmdq_thread_invalidate_fetched_data(thread);
179 static bool cmdq_thread_is_in_wfe(struct cmdq_thread *thread)
181 return readl(thread->base + CMDQ_THR_WAIT_TOKEN) & CMDQ_THR_IS_WAITING;
184 static void cmdq_task_exec_done(struct cmdq_task *task, enum cmdq_cb_status sta)
186 struct cmdq_task_cb *cb = &task->pkt->async_cb;
187 struct cmdq_cb_data data;
189 WARN_ON(cb->cb == (cmdq_async_flush_cb)NULL);
191 data.data = cb->data;
194 list_del(&task->list_entry);
197 static void cmdq_task_handle_error(struct cmdq_task *task)
199 struct cmdq_thread *thread = task->thread;
200 struct cmdq_task *next_task;
201 struct cmdq *cmdq = task->cmdq;
203 dev_err(cmdq->mbox.dev, "task 0x%p error\n", task);
204 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
205 next_task = list_first_entry_or_null(&thread->task_busy_list,
206 struct cmdq_task, list_entry);
208 writel(next_task->pa_base >> cmdq->shift_pa,
209 thread->base + CMDQ_THR_CURR_ADDR);
210 cmdq_thread_resume(thread);
213 static void cmdq_thread_irq_handler(struct cmdq *cmdq,
214 struct cmdq_thread *thread)
216 struct cmdq_task *task, *tmp, *curr_task = NULL;
217 u32 curr_pa, irq_flag, task_end_pa;
220 irq_flag = readl(thread->base + CMDQ_THR_IRQ_STATUS);
221 writel(~irq_flag, thread->base + CMDQ_THR_IRQ_STATUS);
224 * When ISR call this function, another CPU core could run
225 * "release task" right before we acquire the spin lock, and thus
226 * reset / disable this GCE thread, so we need to check the enable
227 * bit of this GCE thread.
229 if (!(readl(thread->base + CMDQ_THR_ENABLE_TASK) & CMDQ_THR_ENABLED))
232 if (irq_flag & CMDQ_THR_IRQ_ERROR)
234 else if (irq_flag & CMDQ_THR_IRQ_DONE)
239 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) << cmdq->shift_pa;
241 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
243 task_end_pa = task->pa_base + task->pkt->cmd_buf_size;
244 if (curr_pa >= task->pa_base && curr_pa < task_end_pa)
247 if (!curr_task || curr_pa == task_end_pa - CMDQ_INST_SIZE) {
248 cmdq_task_exec_done(task, CMDQ_CB_NORMAL);
251 cmdq_task_exec_done(task, CMDQ_CB_ERROR);
252 cmdq_task_handle_error(curr_task);
260 if (list_empty(&thread->task_busy_list)) {
261 cmdq_thread_disable(cmdq, thread);
262 clk_disable(cmdq->clock);
266 static irqreturn_t cmdq_irq_handler(int irq, void *dev)
268 struct cmdq *cmdq = dev;
269 unsigned long irq_status, flags = 0L;
272 irq_status = readl(cmdq->base + CMDQ_CURR_IRQ_STATUS) & cmdq->irq_mask;
273 if (!(irq_status ^ cmdq->irq_mask))
276 for_each_clear_bit(bit, &irq_status, cmdq->thread_nr) {
277 struct cmdq_thread *thread = &cmdq->thread[bit];
279 spin_lock_irqsave(&thread->chan->lock, flags);
280 cmdq_thread_irq_handler(cmdq, thread);
281 spin_unlock_irqrestore(&thread->chan->lock, flags);
287 static int cmdq_suspend(struct device *dev)
289 struct cmdq *cmdq = dev_get_drvdata(dev);
290 struct cmdq_thread *thread;
292 bool task_running = false;
294 cmdq->suspended = true;
296 for (i = 0; i < cmdq->thread_nr; i++) {
297 thread = &cmdq->thread[i];
298 if (!list_empty(&thread->task_busy_list)) {
305 dev_warn(dev, "exist running task(s) in suspend\n");
307 clk_unprepare(cmdq->clock);
312 static int cmdq_resume(struct device *dev)
314 struct cmdq *cmdq = dev_get_drvdata(dev);
316 WARN_ON(clk_prepare(cmdq->clock) < 0);
317 cmdq->suspended = false;
321 static int cmdq_remove(struct platform_device *pdev)
323 struct cmdq *cmdq = platform_get_drvdata(pdev);
325 clk_unprepare(cmdq->clock);
330 static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
332 struct cmdq_pkt *pkt = (struct cmdq_pkt *)data;
333 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
334 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
335 struct cmdq_task *task;
336 unsigned long curr_pa, end_pa;
338 /* Client should not flush new tasks if suspended. */
339 WARN_ON(cmdq->suspended);
341 task = kzalloc(sizeof(*task), GFP_ATOMIC);
346 INIT_LIST_HEAD(&task->list_entry);
347 task->pa_base = pkt->pa_base;
348 task->thread = thread;
351 if (list_empty(&thread->task_busy_list)) {
352 WARN_ON(clk_enable(cmdq->clock) < 0);
354 * The thread reset will clear thread related register to 0,
355 * including pc, end, priority, irq, suspend and enable. Thus
356 * set CMDQ_THR_ENABLED to CMDQ_THR_ENABLE_TASK will enable
357 * thread and make it running.
359 WARN_ON(cmdq_thread_reset(cmdq, thread) < 0);
361 writel(task->pa_base >> cmdq->shift_pa,
362 thread->base + CMDQ_THR_CURR_ADDR);
363 writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->shift_pa,
364 thread->base + CMDQ_THR_END_ADDR);
366 writel(thread->priority, thread->base + CMDQ_THR_PRIORITY);
367 writel(CMDQ_THR_IRQ_EN, thread->base + CMDQ_THR_IRQ_ENABLE);
368 writel(CMDQ_THR_ENABLED, thread->base + CMDQ_THR_ENABLE_TASK);
370 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
371 curr_pa = readl(thread->base + CMDQ_THR_CURR_ADDR) <<
373 end_pa = readl(thread->base + CMDQ_THR_END_ADDR) <<
376 if (curr_pa == end_pa - CMDQ_INST_SIZE ||
378 /* set to this task directly */
379 writel(task->pa_base >> cmdq->shift_pa,
380 thread->base + CMDQ_THR_CURR_ADDR);
382 cmdq_task_insert_into_thread(task);
383 smp_mb(); /* modify jump before enable thread */
385 writel((task->pa_base + pkt->cmd_buf_size) >> cmdq->shift_pa,
386 thread->base + CMDQ_THR_END_ADDR);
387 cmdq_thread_resume(thread);
389 list_move_tail(&task->list_entry, &thread->task_busy_list);
394 static int cmdq_mbox_startup(struct mbox_chan *chan)
399 static void cmdq_mbox_shutdown(struct mbox_chan *chan)
401 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
402 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
403 struct cmdq_task *task, *tmp;
406 spin_lock_irqsave(&thread->chan->lock, flags);
407 if (list_empty(&thread->task_busy_list))
410 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
412 /* make sure executed tasks have success callback */
413 cmdq_thread_irq_handler(cmdq, thread);
414 if (list_empty(&thread->task_busy_list))
417 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
419 cmdq_task_exec_done(task, CMDQ_CB_ERROR);
423 cmdq_thread_disable(cmdq, thread);
424 clk_disable(cmdq->clock);
427 * The thread->task_busy_list empty means thread already disable. The
428 * cmdq_mbox_send_data() always reset thread which clear disable and
429 * suspend statue when first pkt send to channel, so there is no need
430 * to do any operation here, only unlock and leave.
432 spin_unlock_irqrestore(&thread->chan->lock, flags);
435 static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
437 struct cmdq_thread *thread = (struct cmdq_thread *)chan->con_priv;
438 struct cmdq_task_cb *cb;
439 struct cmdq_cb_data data;
440 struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
441 struct cmdq_task *task, *tmp;
445 spin_lock_irqsave(&thread->chan->lock, flags);
446 if (list_empty(&thread->task_busy_list))
449 WARN_ON(cmdq_thread_suspend(cmdq, thread) < 0);
450 if (!cmdq_thread_is_in_wfe(thread))
453 list_for_each_entry_safe(task, tmp, &thread->task_busy_list,
455 cb = &task->pkt->async_cb;
457 data.sta = CMDQ_CB_ERROR;
458 data.data = cb->data;
461 list_del(&task->list_entry);
465 cmdq_thread_resume(thread);
466 cmdq_thread_disable(cmdq, thread);
467 clk_disable(cmdq->clock);
470 spin_unlock_irqrestore(&thread->chan->lock, flags);
474 cmdq_thread_resume(thread);
475 spin_unlock_irqrestore(&thread->chan->lock, flags);
476 if (readl_poll_timeout_atomic(thread->base + CMDQ_THR_ENABLE_TASK,
477 enable, enable == 0, 1, timeout)) {
478 dev_err(cmdq->mbox.dev, "Fail to wait GCE thread 0x%x done\n",
479 (u32)(thread->base - cmdq->base));
486 static const struct mbox_chan_ops cmdq_mbox_chan_ops = {
487 .send_data = cmdq_mbox_send_data,
488 .startup = cmdq_mbox_startup,
489 .shutdown = cmdq_mbox_shutdown,
490 .flush = cmdq_mbox_flush,
493 static struct mbox_chan *cmdq_xlate(struct mbox_controller *mbox,
494 const struct of_phandle_args *sp)
496 int ind = sp->args[0];
497 struct cmdq_thread *thread;
499 if (ind >= mbox->num_chans)
500 return ERR_PTR(-EINVAL);
502 thread = (struct cmdq_thread *)mbox->chans[ind].con_priv;
503 thread->priority = sp->args[1];
504 thread->chan = &mbox->chans[ind];
506 return &mbox->chans[ind];
509 static int cmdq_probe(struct platform_device *pdev)
511 struct device *dev = &pdev->dev;
512 struct resource *res;
515 struct gce_plat *plat_data;
517 cmdq = devm_kzalloc(dev, sizeof(*cmdq), GFP_KERNEL);
521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
522 cmdq->base = devm_ioremap_resource(dev, res);
523 if (IS_ERR(cmdq->base)) {
524 dev_err(dev, "failed to ioremap gce\n");
525 return PTR_ERR(cmdq->base);
528 cmdq->irq = platform_get_irq(pdev, 0);
532 plat_data = (struct gce_plat *)of_device_get_match_data(dev);
534 dev_err(dev, "failed to get match data\n");
538 cmdq->thread_nr = plat_data->thread_nr;
539 cmdq->shift_pa = plat_data->shift;
540 cmdq->irq_mask = GENMASK(cmdq->thread_nr - 1, 0);
541 err = devm_request_irq(dev, cmdq->irq, cmdq_irq_handler, IRQF_SHARED,
544 dev_err(dev, "failed to register ISR (%d)\n", err);
548 dev_dbg(dev, "cmdq device: addr:0x%p, va:0x%p, irq:%d\n",
549 dev, cmdq->base, cmdq->irq);
551 cmdq->clock = devm_clk_get(dev, "gce");
552 if (IS_ERR(cmdq->clock)) {
553 dev_err(dev, "failed to get gce clk\n");
554 return PTR_ERR(cmdq->clock);
557 cmdq->mbox.dev = dev;
558 cmdq->mbox.chans = devm_kcalloc(dev, cmdq->thread_nr,
559 sizeof(*cmdq->mbox.chans), GFP_KERNEL);
560 if (!cmdq->mbox.chans)
563 cmdq->mbox.num_chans = cmdq->thread_nr;
564 cmdq->mbox.ops = &cmdq_mbox_chan_ops;
565 cmdq->mbox.of_xlate = cmdq_xlate;
567 /* make use of TXDONE_BY_ACK */
568 cmdq->mbox.txdone_irq = false;
569 cmdq->mbox.txdone_poll = false;
571 cmdq->thread = devm_kcalloc(dev, cmdq->thread_nr,
572 sizeof(*cmdq->thread), GFP_KERNEL);
576 for (i = 0; i < cmdq->thread_nr; i++) {
577 cmdq->thread[i].base = cmdq->base + CMDQ_THR_BASE +
579 INIT_LIST_HEAD(&cmdq->thread[i].task_busy_list);
580 cmdq->mbox.chans[i].con_priv = (void *)&cmdq->thread[i];
583 err = devm_mbox_controller_register(dev, &cmdq->mbox);
585 dev_err(dev, "failed to register mailbox: %d\n", err);
589 platform_set_drvdata(pdev, cmdq);
590 WARN_ON(clk_prepare(cmdq->clock) < 0);
597 static const struct dev_pm_ops cmdq_pm_ops = {
598 .suspend = cmdq_suspend,
599 .resume = cmdq_resume,
602 static const struct gce_plat gce_plat_v2 = {.thread_nr = 16};
603 static const struct gce_plat gce_plat_v3 = {.thread_nr = 24};
604 static const struct gce_plat gce_plat_v4 = {.thread_nr = 24, .shift = 3};
606 static const struct of_device_id cmdq_of_ids[] = {
607 {.compatible = "mediatek,mt8173-gce", .data = (void *)&gce_plat_v2},
608 {.compatible = "mediatek,mt8183-gce", .data = (void *)&gce_plat_v3},
609 {.compatible = "mediatek,mt6779-gce", .data = (void *)&gce_plat_v4},
613 static struct platform_driver cmdq_drv = {
615 .remove = cmdq_remove,
619 .of_match_table = cmdq_of_ids,
623 static int __init cmdq_drv_init(void)
625 return platform_driver_register(&cmdq_drv);
628 static void __exit cmdq_drv_exit(void)
630 platform_driver_unregister(&cmdq_drv);
633 subsys_initcall(cmdq_drv_init);
634 module_exit(cmdq_drv_exit);
636 MODULE_LICENSE("GPL v2");