Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
mailbox: mtk-cmdq-mailbox: Implement Runtime PM with autosuspend
Browse files Browse the repository at this point in the history
MediaTek found an issue with display HW registers configuration, and
located the reason in the CMDQ Mailbox driver; reporting the original
comment with the analysis of this problem by Jason-JH Lin:

  GCE should config HW in every vblanking duration.
  The stream done event is the start signal of vblanking.

  If stream done event is sent between GCE clk_disable
  and clk_enable. After GCE clk_enable the stream done event
  may not appear immediately and have about 3us delay.

  Normal case:
  clk_disable -> get EventA -> clk_enable -> clear EventA
  -> wait EventB -> get EventB -> config HW

  Abnormal case:
  clk_disable -> get EventA -> clk_enable -> EventA delay appear
  -> clear EventA fail -> wait EventB but get EventA -> config HW
  This abnormal case may configure display HW in the vactive or
  non-vblanking duration.

From his analysis we get that the GCE may finish its event processing
after some amount of time (and not immediately after sending commands
to it); since the GCE is used for more than just display, and it gets
used frequently, solve this issue by implementing Runtime PM handlers
with autosuspend: this allows us to overcome to the remote processor
delay issues and reduce the clock enable()/disable() calls, while also
still managing to save some power, which is something that we wouldn't
be able to do if we just enable the GCE clocks at probe.

Speaking of which: if Runtime PM is not available there will obviously
be no way to get this power saving action so, in this case, the clocks
will be enabled at probe() time, kept enabled for the entire driver's
life and disabled at remove().

Reported-by: Jason-JH.Lin <jason-jh.lin@mediatek.com>
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Tested-by: Jason-JH.Lin <jason-jh.lin@mediatek.com>
Signed-off-by: Jassi Brar <jaswinder.singh@linaro.org>
  • Loading branch information
AngeloGioacchino Del Regno authored and JassiBrar committed Oct 15, 2023
1 parent 5cb5d0c commit 8afe816
Showing 1 changed file with 68 additions and 12 deletions.
80 changes: 68 additions & 12 deletions drivers/mailbox/mtk-cmdq-mailbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/mailbox_controller.h>
#include <linux/mailbox/mtk-cmdq-mailbox.h>
#include <linux/of.h>

#define CMDQ_MBOX_AUTOSUSPEND_DELAY_MS 100

#define CMDQ_OP_CODE_MASK (0xff << CMDQ_OP_CODE_SHIFT)
#define CMDQ_NUM_CMD(t) (t->cmd_buf_size / CMDQ_INST_SIZE)
#define CMDQ_GCE_NUM_MAX (2)
Expand Down Expand Up @@ -283,10 +286,8 @@ static void cmdq_thread_irq_handler(struct cmdq *cmdq,
break;
}

if (list_empty(&thread->task_busy_list)) {
if (list_empty(&thread->task_busy_list))
cmdq_thread_disable(cmdq, thread);
clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
}
}

static irqreturn_t cmdq_irq_handler(int irq, void *dev)
Expand All @@ -307,9 +308,26 @@ static irqreturn_t cmdq_irq_handler(int irq, void *dev)
spin_unlock_irqrestore(&thread->chan->lock, flags);
}

pm_runtime_mark_last_busy(cmdq->mbox.dev);

return IRQ_HANDLED;
}

static int cmdq_runtime_resume(struct device *dev)
{
struct cmdq *cmdq = dev_get_drvdata(dev);

return clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks);
}

static int cmdq_runtime_suspend(struct device *dev)
{
struct cmdq *cmdq = dev_get_drvdata(dev);

clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);
return 0;
}

static int cmdq_suspend(struct device *dev)
{
struct cmdq *cmdq = dev_get_drvdata(dev);
Expand All @@ -333,16 +351,14 @@ static int cmdq_suspend(struct device *dev)
if (cmdq->pdata->sw_ddr_en)
cmdq_sw_ddr_enable(cmdq, false);

clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);

return 0;
return pm_runtime_force_suspend(dev);
}

static int cmdq_resume(struct device *dev)
{
struct cmdq *cmdq = dev_get_drvdata(dev);

WARN_ON(clk_bulk_prepare(cmdq->pdata->gce_num, cmdq->clocks));
WARN_ON(pm_runtime_force_resume(dev));
cmdq->suspended = false;

if (cmdq->pdata->sw_ddr_en)
Expand All @@ -358,6 +374,9 @@ static int cmdq_remove(struct platform_device *pdev)
if (cmdq->pdata->sw_ddr_en)
cmdq_sw_ddr_enable(cmdq, false);

if (!IS_ENABLED(CONFIG_PM))
cmdq_runtime_suspend(&pdev->dev);

clk_bulk_unprepare(cmdq->pdata->gce_num, cmdq->clocks);
return 0;
}
Expand All @@ -369,13 +388,20 @@ static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
struct cmdq *cmdq = dev_get_drvdata(chan->mbox->dev);
struct cmdq_task *task;
unsigned long curr_pa, end_pa;
int ret;

/* Client should not flush new tasks if suspended. */
WARN_ON(cmdq->suspended);

ret = pm_runtime_get_sync(cmdq->mbox.dev);
if (ret < 0)
return ret;

task = kzalloc(sizeof(*task), GFP_ATOMIC);
if (!task)
if (!task) {
pm_runtime_put_autosuspend(cmdq->mbox.dev);
return -ENOMEM;
}

task->cmdq = cmdq;
INIT_LIST_HEAD(&task->list_entry);
Expand All @@ -384,8 +410,6 @@ static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
task->pkt = pkt;

if (list_empty(&thread->task_busy_list)) {
WARN_ON(clk_bulk_enable(cmdq->pdata->gce_num, cmdq->clocks));

/*
* The thread reset will clear thread related register to 0,
* including pc, end, priority, irq, suspend and enable. Thus
Expand Down Expand Up @@ -424,6 +448,9 @@ static int cmdq_mbox_send_data(struct mbox_chan *chan, void *data)
}
list_move_tail(&task->list_entry, &thread->task_busy_list);

pm_runtime_mark_last_busy(cmdq->mbox.dev);
pm_runtime_put_autosuspend(cmdq->mbox.dev);

return 0;
}

Expand All @@ -439,6 +466,8 @@ static void cmdq_mbox_shutdown(struct mbox_chan *chan)
struct cmdq_task *task, *tmp;
unsigned long flags;

WARN_ON(pm_runtime_get_sync(cmdq->mbox.dev));

spin_lock_irqsave(&thread->chan->lock, flags);
if (list_empty(&thread->task_busy_list))
goto done;
Expand All @@ -457,7 +486,6 @@ static void cmdq_mbox_shutdown(struct mbox_chan *chan)
}

cmdq_thread_disable(cmdq, thread);
clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);

done:
/*
Expand All @@ -467,6 +495,9 @@ static void cmdq_mbox_shutdown(struct mbox_chan *chan)
* to do any operation here, only unlock and leave.
*/
spin_unlock_irqrestore(&thread->chan->lock, flags);

pm_runtime_mark_last_busy(cmdq->mbox.dev);
pm_runtime_put_autosuspend(cmdq->mbox.dev);
}

static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
Expand All @@ -477,6 +508,11 @@ static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)
struct cmdq_task *task, *tmp;
unsigned long flags;
u32 enable;
int ret;

ret = pm_runtime_get_sync(cmdq->mbox.dev);
if (ret < 0)
return ret;

spin_lock_irqsave(&thread->chan->lock, flags);
if (list_empty(&thread->task_busy_list))
Expand All @@ -497,10 +533,12 @@ static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)

cmdq_thread_resume(thread);
cmdq_thread_disable(cmdq, thread);
clk_bulk_disable(cmdq->pdata->gce_num, cmdq->clocks);

out:
spin_unlock_irqrestore(&thread->chan->lock, flags);
pm_runtime_mark_last_busy(cmdq->mbox.dev);
pm_runtime_put_autosuspend(cmdq->mbox.dev);

return 0;

wait:
Expand All @@ -513,6 +551,8 @@ static int cmdq_mbox_flush(struct mbox_chan *chan, unsigned long timeout)

return -EFAULT;
}
pm_runtime_mark_last_busy(cmdq->mbox.dev);
pm_runtime_put_autosuspend(cmdq->mbox.dev);
return 0;
}

Expand Down Expand Up @@ -642,12 +682,28 @@ static int cmdq_probe(struct platform_device *pdev)
return err;
}

/* If Runtime PM is not available enable the clocks now. */
if (!IS_ENABLED(CONFIG_PM)) {
err = cmdq_runtime_resume(dev);
if (err)
return err;
}

err = devm_pm_runtime_enable(dev);
if (err)
return err;

pm_runtime_set_autosuspend_delay(dev, CMDQ_MBOX_AUTOSUSPEND_DELAY_MS);
pm_runtime_use_autosuspend(dev);

return 0;
}

static const struct dev_pm_ops cmdq_pm_ops = {
.suspend = cmdq_suspend,
.resume = cmdq_resume,
SET_RUNTIME_PM_OPS(cmdq_runtime_suspend,
cmdq_runtime_resume, NULL)
};

static const struct gce_plat gce_plat_v2 = {
Expand Down

0 comments on commit 8afe816

Please sign in to comment.