1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Collabora ltd. */
5 #include <linux/devfreq.h>
6 #include <linux/devfreq_cooling.h>
7 #include <linux/platform_device.h>
8 #include <linux/pm_opp.h>
10 #include "panfrost_device.h"
11 #include "panfrost_devfreq.h"
13 static void panfrost_devfreq_update_utilization(struct panfrost_devfreq *pfdevfreq)
18 last = pfdevfreq->time_last_update;
20 if (pfdevfreq->busy_count > 0)
21 pfdevfreq->busy_time += ktime_sub(now, last);
23 pfdevfreq->idle_time += ktime_sub(now, last);
25 pfdevfreq->time_last_update = now;
28 static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
31 struct dev_pm_opp *opp;
33 opp = devfreq_recommended_opp(dev, freq, flags);
38 return dev_pm_opp_set_rate(dev, *freq);
41 static void panfrost_devfreq_reset(struct panfrost_devfreq *pfdevfreq)
43 pfdevfreq->busy_time = 0;
44 pfdevfreq->idle_time = 0;
45 pfdevfreq->time_last_update = ktime_get();
48 static int panfrost_devfreq_get_dev_status(struct device *dev,
49 struct devfreq_dev_status *status)
51 struct panfrost_device *pfdev = dev_get_drvdata(dev);
52 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
53 unsigned long irqflags;
55 status->current_frequency = clk_get_rate(pfdev->clock);
57 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
59 panfrost_devfreq_update_utilization(pfdevfreq);
61 status->total_time = ktime_to_ns(ktime_add(pfdevfreq->busy_time,
62 pfdevfreq->idle_time));
64 status->busy_time = ktime_to_ns(pfdevfreq->busy_time);
66 panfrost_devfreq_reset(pfdevfreq);
68 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
70 dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
71 status->busy_time, status->total_time,
72 status->busy_time / (status->total_time / 100),
73 status->current_frequency / 1000 / 1000);
78 static struct devfreq_dev_profile panfrost_devfreq_profile = {
79 .timer = DEVFREQ_TIMER_DELAYED,
80 .polling_ms = 50, /* ~3 frames */
81 .target = panfrost_devfreq_target,
82 .get_dev_status = panfrost_devfreq_get_dev_status,
85 int panfrost_devfreq_init(struct panfrost_device *pfdev)
88 struct dev_pm_opp *opp;
89 unsigned long cur_freq;
90 struct device *dev = &pfdev->pdev->dev;
91 struct devfreq *devfreq;
92 struct thermal_cooling_device *cooling;
93 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
95 if (pfdev->comp->num_supplies > 1) {
97 * GPUs with more than 1 supply require platform-specific handling:
98 * continue without devfreq
100 DRM_DEV_INFO(dev, "More than 1 supply is not supported yet\n");
104 ret = devm_pm_opp_set_regulators(dev, pfdev->comp->supply_names,
105 pfdev->comp->num_supplies);
107 /* Continue if the optional regulator is missing */
108 if (ret != -ENODEV) {
109 DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n");
114 ret = devm_pm_opp_of_add_table(dev);
116 /* Optional, continue without devfreq */
121 pfdevfreq->opp_of_table_added = true;
123 spin_lock_init(&pfdevfreq->lock);
125 panfrost_devfreq_reset(pfdevfreq);
127 cur_freq = clk_get_rate(pfdev->clock);
129 opp = devfreq_recommended_opp(dev, &cur_freq, 0);
133 panfrost_devfreq_profile.initial_freq = cur_freq;
137 * Setup default thresholds for the simple_ondemand governor.
138 * The values are chosen based on experiments.
140 pfdevfreq->gov_data.upthreshold = 45;
141 pfdevfreq->gov_data.downdifferential = 5;
143 devfreq = devm_devfreq_add_device(dev, &panfrost_devfreq_profile,
144 DEVFREQ_GOV_SIMPLE_ONDEMAND,
145 &pfdevfreq->gov_data);
146 if (IS_ERR(devfreq)) {
147 DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
148 return PTR_ERR(devfreq);
150 pfdevfreq->devfreq = devfreq;
152 cooling = devfreq_cooling_em_register(devfreq, NULL);
154 DRM_DEV_INFO(dev, "Failed to register cooling device\n");
156 pfdevfreq->cooling = cooling;
161 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
163 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
165 if (pfdevfreq->cooling) {
166 devfreq_cooling_unregister(pfdevfreq->cooling);
167 pfdevfreq->cooling = NULL;
171 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
173 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
175 if (!pfdevfreq->devfreq)
178 panfrost_devfreq_reset(pfdevfreq);
180 devfreq_resume_device(pfdevfreq->devfreq);
183 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
185 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
187 if (!pfdevfreq->devfreq)
190 devfreq_suspend_device(pfdevfreq->devfreq);
193 void panfrost_devfreq_record_busy(struct panfrost_devfreq *pfdevfreq)
195 unsigned long irqflags;
197 if (!pfdevfreq->devfreq)
200 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
202 panfrost_devfreq_update_utilization(pfdevfreq);
204 pfdevfreq->busy_count++;
206 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
209 void panfrost_devfreq_record_idle(struct panfrost_devfreq *pfdevfreq)
211 unsigned long irqflags;
213 if (!pfdevfreq->devfreq)
216 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
218 panfrost_devfreq_update_utilization(pfdevfreq);
220 WARN_ON(--pfdevfreq->busy_count < 0);
222 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);