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 dev_pm_opp *opp;
24 opp = devfreq_recommended_opp(dev, freq, flags);
29 err = dev_pm_opp_set_rate(dev, *freq);
36 static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
38 pfdev->devfreq.busy_time = 0;
39 pfdev->devfreq.idle_time = 0;
40 pfdev->devfreq.time_last_update = ktime_get();
43 static int panfrost_devfreq_get_dev_status(struct device *dev,
44 struct devfreq_dev_status *status)
46 struct panfrost_device *pfdev = dev_get_drvdata(dev);
48 panfrost_devfreq_update_utilization(pfdev);
50 status->current_frequency = clk_get_rate(pfdev->clock);
51 status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.busy_time,
52 pfdev->devfreq.idle_time));
54 status->busy_time = ktime_to_ns(pfdev->devfreq.busy_time);
56 panfrost_devfreq_reset(pfdev);
58 dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
60 status->busy_time / (status->total_time / 100),
61 status->current_frequency / 1000 / 1000);
66 static struct devfreq_dev_profile panfrost_devfreq_profile = {
67 .polling_ms = 50, /* ~3 frames */
68 .target = panfrost_devfreq_target,
69 .get_dev_status = panfrost_devfreq_get_dev_status,
72 int panfrost_devfreq_init(struct panfrost_device *pfdev)
75 struct dev_pm_opp *opp;
76 unsigned long cur_freq;
78 ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev);
79 if (ret == -ENODEV) /* Optional, continue without devfreq */
84 panfrost_devfreq_reset(pfdev);
86 cur_freq = clk_get_rate(pfdev->clock);
88 opp = devfreq_recommended_opp(&pfdev->pdev->dev, &cur_freq, 0);
92 panfrost_devfreq_profile.initial_freq = cur_freq;
95 pfdev->devfreq.devfreq = devm_devfreq_add_device(&pfdev->pdev->dev,
96 &panfrost_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
98 if (IS_ERR(pfdev->devfreq.devfreq)) {
99 DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n");
100 ret = PTR_ERR(pfdev->devfreq.devfreq);
101 pfdev->devfreq.devfreq = NULL;
102 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
109 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
111 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
114 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
116 if (!pfdev->devfreq.devfreq)
119 panfrost_devfreq_reset(pfdev);
121 devfreq_resume_device(pfdev->devfreq.devfreq);
124 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
126 if (!pfdev->devfreq.devfreq)
129 devfreq_suspend_device(pfdev->devfreq.devfreq);
132 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev)
137 if (!pfdev->devfreq.devfreq)
141 last = pfdev->devfreq.time_last_update;
143 if (atomic_read(&pfdev->devfreq.busy_count) > 0)
144 pfdev->devfreq.busy_time += ktime_sub(now, last);
146 pfdev->devfreq.idle_time += ktime_sub(now, last);
148 pfdev->devfreq.time_last_update = now;
151 void panfrost_devfreq_record_busy(struct panfrost_device *pfdev)
153 panfrost_devfreq_update_utilization(pfdev);
154 atomic_inc(&pfdev->devfreq.busy_count);
157 void panfrost_devfreq_record_idle(struct panfrost_device *pfdev)
161 panfrost_devfreq_update_utilization(pfdev);
162 count = atomic_dec_if_positive(&pfdev->devfreq.busy_count);