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/nvmem-consumer.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_opp.h>
11 #include "panfrost_device.h"
12 #include "panfrost_devfreq.h"
14 static void panfrost_devfreq_update_utilization(struct panfrost_devfreq *pfdevfreq)
19 last = pfdevfreq->time_last_update;
21 if (pfdevfreq->busy_count > 0)
22 pfdevfreq->busy_time += ktime_sub(now, last);
24 pfdevfreq->idle_time += ktime_sub(now, last);
26 pfdevfreq->time_last_update = now;
29 static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
32 struct dev_pm_opp *opp;
34 opp = devfreq_recommended_opp(dev, freq, flags);
39 return dev_pm_opp_set_rate(dev, *freq);
42 static void panfrost_devfreq_reset(struct panfrost_devfreq *pfdevfreq)
44 pfdevfreq->busy_time = 0;
45 pfdevfreq->idle_time = 0;
46 pfdevfreq->time_last_update = ktime_get();
49 static int panfrost_devfreq_get_dev_status(struct device *dev,
50 struct devfreq_dev_status *status)
52 struct panfrost_device *pfdev = dev_get_drvdata(dev);
53 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
54 unsigned long irqflags;
56 status->current_frequency = clk_get_rate(pfdev->clock);
58 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
60 panfrost_devfreq_update_utilization(pfdevfreq);
61 pfdevfreq->current_frequency = status->current_frequency;
63 status->total_time = ktime_to_ns(ktime_add(pfdevfreq->busy_time,
64 pfdevfreq->idle_time));
66 status->busy_time = ktime_to_ns(pfdevfreq->busy_time);
68 panfrost_devfreq_reset(pfdevfreq);
70 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
72 dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
73 status->busy_time, status->total_time,
74 status->busy_time / (status->total_time / 100),
75 status->current_frequency / 1000 / 1000);
80 static struct devfreq_dev_profile panfrost_devfreq_profile = {
81 .timer = DEVFREQ_TIMER_DELAYED,
82 .polling_ms = 50, /* ~3 frames */
83 .target = panfrost_devfreq_target,
84 .get_dev_status = panfrost_devfreq_get_dev_status,
87 static int panfrost_read_speedbin(struct device *dev)
92 ret = nvmem_cell_read_variable_le_u32(dev, "speed-bin", &val);
95 * -ENOENT means that this platform doesn't support speedbins
96 * as it didn't declare any speed-bin nvmem: in this case, we
97 * keep going without it; any other error means that we are
98 * supposed to read the bin value, but we failed doing so.
100 if (ret != -ENOENT && ret != -EOPNOTSUPP) {
101 DRM_DEV_ERROR(dev, "Cannot read speed-bin (%d).", ret);
107 DRM_DEV_DEBUG(dev, "Using speed-bin = 0x%x\n", val);
109 return devm_pm_opp_set_supported_hw(dev, &val, 1);
112 int panfrost_devfreq_init(struct panfrost_device *pfdev)
115 struct dev_pm_opp *opp;
116 unsigned long cur_freq;
117 struct device *dev = &pfdev->pdev->dev;
118 struct devfreq *devfreq;
119 struct thermal_cooling_device *cooling;
120 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
121 unsigned long freq = ULONG_MAX;
123 if (pfdev->comp->num_supplies > 1) {
125 * GPUs with more than 1 supply require platform-specific handling:
126 * continue without devfreq
128 DRM_DEV_INFO(dev, "More than 1 supply is not supported yet\n");
132 ret = panfrost_read_speedbin(dev);
136 ret = devm_pm_opp_set_regulators(dev, pfdev->comp->supply_names);
138 /* Continue if the optional regulator is missing */
139 if (ret != -ENODEV) {
140 if (ret != -EPROBE_DEFER)
141 DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n");
146 ret = devm_pm_opp_of_add_table(dev);
148 /* Optional, continue without devfreq */
153 pfdevfreq->opp_of_table_added = true;
155 spin_lock_init(&pfdevfreq->lock);
157 panfrost_devfreq_reset(pfdevfreq);
159 cur_freq = clk_get_rate(pfdev->clock);
161 opp = devfreq_recommended_opp(dev, &cur_freq, 0);
165 panfrost_devfreq_profile.initial_freq = cur_freq;
168 * Set the recommend OPP this will enable and configure the regulator
169 * if any and will avoid a switch off by regulator_late_cleanup()
171 ret = dev_pm_opp_set_opp(dev, opp);
173 DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n");
177 /* Find the fastest defined rate */
178 opp = dev_pm_opp_find_freq_floor(dev, &freq);
181 pfdevfreq->fast_rate = freq;
186 * Setup default thresholds for the simple_ondemand governor.
187 * The values are chosen based on experiments.
189 pfdevfreq->gov_data.upthreshold = 45;
190 pfdevfreq->gov_data.downdifferential = 5;
192 devfreq = devm_devfreq_add_device(dev, &panfrost_devfreq_profile,
193 DEVFREQ_GOV_SIMPLE_ONDEMAND,
194 &pfdevfreq->gov_data);
195 if (IS_ERR(devfreq)) {
196 DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
197 return PTR_ERR(devfreq);
199 pfdevfreq->devfreq = devfreq;
201 cooling = devfreq_cooling_em_register(devfreq, NULL);
203 DRM_DEV_INFO(dev, "Failed to register cooling device\n");
205 pfdevfreq->cooling = cooling;
210 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
212 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
214 if (pfdevfreq->cooling) {
215 devfreq_cooling_unregister(pfdevfreq->cooling);
216 pfdevfreq->cooling = NULL;
220 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
222 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
224 if (!pfdevfreq->devfreq)
227 panfrost_devfreq_reset(pfdevfreq);
229 devfreq_resume_device(pfdevfreq->devfreq);
232 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
234 struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
236 if (!pfdevfreq->devfreq)
239 devfreq_suspend_device(pfdevfreq->devfreq);
242 void panfrost_devfreq_record_busy(struct panfrost_devfreq *pfdevfreq)
244 unsigned long irqflags;
246 if (!pfdevfreq->devfreq)
249 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
251 panfrost_devfreq_update_utilization(pfdevfreq);
253 pfdevfreq->busy_count++;
255 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
258 void panfrost_devfreq_record_idle(struct panfrost_devfreq *pfdevfreq)
260 unsigned long irqflags;
262 if (!pfdevfreq->devfreq)
265 spin_lock_irqsave(&pfdevfreq->lock, irqflags);
267 panfrost_devfreq_update_utilization(pfdevfreq);
269 WARN_ON(--pfdevfreq->busy_count < 0);
271 spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);