1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
8 #include "msm_gpu_trace.h"
10 #include <linux/devfreq.h>
11 #include <linux/devfreq_cooling.h>
12 #include <linux/math64.h>
13 #include <linux/units.h>
19 static int msm_devfreq_target(struct device *dev, unsigned long *freq,
22 struct msm_gpu *gpu = dev_to_gpu(dev);
23 struct msm_gpu_devfreq *df = &gpu->devfreq;
24 struct dev_pm_opp *opp;
27 * Note that devfreq_recommended_opp() can modify the freq
28 * to something that actually is in the opp table:
30 opp = devfreq_recommended_opp(dev, freq, flags);
34 trace_msm_gpu_freq_change(dev_pm_opp_get_freq(opp));
37 * If the GPU is idle, devfreq is not aware, so just stash
38 * the new target freq (to use when we return to active)
41 df->idle_freq = *freq;
46 if (gpu->funcs->gpu_set_freq) {
47 mutex_lock(&df->lock);
48 gpu->funcs->gpu_set_freq(gpu, opp, df->suspended);
49 mutex_unlock(&df->lock);
51 dev_pm_opp_set_rate(dev, *freq);
59 static unsigned long get_freq(struct msm_gpu *gpu)
61 struct msm_gpu_devfreq *df = &gpu->devfreq;
64 * If the GPU is idle, use the shadow/saved freq to avoid
65 * confusing devfreq (which is unaware that we are switching
66 * to lowest freq until the device is active again)
71 if (gpu->funcs->gpu_get_freq)
72 return gpu->funcs->gpu_get_freq(gpu);
74 return clk_get_rate(gpu->core_clk);
77 static int msm_devfreq_get_dev_status(struct device *dev,
78 struct devfreq_dev_status *status)
80 struct msm_gpu *gpu = dev_to_gpu(dev);
81 struct msm_gpu_devfreq *df = &gpu->devfreq;
82 u64 busy_cycles, busy_time;
83 unsigned long sample_rate;
86 mutex_lock(&df->lock);
88 status->current_frequency = get_freq(gpu);
90 status->total_time = ktime_us_delta(time, df->time);
94 mutex_unlock(&df->lock);
95 status->busy_time = 0;
99 busy_cycles = gpu->funcs->gpu_busy(gpu, &sample_rate);
100 busy_time = busy_cycles - df->busy_cycles;
101 df->busy_cycles = busy_cycles;
103 mutex_unlock(&df->lock);
105 busy_time *= USEC_PER_SEC;
106 busy_time = div64_ul(busy_time, sample_rate);
107 if (WARN_ON(busy_time > ~0LU))
110 status->busy_time = busy_time;
115 static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
117 *freq = get_freq(dev_to_gpu(dev));
122 static struct devfreq_dev_profile msm_devfreq_profile = {
123 .timer = DEVFREQ_TIMER_DELAYED,
125 .target = msm_devfreq_target,
126 .get_dev_status = msm_devfreq_get_dev_status,
127 .get_cur_freq = msm_devfreq_get_cur_freq,
130 static void msm_devfreq_boost_work(struct kthread_work *work);
131 static void msm_devfreq_idle_work(struct kthread_work *work);
133 static bool has_devfreq(struct msm_gpu *gpu)
135 struct msm_gpu_devfreq *df = &gpu->devfreq;
136 return !!df->devfreq;
139 void msm_devfreq_init(struct msm_gpu *gpu)
141 struct msm_gpu_devfreq *df = &gpu->devfreq;
142 struct msm_drm_private *priv = gpu->dev->dev_private;
144 /* We need target support to do devfreq */
145 if (!gpu->funcs->gpu_busy)
149 * Setup default values for simple_ondemand governor tuning. We
150 * want to throttle up at 50% load for the double-buffer case,
151 * where due to stalling waiting for vblank we could get stuck
152 * at (for ex) 30fps at 50% utilization.
154 priv->gpu_devfreq_config.upthreshold = 50;
155 priv->gpu_devfreq_config.downdifferential = 10;
157 mutex_init(&df->lock);
159 dev_pm_qos_add_request(&gpu->pdev->dev, &df->boost_freq,
160 DEV_PM_QOS_MIN_FREQUENCY, 0);
162 msm_devfreq_profile.initial_freq = gpu->fast_rate;
165 * Don't set the freq_table or max_state and let devfreq build the table
167 * After a deferred probe, these may have be left to non-zero values,
168 * so set them back to zero before creating the devfreq device
170 msm_devfreq_profile.freq_table = NULL;
171 msm_devfreq_profile.max_state = 0;
173 df->devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
174 &msm_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
175 &priv->gpu_devfreq_config);
177 if (IS_ERR(df->devfreq)) {
178 DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
179 dev_pm_qos_remove_request(&df->boost_freq);
184 devfreq_suspend_device(df->devfreq);
186 gpu->cooling = of_devfreq_cooling_register(gpu->pdev->dev.of_node, df->devfreq);
187 if (IS_ERR(gpu->cooling)) {
188 DRM_DEV_ERROR(&gpu->pdev->dev,
189 "Couldn't register GPU cooling device\n");
193 msm_hrtimer_work_init(&df->boost_work, gpu->worker, msm_devfreq_boost_work,
194 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
195 msm_hrtimer_work_init(&df->idle_work, gpu->worker, msm_devfreq_idle_work,
196 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
199 static void cancel_idle_work(struct msm_gpu_devfreq *df)
201 hrtimer_cancel(&df->idle_work.timer);
202 kthread_cancel_work_sync(&df->idle_work.work);
205 static void cancel_boost_work(struct msm_gpu_devfreq *df)
207 hrtimer_cancel(&df->boost_work.timer);
208 kthread_cancel_work_sync(&df->boost_work.work);
211 void msm_devfreq_cleanup(struct msm_gpu *gpu)
213 struct msm_gpu_devfreq *df = &gpu->devfreq;
215 if (!has_devfreq(gpu))
218 devfreq_cooling_unregister(gpu->cooling);
219 dev_pm_qos_remove_request(&df->boost_freq);
222 void msm_devfreq_resume(struct msm_gpu *gpu)
224 struct msm_gpu_devfreq *df = &gpu->devfreq;
225 unsigned long sample_rate;
227 if (!has_devfreq(gpu))
230 mutex_lock(&df->lock);
231 df->busy_cycles = gpu->funcs->gpu_busy(gpu, &sample_rate);
232 df->time = ktime_get();
233 df->suspended = false;
234 mutex_unlock(&df->lock);
236 devfreq_resume_device(df->devfreq);
239 void msm_devfreq_suspend(struct msm_gpu *gpu)
241 struct msm_gpu_devfreq *df = &gpu->devfreq;
243 if (!has_devfreq(gpu))
246 mutex_lock(&df->lock);
247 df->suspended = true;
248 mutex_unlock(&df->lock);
250 devfreq_suspend_device(df->devfreq);
252 cancel_idle_work(df);
253 cancel_boost_work(df);
256 static void msm_devfreq_boost_work(struct kthread_work *work)
258 struct msm_gpu_devfreq *df = container_of(work,
259 struct msm_gpu_devfreq, boost_work.work);
261 dev_pm_qos_update_request(&df->boost_freq, 0);
264 void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor)
266 struct msm_gpu_devfreq *df = &gpu->devfreq;
269 if (!has_devfreq(gpu))
272 freq = get_freq(gpu);
276 * A nice little trap is that PM QoS operates in terms of KHz,
277 * while devfreq operates in terms of Hz:
279 do_div(freq, HZ_PER_KHZ);
281 dev_pm_qos_update_request(&df->boost_freq, freq);
283 msm_hrtimer_queue_work(&df->boost_work,
284 ms_to_ktime(msm_devfreq_profile.polling_ms),
288 void msm_devfreq_active(struct msm_gpu *gpu)
290 struct msm_gpu_devfreq *df = &gpu->devfreq;
291 unsigned int idle_time;
292 unsigned long target_freq;
294 if (!has_devfreq(gpu))
298 * Cancel any pending transition to idle frequency:
300 cancel_idle_work(df);
303 * Hold devfreq lock to synchronize with get_dev_status()/
306 mutex_lock(&df->devfreq->lock);
308 target_freq = df->idle_freq;
310 idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
315 * We could have become active again before the idle work had a
316 * chance to run, in which case the df->idle_freq would have
317 * still been zero. In this case, no need to change freq.
320 msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
322 mutex_unlock(&df->devfreq->lock);
325 * If we've been idle for a significant fraction of a polling
326 * interval, then we won't meet the threshold of busyness for
327 * the governor to ramp up the freq.. so give some boost
329 if (idle_time > msm_devfreq_profile.polling_ms) {
330 msm_devfreq_boost(gpu, 2);
335 static void msm_devfreq_idle_work(struct kthread_work *work)
337 struct msm_gpu_devfreq *df = container_of(work,
338 struct msm_gpu_devfreq, idle_work.work);
339 struct msm_gpu *gpu = container_of(df, struct msm_gpu, devfreq);
340 struct msm_drm_private *priv = gpu->dev->dev_private;
341 unsigned long idle_freq, target_freq = 0;
344 * Hold devfreq lock to synchronize with get_dev_status()/
347 mutex_lock(&df->devfreq->lock);
349 idle_freq = get_freq(gpu);
351 if (priv->gpu_clamp_to_idle)
352 msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
354 df->idle_time = ktime_get();
355 df->idle_freq = idle_freq;
357 mutex_unlock(&df->devfreq->lock);
360 void msm_devfreq_idle(struct msm_gpu *gpu)
362 struct msm_gpu_devfreq *df = &gpu->devfreq;
364 if (!has_devfreq(gpu))
367 msm_hrtimer_queue_work(&df->idle_work, ms_to_ktime(1),