1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Collabora ltd. */
3 #include <linux/devfreq.h>
4 #include <linux/platform_device.h>
5 #include <linux/pm_opp.h>
7 #include <linux/regulator/consumer.h>
9 #include "panfrost_device.h"
10 #include "panfrost_devfreq.h"
11 #include "panfrost_features.h"
12 #include "panfrost_issues.h"
13 #include "panfrost_gpu.h"
14 #include "panfrost_regs.h"
16 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev);
18 static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
21 struct panfrost_device *pfdev = dev_get_drvdata(dev);
24 err = dev_pm_opp_set_rate(dev, *freq);
28 *freq = clk_get_rate(pfdev->clock);
33 static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
35 pfdev->devfreq.busy_time = 0;
36 pfdev->devfreq.idle_time = 0;
37 pfdev->devfreq.time_last_update = ktime_get();
40 static int panfrost_devfreq_get_dev_status(struct device *dev,
41 struct devfreq_dev_status *status)
43 struct panfrost_device *pfdev = dev_get_drvdata(dev);
45 panfrost_devfreq_update_utilization(pfdev);
47 status->current_frequency = clk_get_rate(pfdev->clock);
48 status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.busy_time,
49 pfdev->devfreq.idle_time));
51 status->busy_time = ktime_to_ns(pfdev->devfreq.busy_time);
53 panfrost_devfreq_reset(pfdev);
55 dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
57 status->busy_time / (status->total_time / 100),
58 status->current_frequency / 1000 / 1000);
63 static int panfrost_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
65 struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
67 *freq = clk_get_rate(pfdev->clock);
72 static struct devfreq_dev_profile panfrost_devfreq_profile = {
73 .polling_ms = 50, /* ~3 frames */
74 .target = panfrost_devfreq_target,
75 .get_dev_status = panfrost_devfreq_get_dev_status,
76 .get_cur_freq = panfrost_devfreq_get_cur_freq,
79 int panfrost_devfreq_init(struct panfrost_device *pfdev)
82 struct dev_pm_opp *opp;
83 unsigned long cur_freq;
85 ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev);
86 if (ret == -ENODEV) /* Optional, continue without devfreq */
91 panfrost_devfreq_reset(pfdev);
93 cur_freq = clk_get_rate(pfdev->clock);
95 opp = devfreq_recommended_opp(&pfdev->pdev->dev, &cur_freq, 0);
99 panfrost_devfreq_profile.initial_freq = cur_freq;
102 pfdev->devfreq.devfreq = devm_devfreq_add_device(&pfdev->pdev->dev,
103 &panfrost_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
105 if (IS_ERR(pfdev->devfreq.devfreq)) {
106 DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n");
107 ret = PTR_ERR(pfdev->devfreq.devfreq);
108 pfdev->devfreq.devfreq = NULL;
109 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
116 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
118 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
121 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
123 if (!pfdev->devfreq.devfreq)
126 panfrost_devfreq_reset(pfdev);
128 devfreq_resume_device(pfdev->devfreq.devfreq);
131 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
133 if (!pfdev->devfreq.devfreq)
136 devfreq_suspend_device(pfdev->devfreq.devfreq);
139 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev)
144 if (!pfdev->devfreq.devfreq)
148 last = pfdev->devfreq.time_last_update;
150 if (atomic_read(&pfdev->devfreq.busy_count) > 0)
151 pfdev->devfreq.busy_time += ktime_sub(now, last);
153 pfdev->devfreq.idle_time += ktime_sub(now, last);
155 pfdev->devfreq.time_last_update = now;
158 void panfrost_devfreq_record_busy(struct panfrost_device *pfdev)
160 panfrost_devfreq_update_utilization(pfdev);
161 atomic_inc(&pfdev->devfreq.busy_count);
164 void panfrost_devfreq_record_idle(struct panfrost_device *pfdev)
168 panfrost_devfreq_update_utilization(pfdev);
169 count = atomic_dec_if_positive(&pfdev->devfreq.busy_count);