2018-09-11 03:27:58 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright 2019 Collabora ltd. */
|
2020-07-10 17:53:57 +08:00
|
|
|
|
|
|
|
#include <linux/clk.h>
|
2018-09-11 03:27:58 +08:00
|
|
|
#include <linux/devfreq.h>
|
2019-11-29 04:54:27 +08:00
|
|
|
#include <linux/devfreq_cooling.h>
|
2018-09-11 03:27:58 +08:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm_opp.h>
|
|
|
|
|
|
|
|
#include "panfrost_device.h"
|
2019-04-17 22:47:49 +08:00
|
|
|
#include "panfrost_devfreq.h"
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
static void panfrost_devfreq_update_utilization(struct panfrost_devfreq *pfdevfreq)
|
2020-07-10 17:53:56 +08:00
|
|
|
{
|
|
|
|
ktime_t now;
|
|
|
|
ktime_t last;
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
if (!pfdevfreq->devfreq)
|
2020-07-10 17:53:56 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
now = ktime_get();
|
2020-07-10 17:53:59 +08:00
|
|
|
last = pfdevfreq->time_last_update;
|
2020-07-10 17:53:56 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
if (atomic_read(&pfdevfreq->busy_count) > 0)
|
|
|
|
pfdevfreq->busy_time += ktime_sub(now, last);
|
2020-07-10 17:53:56 +08:00
|
|
|
else
|
2020-07-10 17:53:59 +08:00
|
|
|
pfdevfreq->idle_time += ktime_sub(now, last);
|
2020-07-10 17:53:56 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
pfdevfreq->time_last_update = now;
|
2020-07-10 17:53:56 +08:00
|
|
|
}
|
2018-09-11 03:27:58 +08:00
|
|
|
|
|
|
|
static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
|
|
|
|
u32 flags)
|
|
|
|
{
|
2019-11-19 01:30:02 +08:00
|
|
|
struct dev_pm_opp *opp;
|
2018-09-11 03:27:58 +08:00
|
|
|
int err;
|
|
|
|
|
2019-11-19 01:30:02 +08:00
|
|
|
opp = devfreq_recommended_opp(dev, freq, flags);
|
|
|
|
if (IS_ERR(opp))
|
|
|
|
return PTR_ERR(opp);
|
|
|
|
dev_pm_opp_put(opp);
|
|
|
|
|
2019-10-25 21:41:42 +08:00
|
|
|
err = dev_pm_opp_set_rate(dev, *freq);
|
|
|
|
if (err)
|
2018-09-11 03:27:58 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
static void panfrost_devfreq_reset(struct panfrost_devfreq *pfdevfreq)
|
2018-09-11 03:27:58 +08:00
|
|
|
{
|
2020-07-10 17:53:59 +08:00
|
|
|
pfdevfreq->busy_time = 0;
|
|
|
|
pfdevfreq->idle_time = 0;
|
|
|
|
pfdevfreq->time_last_update = ktime_get();
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int panfrost_devfreq_get_dev_status(struct device *dev,
|
|
|
|
struct devfreq_dev_status *status)
|
|
|
|
{
|
2019-10-25 21:41:42 +08:00
|
|
|
struct panfrost_device *pfdev = dev_get_drvdata(dev);
|
2020-07-10 17:53:59 +08:00
|
|
|
struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_update_utilization(pfdevfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
|
|
|
status->current_frequency = clk_get_rate(pfdev->clock);
|
2020-07-10 17:53:59 +08:00
|
|
|
status->total_time = ktime_to_ns(ktime_add(pfdevfreq->busy_time,
|
|
|
|
pfdevfreq->idle_time));
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
status->busy_time = ktime_to_ns(pfdevfreq->busy_time);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_reset(pfdevfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
|
|
|
|
status->busy_time, status->total_time,
|
2018-09-11 03:27:58 +08:00
|
|
|
status->busy_time / (status->total_time / 100),
|
|
|
|
status->current_frequency / 1000 / 1000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct devfreq_dev_profile panfrost_devfreq_profile = {
|
|
|
|
.polling_ms = 50, /* ~3 frames */
|
|
|
|
.target = panfrost_devfreq_target,
|
|
|
|
.get_dev_status = panfrost_devfreq_get_dev_status,
|
|
|
|
};
|
|
|
|
|
|
|
|
int panfrost_devfreq_init(struct panfrost_device *pfdev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct dev_pm_opp *opp;
|
2019-10-25 21:41:42 +08:00
|
|
|
unsigned long cur_freq;
|
2019-11-29 04:54:27 +08:00
|
|
|
struct device *dev = &pfdev->pdev->dev;
|
|
|
|
struct devfreq *devfreq;
|
|
|
|
struct thermal_cooling_device *cooling;
|
2020-07-10 17:53:59 +08:00
|
|
|
struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-11-29 04:54:27 +08:00
|
|
|
ret = dev_pm_opp_of_add_table(dev);
|
2019-06-05 23:02:33 +08:00
|
|
|
if (ret == -ENODEV) /* Optional, continue without devfreq */
|
|
|
|
return 0;
|
|
|
|
else if (ret)
|
2019-05-17 23:00:42 +08:00
|
|
|
return ret;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_reset(pfdevfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-10-25 21:41:42 +08:00
|
|
|
cur_freq = clk_get_rate(pfdev->clock);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-11-29 04:54:27 +08:00
|
|
|
opp = devfreq_recommended_opp(dev, &cur_freq, 0);
|
2018-09-11 03:27:58 +08:00
|
|
|
if (IS_ERR(opp))
|
|
|
|
return PTR_ERR(opp);
|
|
|
|
|
2019-10-25 21:41:42 +08:00
|
|
|
panfrost_devfreq_profile.initial_freq = cur_freq;
|
2018-09-11 03:27:58 +08:00
|
|
|
dev_pm_opp_put(opp);
|
|
|
|
|
2019-11-29 04:54:27 +08:00
|
|
|
devfreq = devm_devfreq_add_device(dev, &panfrost_devfreq_profile,
|
|
|
|
DEVFREQ_GOV_SIMPLE_ONDEMAND, NULL);
|
|
|
|
if (IS_ERR(devfreq)) {
|
|
|
|
DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
|
|
|
|
dev_pm_opp_of_remove_table(dev);
|
|
|
|
return PTR_ERR(devfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
2020-07-10 17:53:59 +08:00
|
|
|
pfdevfreq->devfreq = devfreq;
|
2019-11-29 04:54:27 +08:00
|
|
|
|
|
|
|
cooling = of_devfreq_cooling_register(dev->of_node, devfreq);
|
|
|
|
if (IS_ERR(cooling))
|
|
|
|
DRM_DEV_INFO(dev, "Failed to register cooling device\n");
|
|
|
|
else
|
2020-07-10 17:53:59 +08:00
|
|
|
pfdevfreq->cooling = cooling;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:31:07 +08:00
|
|
|
void panfrost_devfreq_fini(struct panfrost_device *pfdev)
|
|
|
|
{
|
2020-07-10 17:53:59 +08:00
|
|
|
struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
|
|
|
|
|
|
|
|
if (pfdevfreq->cooling)
|
|
|
|
devfreq_cooling_unregister(pfdevfreq->cooling);
|
2019-08-16 17:31:07 +08:00
|
|
|
dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
|
|
|
|
}
|
|
|
|
|
2018-09-11 03:27:58 +08:00
|
|
|
void panfrost_devfreq_resume(struct panfrost_device *pfdev)
|
|
|
|
{
|
2020-07-10 17:53:59 +08:00
|
|
|
struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
|
|
|
|
|
|
|
|
if (!pfdevfreq->devfreq)
|
2019-06-05 23:02:33 +08:00
|
|
|
return;
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_reset(pfdevfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
devfreq_resume_device(pfdevfreq->devfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
|
|
|
|
{
|
2020-07-10 17:53:59 +08:00
|
|
|
struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
|
|
|
|
|
|
|
|
if (!pfdevfreq->devfreq)
|
2019-06-05 23:02:33 +08:00
|
|
|
return;
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
devfreq_suspend_device(pfdevfreq->devfreq);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
void panfrost_devfreq_record_busy(struct panfrost_devfreq *pfdevfreq)
|
2019-10-25 21:41:43 +08:00
|
|
|
{
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_update_utilization(pfdevfreq);
|
|
|
|
atomic_inc(&pfdevfreq->busy_count);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
void panfrost_devfreq_record_idle(struct panfrost_devfreq *pfdevfreq)
|
2018-09-11 03:27:58 +08:00
|
|
|
{
|
2019-10-25 21:41:43 +08:00
|
|
|
int count;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2020-07-10 17:53:59 +08:00
|
|
|
panfrost_devfreq_update_utilization(pfdevfreq);
|
|
|
|
count = atomic_dec_if_positive(&pfdevfreq->busy_count);
|
2019-10-25 21:41:43 +08:00
|
|
|
WARN_ON(count < 0);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|