• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
msm_devfreq_init(struct msm_gpu * gpu)96 void msm_devfreq_init(struct msm_gpu *gpu)
97 {
98 	/* We need target support to do devfreq */
99 	if (!gpu->funcs->gpu_busy)
100 		return;
101 
102 	msm_devfreq_profile.initial_freq = gpu->fast_rate;
103 
104 	/*
105 	 * Don't set the freq_table or max_state and let devfreq build the table
106 	 * from OPP
107 	 * After a deferred probe, these may have be left to non-zero values,
108 	 * so set them back to zero before creating the devfreq device
109 	 */
110 	msm_devfreq_profile.freq_table = NULL;
111 	msm_devfreq_profile.max_state = 0;
112 
113 	gpu->devfreq.devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
114 			&msm_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
115 			NULL);
116 
117 	if (IS_ERR(gpu->devfreq.devfreq)) {
118 		DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
119 		gpu->devfreq.devfreq = NULL;
120 		return;
121 	}
122 
123 	devfreq_suspend_device(gpu->devfreq.devfreq);
124 
125 	gpu->cooling = of_devfreq_cooling_register(gpu->pdev->dev.of_node,
126 			gpu->devfreq.devfreq);
127 	if (IS_ERR(gpu->cooling)) {
128 		DRM_DEV_ERROR(&gpu->pdev->dev,
129 				"Couldn't register GPU cooling device\n");
130 		gpu->cooling = NULL;
131 	}
132 }
133 
msm_devfreq_cleanup(struct msm_gpu * gpu)134 void msm_devfreq_cleanup(struct msm_gpu *gpu)
135 {
136 	devfreq_cooling_unregister(gpu->cooling);
137 }
138 
msm_devfreq_resume(struct msm_gpu * gpu)139 void msm_devfreq_resume(struct msm_gpu *gpu)
140 {
141 	gpu->devfreq.busy_cycles = 0;
142 	gpu->devfreq.time = ktime_get();
143 
144 	devfreq_resume_device(gpu->devfreq.devfreq);
145 }
146 
msm_devfreq_suspend(struct msm_gpu * gpu)147 void msm_devfreq_suspend(struct msm_gpu *gpu)
148 {
149 	devfreq_suspend_device(gpu->devfreq.devfreq);
150 }
151 
msm_devfreq_active(struct msm_gpu * gpu)152 void msm_devfreq_active(struct msm_gpu *gpu)
153 {
154 	struct msm_gpu_devfreq *df = &gpu->devfreq;
155 	struct devfreq_dev_status status;
156 	unsigned int idle_time;
157 	unsigned long target_freq = df->idle_freq;
158 
159 	if (!df->devfreq)
160 		return;
161 
162 	/*
163 	 * Hold devfreq lock to synchronize with get_dev_status()/
164 	 * target() callbacks
165 	 */
166 	mutex_lock(&df->devfreq->lock);
167 
168 	idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
169 
170 	/*
171 	 * If we've been idle for a significant fraction of a polling
172 	 * interval, then we won't meet the threshold of busyness for
173 	 * the governor to ramp up the freq.. so give some boost
174 	 */
175 	if (idle_time > msm_devfreq_profile.polling_ms/2) {
176 		target_freq *= 2;
177 	}
178 
179 	df->idle_freq = 0;
180 
181 	msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
182 
183 	/*
184 	 * Reset the polling interval so we aren't inconsistent
185 	 * about freq vs busy/total cycles
186 	 */
187 	msm_devfreq_get_dev_status(&gpu->pdev->dev, &status);
188 
189 	mutex_unlock(&df->devfreq->lock);
190 }
191 
msm_devfreq_idle(struct msm_gpu * gpu)192 void msm_devfreq_idle(struct msm_gpu *gpu)
193 {
194 	struct msm_gpu_devfreq *df = &gpu->devfreq;
195 	unsigned long idle_freq, target_freq = 0;
196 
197 	if (!df->devfreq)
198 		return;
199 
200 	/*
201 	 * Hold devfreq lock to synchronize with get_dev_status()/
202 	 * target() callbacks
203 	 */
204 	mutex_lock(&df->devfreq->lock);
205 
206 	idle_freq = get_freq(gpu);
207 
208 	if (gpu->clamp_to_idle)
209 		msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
210 
211 	df->idle_time = ktime_get();
212 	df->idle_freq = idle_freq;
213 
214 	mutex_unlock(&df->devfreq->lock);
215 }
216