]> Git Repo - linux.git/blob - drivers/gpu/drm/msm/msm_gpu_devfreq.c
Merge tag 'fs.rt.v5.18' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner...
[linux.git] / drivers / gpu / drm / msm / msm_gpu_devfreq.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <[email protected]>
5  */
6
7 #include "msm_gpu.h"
8 #include "msm_gpu_trace.h"
9
10 #include <linux/devfreq.h>
11 #include <linux/devfreq_cooling.h>
12 #include <linux/units.h>
13
14 /*
15  * Power Management:
16  */
17
18 static int msm_devfreq_target(struct device *dev, unsigned long *freq,
19                 u32 flags)
20 {
21         struct msm_gpu *gpu = dev_to_gpu(dev);
22         struct dev_pm_opp *opp;
23
24         /*
25          * Note that devfreq_recommended_opp() can modify the freq
26          * to something that actually is in the opp table:
27          */
28         opp = devfreq_recommended_opp(dev, freq, flags);
29         if (IS_ERR(opp))
30                 return PTR_ERR(opp);
31
32         trace_msm_gpu_freq_change(dev_pm_opp_get_freq(opp));
33
34         if (gpu->funcs->gpu_set_freq)
35                 gpu->funcs->gpu_set_freq(gpu, opp);
36         else
37                 clk_set_rate(gpu->core_clk, *freq);
38
39         dev_pm_opp_put(opp);
40
41         return 0;
42 }
43
44 static unsigned long get_freq(struct msm_gpu *gpu)
45 {
46         if (gpu->funcs->gpu_get_freq)
47                 return gpu->funcs->gpu_get_freq(gpu);
48
49         return clk_get_rate(gpu->core_clk);
50 }
51
52 static int msm_devfreq_get_dev_status(struct device *dev,
53                 struct devfreq_dev_status *status)
54 {
55         struct msm_gpu *gpu = dev_to_gpu(dev);
56         ktime_t time;
57
58         status->current_frequency = get_freq(gpu);
59         status->busy_time = gpu->funcs->gpu_busy(gpu);
60
61         time = ktime_get();
62         status->total_time = ktime_us_delta(time, gpu->devfreq.time);
63         gpu->devfreq.time = time;
64
65         return 0;
66 }
67
68 static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
69 {
70         *freq = get_freq(dev_to_gpu(dev));
71
72         return 0;
73 }
74
75 static struct devfreq_dev_profile msm_devfreq_profile = {
76         .timer = DEVFREQ_TIMER_DELAYED,
77         .polling_ms = 50,
78         .target = msm_devfreq_target,
79         .get_dev_status = msm_devfreq_get_dev_status,
80         .get_cur_freq = msm_devfreq_get_cur_freq,
81 };
82
83 static void msm_devfreq_boost_work(struct kthread_work *work);
84 static void msm_devfreq_idle_work(struct kthread_work *work);
85
86 void msm_devfreq_init(struct msm_gpu *gpu)
87 {
88         struct msm_gpu_devfreq *df = &gpu->devfreq;
89
90         /* We need target support to do devfreq */
91         if (!gpu->funcs->gpu_busy)
92                 return;
93
94         dev_pm_qos_add_request(&gpu->pdev->dev, &df->idle_freq,
95                                DEV_PM_QOS_MAX_FREQUENCY,
96                                PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
97         dev_pm_qos_add_request(&gpu->pdev->dev, &df->boost_freq,
98                                DEV_PM_QOS_MIN_FREQUENCY, 0);
99
100         msm_devfreq_profile.initial_freq = gpu->fast_rate;
101
102         /*
103          * Don't set the freq_table or max_state and let devfreq build the table
104          * from OPP
105          * After a deferred probe, these may have be left to non-zero values,
106          * so set them back to zero before creating the devfreq device
107          */
108         msm_devfreq_profile.freq_table = NULL;
109         msm_devfreq_profile.max_state = 0;
110
111         df->devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
112                         &msm_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
113                         NULL);
114
115         if (IS_ERR(df->devfreq)) {
116                 DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
117                 df->devfreq = NULL;
118                 return;
119         }
120
121         devfreq_suspend_device(df->devfreq);
122
123         gpu->cooling = of_devfreq_cooling_register(gpu->pdev->dev.of_node, df->devfreq);
124         if (IS_ERR(gpu->cooling)) {
125                 DRM_DEV_ERROR(&gpu->pdev->dev,
126                                 "Couldn't register GPU cooling device\n");
127                 gpu->cooling = NULL;
128         }
129
130         msm_hrtimer_work_init(&df->boost_work, gpu->worker, msm_devfreq_boost_work,
131                               CLOCK_MONOTONIC, HRTIMER_MODE_REL);
132         msm_hrtimer_work_init(&df->idle_work, gpu->worker, msm_devfreq_idle_work,
133                               CLOCK_MONOTONIC, HRTIMER_MODE_REL);
134 }
135
136 static void cancel_idle_work(struct msm_gpu_devfreq *df)
137 {
138         hrtimer_cancel(&df->idle_work.timer);
139         kthread_cancel_work_sync(&df->idle_work.work);
140 }
141
142 static void cancel_boost_work(struct msm_gpu_devfreq *df)
143 {
144         hrtimer_cancel(&df->boost_work.timer);
145         kthread_cancel_work_sync(&df->boost_work.work);
146 }
147
148 void msm_devfreq_cleanup(struct msm_gpu *gpu)
149 {
150         struct msm_gpu_devfreq *df = &gpu->devfreq;
151
152         devfreq_cooling_unregister(gpu->cooling);
153         dev_pm_qos_remove_request(&df->boost_freq);
154         dev_pm_qos_remove_request(&df->idle_freq);
155 }
156
157 void msm_devfreq_resume(struct msm_gpu *gpu)
158 {
159         gpu->devfreq.busy_cycles = 0;
160         gpu->devfreq.time = ktime_get();
161
162         devfreq_resume_device(gpu->devfreq.devfreq);
163 }
164
165 void msm_devfreq_suspend(struct msm_gpu *gpu)
166 {
167         struct msm_gpu_devfreq *df = &gpu->devfreq;
168
169         devfreq_suspend_device(df->devfreq);
170
171         cancel_idle_work(df);
172         cancel_boost_work(df);
173 }
174
175 static void msm_devfreq_boost_work(struct kthread_work *work)
176 {
177         struct msm_gpu_devfreq *df = container_of(work,
178                         struct msm_gpu_devfreq, boost_work.work);
179
180         dev_pm_qos_update_request(&df->boost_freq, 0);
181 }
182
183 void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor)
184 {
185         struct msm_gpu_devfreq *df = &gpu->devfreq;
186         uint64_t freq;
187
188         freq = get_freq(gpu);
189         freq *= factor;
190
191         /*
192          * A nice little trap is that PM QoS operates in terms of KHz,
193          * while devfreq operates in terms of Hz:
194          */
195         do_div(freq, HZ_PER_KHZ);
196
197         dev_pm_qos_update_request(&df->boost_freq, freq);
198
199         msm_hrtimer_queue_work(&df->boost_work,
200                                ms_to_ktime(msm_devfreq_profile.polling_ms),
201                                HRTIMER_MODE_REL);
202 }
203
204 void msm_devfreq_active(struct msm_gpu *gpu)
205 {
206         struct msm_gpu_devfreq *df = &gpu->devfreq;
207         struct devfreq_dev_status status;
208         unsigned int idle_time;
209
210         if (!df->devfreq)
211                 return;
212
213         /*
214          * Cancel any pending transition to idle frequency:
215          */
216         cancel_idle_work(df);
217
218         idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
219
220         /*
221          * If we've been idle for a significant fraction of a polling
222          * interval, then we won't meet the threshold of busyness for
223          * the governor to ramp up the freq.. so give some boost
224          */
225         if (idle_time > msm_devfreq_profile.polling_ms) {
226                 msm_devfreq_boost(gpu, 2);
227         }
228
229         dev_pm_qos_update_request(&df->idle_freq,
230                                   PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
231
232         /*
233          * Reset the polling interval so we aren't inconsistent
234          * about freq vs busy/total cycles
235          */
236         msm_devfreq_get_dev_status(&gpu->pdev->dev, &status);
237 }
238
239
240 static void msm_devfreq_idle_work(struct kthread_work *work)
241 {
242         struct msm_gpu_devfreq *df = container_of(work,
243                         struct msm_gpu_devfreq, idle_work.work);
244         struct msm_gpu *gpu = container_of(df, struct msm_gpu, devfreq);
245
246         df->idle_time = ktime_get();
247
248         if (gpu->clamp_to_idle)
249                 dev_pm_qos_update_request(&df->idle_freq, 0);
250 }
251
252 void msm_devfreq_idle(struct msm_gpu *gpu)
253 {
254         struct msm_gpu_devfreq *df = &gpu->devfreq;
255
256         if (!df->devfreq)
257                 return;
258
259         msm_hrtimer_queue_work(&df->idle_work, ms_to_ktime(1),
260                                HRTIMER_MODE_REL);
261 }
This page took 0.05383 seconds and 4 git commands to generate.