1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
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 
13 /*
14  * Power Management:
15  */
16 
msm_devfreq_target(struct device * dev,unsigned long * freq,u32 flags)17 static int msm_devfreq_target(struct device *dev, unsigned long *freq,
18 		u32 flags)
19 {
20 	struct msm_gpu *gpu = dev_to_gpu(dev);
21 	struct dev_pm_opp *opp;
22 
23 	/*
24 	 * Note that devfreq_recommended_opp() can modify the freq
25 	 * to something that actually is in the opp table:
26 	 */
27 	opp = devfreq_recommended_opp(dev, freq, flags);
28 
29 	/*
30 	 * If the GPU is idle, devfreq is not aware, so just ignore
31 	 * it's requests
32 	 */
33 	if (gpu->devfreq.idle_freq) {
34 		gpu->devfreq.idle_freq = *freq;
35 		dev_pm_opp_put(opp);
36 		return 0;
37 	}
38 
39 	if (IS_ERR(opp))
40 		return PTR_ERR(opp);
41 
42 	trace_msm_gpu_freq_change(dev_pm_opp_get_freq(opp));
43 
44 	if (gpu->funcs->gpu_set_freq)
45 		gpu->funcs->gpu_set_freq(gpu, opp);
46 	else
47 		clk_set_rate(gpu->core_clk, *freq);
48 
49 	dev_pm_opp_put(opp);
50 
51 	return 0;
52 }
53 
get_freq(struct msm_gpu * gpu)54 static unsigned long get_freq(struct msm_gpu *gpu)
55 {
56 	if (gpu->devfreq.idle_freq)
57 		return gpu->devfreq.idle_freq;
58 
59 	if (gpu->funcs->gpu_get_freq)
60 		return gpu->funcs->gpu_get_freq(gpu);
61 
62 	return clk_get_rate(gpu->core_clk);
63 }
64 
msm_devfreq_get_dev_status(struct device * dev,struct devfreq_dev_status * status)65 static int msm_devfreq_get_dev_status(struct device *dev,
66 		struct devfreq_dev_status *status)
67 {
68 	struct msm_gpu *gpu = dev_to_gpu(dev);
69 	ktime_t time;
70 
71 	status->current_frequency = get_freq(gpu);
72 	status->busy_time = gpu->funcs->gpu_busy(gpu);
73 
74 	time = ktime_get();
75 	status->total_time = ktime_us_delta(time, gpu->devfreq.time);
76 	gpu->devfreq.time = time;
77 
78 	return 0;
79 }
80 
msm_devfreq_get_cur_freq(struct device * dev,unsigned long * freq)81 static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
82 {
83 	*freq = get_freq(dev_to_gpu(dev));
84 
85 	return 0;
86 }
87 
88 static struct devfreq_dev_profile msm_devfreq_profile = {
89 	.timer = DEVFREQ_TIMER_DELAYED,
90 	.polling_ms = 50,
91 	.target = msm_devfreq_target,
92 	.get_dev_status = msm_devfreq_get_dev_status,
93 	.get_cur_freq = msm_devfreq_get_cur_freq,
94 };
95 
96 static void msm_devfreq_idle_work(struct kthread_work *work);
97 
msm_devfreq_init(struct msm_gpu * gpu)98 void msm_devfreq_init(struct msm_gpu *gpu)
99 {
100 	struct msm_gpu_devfreq *df = &gpu->devfreq;
101 
102 	/* We need target support to do devfreq */
103 	if (!gpu->funcs->gpu_busy)
104 		return;
105 
106 	msm_devfreq_profile.initial_freq = gpu->fast_rate;
107 
108 	/*
109 	 * Don't set the freq_table or max_state and let devfreq build the table
110 	 * from OPP
111 	 * After a deferred probe, these may have be left to non-zero values,
112 	 * so set them back to zero before creating the devfreq device
113 	 */
114 	msm_devfreq_profile.freq_table = NULL;
115 	msm_devfreq_profile.max_state = 0;
116 
117 	df->devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
118 			&msm_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
119 			NULL);
120 
121 	if (IS_ERR(df->devfreq)) {
122 		DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
123 		df->devfreq = NULL;
124 		return;
125 	}
126 
127 	devfreq_suspend_device(df->devfreq);
128 
129 	gpu->cooling = of_devfreq_cooling_register(gpu->pdev->dev.of_node, df->devfreq);
130 	if (IS_ERR(gpu->cooling)) {
131 		DRM_DEV_ERROR(&gpu->pdev->dev,
132 				"Couldn't register GPU cooling device\n");
133 		gpu->cooling = NULL;
134 	}
135 
136 	msm_hrtimer_work_init(&df->idle_work, gpu->worker, msm_devfreq_idle_work,
137 			      CLOCK_MONOTONIC, HRTIMER_MODE_REL);
138 }
139 
msm_devfreq_cleanup(struct msm_gpu * gpu)140 void msm_devfreq_cleanup(struct msm_gpu *gpu)
141 {
142 	devfreq_cooling_unregister(gpu->cooling);
143 }
144 
msm_devfreq_resume(struct msm_gpu * gpu)145 void msm_devfreq_resume(struct msm_gpu *gpu)
146 {
147 	gpu->devfreq.busy_cycles = 0;
148 	gpu->devfreq.time = ktime_get();
149 
150 	devfreq_resume_device(gpu->devfreq.devfreq);
151 }
152 
msm_devfreq_suspend(struct msm_gpu * gpu)153 void msm_devfreq_suspend(struct msm_gpu *gpu)
154 {
155 	devfreq_suspend_device(gpu->devfreq.devfreq);
156 }
157 
msm_devfreq_active(struct msm_gpu * gpu)158 void msm_devfreq_active(struct msm_gpu *gpu)
159 {
160 	struct msm_gpu_devfreq *df = &gpu->devfreq;
161 	struct devfreq_dev_status status;
162 	unsigned int idle_time;
163 	unsigned long target_freq = df->idle_freq;
164 
165 	if (!df->devfreq)
166 		return;
167 
168 	/*
169 	 * Cancel any pending transition to idle frequency:
170 	 */
171 	hrtimer_cancel(&df->idle_work.timer);
172 
173 	/*
174 	 * Hold devfreq lock to synchronize with get_dev_status()/
175 	 * target() callbacks
176 	 */
177 	mutex_lock(&df->devfreq->lock);
178 
179 	idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
180 
181 	/*
182 	 * If we've been idle for a significant fraction of a polling
183 	 * interval, then we won't meet the threshold of busyness for
184 	 * the governor to ramp up the freq.. so give some boost
185 	 */
186 	if (idle_time > msm_devfreq_profile.polling_ms/2) {
187 		target_freq *= 2;
188 	}
189 
190 	df->idle_freq = 0;
191 
192 	msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
193 
194 	/*
195 	 * Reset the polling interval so we aren't inconsistent
196 	 * about freq vs busy/total cycles
197 	 */
198 	msm_devfreq_get_dev_status(&gpu->pdev->dev, &status);
199 
200 	mutex_unlock(&df->devfreq->lock);
201 }
202 
203 
msm_devfreq_idle_work(struct kthread_work * work)204 static void msm_devfreq_idle_work(struct kthread_work *work)
205 {
206 	struct msm_gpu_devfreq *df = container_of(work,
207 			struct msm_gpu_devfreq, idle_work.work);
208 	struct msm_gpu *gpu = container_of(df, struct msm_gpu, devfreq);
209 	unsigned long idle_freq, target_freq = 0;
210 
211 	/*
212 	 * Hold devfreq lock to synchronize with get_dev_status()/
213 	 * target() callbacks
214 	 */
215 	mutex_lock(&df->devfreq->lock);
216 
217 	idle_freq = get_freq(gpu);
218 
219 	if (gpu->clamp_to_idle)
220 		msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
221 
222 	df->idle_time = ktime_get();
223 	df->idle_freq = idle_freq;
224 
225 	mutex_unlock(&df->devfreq->lock);
226 }
227 
msm_devfreq_idle(struct msm_gpu * gpu)228 void msm_devfreq_idle(struct msm_gpu *gpu)
229 {
230 	struct msm_gpu_devfreq *df = &gpu->devfreq;
231 
232 	if (!df->devfreq)
233 		return;
234 
235 	msm_hrtimer_queue_work(&df->idle_work, ms_to_ktime(1),
236 			       HRTIMER_MODE_REL);
237 }
238