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 #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 void msm_devfreq_cleanup(struct msm_gpu *gpu)
137 {
138 	struct msm_gpu_devfreq *df = &gpu->devfreq;
139 
140 	devfreq_cooling_unregister(gpu->cooling);
141 	dev_pm_qos_remove_request(&df->boost_freq);
142 	dev_pm_qos_remove_request(&df->idle_freq);
143 }
144 
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 
153 void msm_devfreq_suspend(struct msm_gpu *gpu)
154 {
155 	devfreq_suspend_device(gpu->devfreq.devfreq);
156 }
157 
158 static void msm_devfreq_boost_work(struct kthread_work *work)
159 {
160 	struct msm_gpu_devfreq *df = container_of(work,
161 			struct msm_gpu_devfreq, boost_work.work);
162 
163 	dev_pm_qos_update_request(&df->boost_freq, 0);
164 }
165 
166 void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor)
167 {
168 	struct msm_gpu_devfreq *df = &gpu->devfreq;
169 	uint64_t freq;
170 
171 	freq = get_freq(gpu);
172 	freq *= factor;
173 
174 	/*
175 	 * A nice little trap is that PM QoS operates in terms of KHz,
176 	 * while devfreq operates in terms of Hz:
177 	 */
178 	do_div(freq, HZ_PER_KHZ);
179 
180 	dev_pm_qos_update_request(&df->boost_freq, freq);
181 
182 	msm_hrtimer_queue_work(&df->boost_work,
183 			       ms_to_ktime(msm_devfreq_profile.polling_ms),
184 			       HRTIMER_MODE_REL);
185 }
186 
187 void msm_devfreq_active(struct msm_gpu *gpu)
188 {
189 	struct msm_gpu_devfreq *df = &gpu->devfreq;
190 	struct devfreq_dev_status status;
191 	unsigned int idle_time;
192 
193 	if (!df->devfreq)
194 		return;
195 
196 	/*
197 	 * Cancel any pending transition to idle frequency:
198 	 */
199 	hrtimer_cancel(&df->idle_work.timer);
200 
201 	idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
202 
203 	/*
204 	 * If we've been idle for a significant fraction of a polling
205 	 * interval, then we won't meet the threshold of busyness for
206 	 * the governor to ramp up the freq.. so give some boost
207 	 */
208 	if (idle_time > msm_devfreq_profile.polling_ms) {
209 		msm_devfreq_boost(gpu, 2);
210 	}
211 
212 	dev_pm_qos_update_request(&df->idle_freq,
213 				  PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
214 
215 	/*
216 	 * Reset the polling interval so we aren't inconsistent
217 	 * about freq vs busy/total cycles
218 	 */
219 	msm_devfreq_get_dev_status(&gpu->pdev->dev, &status);
220 }
221 
222 
223 static void msm_devfreq_idle_work(struct kthread_work *work)
224 {
225 	struct msm_gpu_devfreq *df = container_of(work,
226 			struct msm_gpu_devfreq, idle_work.work);
227 	struct msm_gpu *gpu = container_of(df, struct msm_gpu, devfreq);
228 
229 	df->idle_time = ktime_get();
230 
231 	if (gpu->clamp_to_idle)
232 		dev_pm_qos_update_request(&df->idle_freq, 0);
233 }
234 
235 void msm_devfreq_idle(struct msm_gpu *gpu)
236 {
237 	struct msm_gpu_devfreq *df = &gpu->devfreq;
238 
239 	if (!df->devfreq)
240 		return;
241 
242 	msm_hrtimer_queue_work(&df->idle_work, ms_to_ktime(1),
243 			       HRTIMER_MODE_REL);
244 }
245