2018-09-11 03:27:58 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright 2019 Collabora ltd. */
|
|
|
|
#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 <linux/clk.h>
|
|
|
|
#include <linux/regulator/consumer.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
|
|
|
#include "panfrost_features.h"
|
|
|
|
#include "panfrost_issues.h"
|
|
|
|
#include "panfrost_gpu.h"
|
|
|
|
#include "panfrost_regs.h"
|
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
|
|
|
|
{
|
2019-10-25 21:41:43 +08:00
|
|
|
pfdev->devfreq.busy_time = 0;
|
|
|
|
pfdev->devfreq.idle_time = 0;
|
|
|
|
pfdev->devfreq.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);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
panfrost_devfreq_update_utilization(pfdev);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
|
|
|
status->current_frequency = clk_get_rate(pfdev->clock);
|
2019-10-25 21:41:43 +08:00
|
|
|
status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.busy_time,
|
|
|
|
pfdev->devfreq.idle_time));
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
status->busy_time = ktime_to_ns(pfdev->devfreq.busy_time);
|
2018-09-11 03:27:58 +08:00
|
|
|
|
|
|
|
panfrost_devfreq_reset(pfdev);
|
|
|
|
|
|
|
|
dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
|
|
|
|
status->total_time,
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
panfrost_devfreq_reset(pfdev);
|
|
|
|
|
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
|
|
|
}
|
2019-11-29 04:54:27 +08:00
|
|
|
pfdev->devfreq.devfreq = devfreq;
|
|
|
|
|
|
|
|
cooling = of_devfreq_cooling_register(dev->of_node, devfreq);
|
|
|
|
if (IS_ERR(cooling))
|
|
|
|
DRM_DEV_INFO(dev, "Failed to register cooling device\n");
|
|
|
|
else
|
|
|
|
pfdev->devfreq.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)
|
|
|
|
{
|
2019-11-29 04:54:27 +08:00
|
|
|
if (pfdev->devfreq.cooling)
|
|
|
|
devfreq_cooling_unregister(pfdev->devfreq.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)
|
|
|
|
{
|
2019-06-05 23:02:33 +08:00
|
|
|
if (!pfdev->devfreq.devfreq)
|
|
|
|
return;
|
|
|
|
|
2018-09-11 03:27:58 +08:00
|
|
|
panfrost_devfreq_reset(pfdev);
|
|
|
|
|
|
|
|
devfreq_resume_device(pfdev->devfreq.devfreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
|
|
|
|
{
|
2019-06-05 23:02:33 +08:00
|
|
|
if (!pfdev->devfreq.devfreq)
|
|
|
|
return;
|
|
|
|
|
2018-09-11 03:27:58 +08:00
|
|
|
devfreq_suspend_device(pfdev->devfreq.devfreq);
|
|
|
|
}
|
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev)
|
2018-09-11 03:27:58 +08:00
|
|
|
{
|
|
|
|
ktime_t now;
|
|
|
|
ktime_t last;
|
|
|
|
|
2019-06-05 23:02:33 +08:00
|
|
|
if (!pfdev->devfreq.devfreq)
|
|
|
|
return;
|
|
|
|
|
2018-09-11 03:27:58 +08:00
|
|
|
now = ktime_get();
|
2019-10-25 21:41:43 +08:00
|
|
|
last = pfdev->devfreq.time_last_update;
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
if (atomic_read(&pfdev->devfreq.busy_count) > 0)
|
|
|
|
pfdev->devfreq.busy_time += ktime_sub(now, last);
|
2018-09-11 03:27:58 +08:00
|
|
|
else
|
2019-10-25 21:41:43 +08:00
|
|
|
pfdev->devfreq.idle_time += ktime_sub(now, last);
|
|
|
|
|
|
|
|
pfdev->devfreq.time_last_update = now;
|
|
|
|
}
|
2018-09-11 03:27:58 +08:00
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
void panfrost_devfreq_record_busy(struct panfrost_device *pfdev)
|
|
|
|
{
|
|
|
|
panfrost_devfreq_update_utilization(pfdev);
|
|
|
|
atomic_inc(&pfdev->devfreq.busy_count);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
void panfrost_devfreq_record_idle(struct panfrost_device *pfdev)
|
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
|
|
|
|
2019-10-25 21:41:43 +08:00
|
|
|
panfrost_devfreq_update_utilization(pfdev);
|
|
|
|
count = atomic_dec_if_positive(&pfdev->devfreq.busy_count);
|
|
|
|
WARN_ON(count < 0);
|
2018-09-11 03:27:58 +08:00
|
|
|
}
|