xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/backend/gpu/mali_kbase_devfreq.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2014-2022 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU licence.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  * SPDX-License-Identifier: GPL-2.0
21  *
22  */
23 
24 #include <mali_kbase.h>
25 #include <tl/mali_kbase_tracepoints.h>
26 #include <backend/gpu/mali_kbase_devfreq.h>
27 #include <backend/gpu/mali_kbase_pm_internal.h>
28 
29 #include <linux/of.h>
30 #include <linux/clk.h>
31 #include <linux/clk-provider.h>
32 #include <linux/devfreq.h>
33 #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
34 #include <linux/devfreq_cooling.h>
35 #endif
36 
37 #include <linux/version.h>
38 #include <linux/pm_opp.h>
39 #include <linux/pm_runtime.h>
40 #include "mali_kbase_devfreq.h"
41 
42 #include <soc/rockchip/rockchip_ipa.h>
43 #include <soc/rockchip/rockchip_opp_select.h>
44 #include <soc/rockchip/rockchip_system_monitor.h>
45 
46 static struct devfreq_simple_ondemand_data ondemand_data;
47 
48 static struct monitor_dev_profile mali_mdevp = {
49 	.type = MONITOR_TYPE_DEV,
50 	.low_temp_adjust = rockchip_monitor_dev_low_temp_adjust,
51 	.high_temp_adjust = rockchip_monitor_dev_high_temp_adjust,
52 	.update_volt = rockchip_monitor_check_rate_volt,
53 };
54 
55 /**
56  * get_voltage() - Get the voltage value corresponding to the nominal frequency
57  *                 used by devfreq.
58  * @kbdev:    Device pointer
59  * @freq:     Nominal frequency in Hz passed by devfreq.
60  *
61  * This function will be called only when the opp table which is compatible with
62  * "operating-points-v2-mali", is not present in the devicetree for GPU device.
63  *
64  * Return: Voltage value in micro volts, 0 in case of error.
65  */
get_voltage(struct kbase_device * kbdev,unsigned long freq)66 static unsigned long get_voltage(struct kbase_device *kbdev, unsigned long freq)
67 {
68 	struct dev_pm_opp *opp;
69 	unsigned long voltage = 0;
70 
71 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
72 	rcu_read_lock();
73 #endif
74 
75 	opp = dev_pm_opp_find_freq_exact(kbdev->dev, freq, true);
76 
77 	if (IS_ERR_OR_NULL(opp))
78 		dev_err(kbdev->dev, "Failed to get opp (%d)\n", PTR_ERR_OR_ZERO(opp));
79 	else {
80 		voltage = dev_pm_opp_get_voltage(opp);
81 #if KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE
82 		dev_pm_opp_put(opp);
83 #endif
84 	}
85 
86 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
87 	rcu_read_unlock();
88 #endif
89 
90 	/* Return the voltage in micro volts */
91 	return voltage;
92 }
93 
kbase_devfreq_opp_translate(struct kbase_device * kbdev,unsigned long freq,u64 * core_mask,unsigned long * freqs,unsigned long * volts)94 void kbase_devfreq_opp_translate(struct kbase_device *kbdev, unsigned long freq,
95 	u64 *core_mask, unsigned long *freqs, unsigned long *volts)
96 {
97 	unsigned int i;
98 
99 	for (i = 0; i < kbdev->num_opps; i++) {
100 		if (kbdev->devfreq_table[i].opp_freq == freq) {
101 			unsigned int j;
102 
103 			*core_mask = kbdev->devfreq_table[i].core_mask;
104 			for (j = 0; j < kbdev->nr_clocks; j++) {
105 				freqs[j] =
106 					kbdev->devfreq_table[i].real_freqs[j];
107 				volts[j] =
108 					kbdev->devfreq_table[i].opp_volts[j];
109 			}
110 
111 			break;
112 		}
113 	}
114 
115 	/* If failed to find OPP, return all cores enabled
116 	 * and nominal frequency and the corresponding voltage.
117 	 */
118 	if (i == kbdev->num_opps) {
119 		unsigned long voltage = get_voltage(kbdev, freq);
120 
121 		*core_mask = kbdev->gpu_props.props.raw_props.shader_present;
122 
123 		for (i = 0; i < kbdev->nr_clocks; i++) {
124 			freqs[i] = freq;
125 			volts[i] = voltage;
126 		}
127 	}
128 }
129 
kbase_devfreq_opp_helper(struct dev_pm_set_opp_data * data)130 int kbase_devfreq_opp_helper(struct dev_pm_set_opp_data *data)
131 {
132 	struct device *dev = data->dev;
133 	struct dev_pm_opp_supply *old_supply_vdd = &data->old_opp.supplies[0];
134 	struct dev_pm_opp_supply *new_supply_vdd = &data->new_opp.supplies[0];
135 	struct regulator *vdd_reg = data->regulators[0];
136 	struct dev_pm_opp_supply *old_supply_mem;
137 	struct dev_pm_opp_supply *new_supply_mem;
138 	struct regulator *mem_reg;
139 	struct clk *clk = data->clk;
140 	struct kbase_device *kbdev = dev_get_drvdata(dev);
141 	struct rockchip_opp_info *opp_info = &kbdev->opp_info;
142 	unsigned long old_freq = data->old_opp.rate;
143 	unsigned long new_freq = data->new_opp.rate;
144 	unsigned int reg_count = data->regulator_count;
145 	bool is_set_rm = true;
146 	bool is_set_clk = true;
147 	u32 target_rm = UINT_MAX;
148 	int ret = 0;
149 
150 	if (reg_count > 1) {
151 		old_supply_mem = &data->old_opp.supplies[1];
152 		new_supply_mem = &data->new_opp.supplies[1];
153 		mem_reg = data->regulators[1];
154 	}
155 
156 	if (!pm_runtime_active(dev)) {
157 		is_set_rm = false;
158 		if (opp_info->scmi_clk)
159 			is_set_clk = false;
160 	}
161 
162 	ret = clk_bulk_prepare_enable(opp_info->num_clks,  opp_info->clks);
163 	if (ret) {
164 		dev_err(dev, "failed to enable opp clks\n");
165 		return ret;
166 	}
167 	rockchip_get_read_margin(dev, opp_info, new_supply_vdd->u_volt,
168 				 &target_rm);
169 
170 	/* Change frequency */
171 	dev_dbg(dev, "switching OPP: %lu Hz --> %lu Hz\n", old_freq, new_freq);
172 	/* Scaling up? Scale voltage before frequency */
173 	if (new_freq >= old_freq) {
174 		rockchip_set_intermediate_rate(dev, opp_info, clk, old_freq,
175 					       new_freq, true, is_set_clk);
176 		if (reg_count > 1) {
177 			ret = regulator_set_voltage(mem_reg,
178 						    new_supply_mem->u_volt,
179 						    INT_MAX);
180 			if (ret) {
181 				dev_err(dev, "failed to set volt %lu uV for mem reg\n",
182 					new_supply_mem->u_volt);
183 				goto restore_voltage;
184 			}
185 		}
186 		ret = regulator_set_voltage(vdd_reg, new_supply_vdd->u_volt,
187 					    INT_MAX);
188 		if (ret) {
189 			dev_err(dev, "failed to set volt %lu uV for vdd reg\n",
190 				new_supply_vdd->u_volt);
191 			goto restore_voltage;
192 		}
193 		rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm);
194 		if (is_set_clk && clk_set_rate(clk, new_freq)) {
195 			ret = -EINVAL;
196 			dev_err(dev, "failed to set clk rate\n");
197 			goto restore_rm;
198 		}
199 	/* Scaling down? Scale voltage after frequency */
200 	} else {
201 		rockchip_set_intermediate_rate(dev, opp_info, clk, old_freq,
202 					       new_freq, false, is_set_clk);
203 		rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm);
204 		if (is_set_clk && clk_set_rate(clk, new_freq)) {
205 			ret = -EINVAL;
206 			dev_err(dev, "failed to set clk rate\n");
207 			goto restore_rm;
208 		}
209 		ret = regulator_set_voltage(vdd_reg, new_supply_vdd->u_volt,
210 					    INT_MAX);
211 		if (ret) {
212 			dev_err(dev, "failed to set volt %lu uV for vdd reg\n",
213 				new_supply_vdd->u_volt);
214 			goto restore_freq;
215 		}
216 		if (reg_count > 1) {
217 			ret = regulator_set_voltage(mem_reg,
218 						    new_supply_mem->u_volt,
219 						    INT_MAX);
220 			if (ret) {
221 				dev_err(dev, "failed to set volt %lu uV for mem reg\n",
222 					new_supply_mem->u_volt);
223 				goto restore_voltage;
224 			}
225 		}
226 	}
227 
228 	clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks);
229 
230 	return 0;
231 
232 restore_freq:
233 	if (is_set_clk && clk_set_rate(clk, old_freq))
234 		dev_err(dev, "failed to restore old-freq %lu Hz\n", old_freq);
235 restore_rm:
236 	rockchip_get_read_margin(dev, opp_info, old_supply_vdd->u_volt,
237 				 &target_rm);
238 	rockchip_set_read_margin(dev, opp_info, opp_info->target_rm, is_set_rm);
239 restore_voltage:
240 	if (reg_count > 1 && old_supply_mem->u_volt)
241 		regulator_set_voltage(mem_reg, old_supply_mem->u_volt, INT_MAX);
242 	regulator_set_voltage(vdd_reg, old_supply_vdd->u_volt, INT_MAX);
243 	clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks);
244 
245 	return ret;
246 }
247 
248 static int
kbase_devfreq_target(struct device * dev,unsigned long * freq,u32 flags)249 kbase_devfreq_target(struct device *dev, unsigned long *freq, u32 flags)
250 {
251 	struct kbase_device *kbdev = dev_get_drvdata(dev);
252 	struct dev_pm_opp *opp;
253 	int ret = 0;
254 
255 	if (!mali_mdevp.is_checked)
256 		return -EINVAL;
257 
258 	opp = devfreq_recommended_opp(dev, freq, flags);
259 	if (IS_ERR(opp))
260 		return PTR_ERR(opp);
261 	dev_pm_opp_put(opp);
262 
263 	if (*freq == kbdev->current_nominal_freq)
264 		return 0;
265 	rockchip_monitor_volt_adjust_lock(kbdev->mdev_info);
266 	ret = dev_pm_opp_set_rate(dev, *freq);
267 	if (!ret) {
268 		kbdev->current_nominal_freq = *freq;
269 		KBASE_TLSTREAM_AUX_DEVFREQ_TARGET(kbdev, (u64)*freq);
270 	}
271 	rockchip_monitor_volt_adjust_unlock(kbdev->mdev_info);
272 
273 	return ret;
274 }
275 
kbase_devfreq_force_freq(struct kbase_device * kbdev,unsigned long freq)276 void kbase_devfreq_force_freq(struct kbase_device *kbdev, unsigned long freq)
277 {
278 	unsigned long target_freq = freq;
279 
280 	kbase_devfreq_target(kbdev->dev, &target_freq, 0);
281 }
282 
283 static int
kbase_devfreq_cur_freq(struct device * dev,unsigned long * freq)284 kbase_devfreq_cur_freq(struct device *dev, unsigned long *freq)
285 {
286 	struct kbase_device *kbdev = dev_get_drvdata(dev);
287 
288 	*freq = kbdev->current_nominal_freq;
289 
290 	return 0;
291 }
292 
293 static int
kbase_devfreq_status(struct device * dev,struct devfreq_dev_status * stat)294 kbase_devfreq_status(struct device *dev, struct devfreq_dev_status *stat)
295 {
296 	struct kbase_device *kbdev = dev_get_drvdata(dev);
297 	struct kbasep_pm_metrics diff;
298 
299 	kbase_pm_get_dvfs_metrics(kbdev, &kbdev->last_devfreq_metrics, &diff);
300 
301 	stat->busy_time = diff.time_busy;
302 	stat->total_time = diff.time_busy + diff.time_idle;
303 	stat->current_frequency = kbdev->current_nominal_freq;
304 	stat->private_data = NULL;
305 
306 #if MALI_USE_CSF && defined CONFIG_DEVFREQ_THERMAL
307 	if (!kbdev->dfc_power.dyn_power_coeff)
308 		kbase_ipa_reset_data(kbdev);
309 #endif
310 
311 	return 0;
312 }
313 
kbase_devfreq_init_freq_table(struct kbase_device * kbdev,struct devfreq_dev_profile * dp)314 static int kbase_devfreq_init_freq_table(struct kbase_device *kbdev,
315 		struct devfreq_dev_profile *dp)
316 {
317 	int count;
318 	int i = 0;
319 	unsigned long freq;
320 	struct dev_pm_opp *opp;
321 
322 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
323 	rcu_read_lock();
324 #endif
325 	count = dev_pm_opp_get_opp_count(kbdev->dev);
326 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
327 	rcu_read_unlock();
328 #endif
329 	if (count < 0)
330 		return count;
331 
332 	dp->freq_table = kmalloc_array(count, sizeof(dp->freq_table[0]),
333 				GFP_KERNEL);
334 	if (!dp->freq_table)
335 		return -ENOMEM;
336 
337 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
338 	rcu_read_lock();
339 #endif
340 	for (i = 0, freq = ULONG_MAX; i < count; i++, freq--) {
341 		opp = dev_pm_opp_find_freq_floor(kbdev->dev, &freq);
342 		if (IS_ERR(opp))
343 			break;
344 #if KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE
345 		dev_pm_opp_put(opp);
346 #endif /* KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE */
347 
348 		dp->freq_table[i] = freq;
349 	}
350 #if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
351 	rcu_read_unlock();
352 #endif
353 
354 	if (count != i)
355 		dev_warn(kbdev->dev, "Unable to enumerate all OPPs (%d!=%d\n",
356 				count, i);
357 
358 	dp->max_state = i;
359 
360 
361 	/* Have the lowest clock as suspend clock.
362 	 * It may be overridden by 'opp-mali-errata-1485982'.
363 	 */
364 	if (kbdev->pm.backend.gpu_clock_slow_down_wa) {
365 		freq = 0;
366 		opp = dev_pm_opp_find_freq_ceil(kbdev->dev, &freq);
367 		if (IS_ERR(opp)) {
368 			dev_err(kbdev->dev, "failed to find slowest clock");
369 			return 0;
370 		}
371 		dev_pm_opp_put(opp);
372 		dev_info(kbdev->dev, "suspend clock %lu from slowest", freq);
373 		kbdev->pm.backend.gpu_clock_suspend_freq = freq;
374 	}
375 
376 	return 0;
377 }
378 
kbase_devfreq_term_freq_table(struct kbase_device * kbdev)379 static void kbase_devfreq_term_freq_table(struct kbase_device *kbdev)
380 {
381 	struct devfreq_dev_profile *dp = &kbdev->devfreq_profile;
382 
383 	kfree(dp->freq_table);
384 	dp->freq_table = NULL;
385 }
386 
kbase_devfreq_term_core_mask_table(struct kbase_device * kbdev)387 static void kbase_devfreq_term_core_mask_table(struct kbase_device *kbdev)
388 {
389 	kfree(kbdev->devfreq_table);
390 	kbdev->devfreq_table = NULL;
391 }
392 
kbase_devfreq_exit(struct device * dev)393 static void kbase_devfreq_exit(struct device *dev)
394 {
395 	struct kbase_device *kbdev = dev_get_drvdata(dev);
396 
397 	if (kbdev)
398 		kbase_devfreq_term_freq_table(kbdev);
399 }
400 
kbasep_devfreq_read_suspend_clock(struct kbase_device * kbdev,struct device_node * node)401 static void kbasep_devfreq_read_suspend_clock(struct kbase_device *kbdev,
402 		struct device_node *node)
403 {
404 	u64 freq = 0;
405 	int err = 0;
406 
407 	/* Check if this node is the opp entry having 'opp-mali-errata-1485982'
408 	 * to get the suspend clock, otherwise skip it.
409 	 */
410 	if (!of_property_read_bool(node, "opp-mali-errata-1485982"))
411 		return;
412 
413 	/* In kbase DevFreq, the clock will be read from 'opp-hz'
414 	 * and translated into the actual clock by opp_translate.
415 	 *
416 	 * In customer DVFS, the clock will be read from 'opp-hz-real'
417 	 * for clk driver. If 'opp-hz-real' does not exist,
418 	 * read from 'opp-hz'.
419 	 */
420 	if (IS_ENABLED(CONFIG_MALI_BIFROST_DEVFREQ))
421 		err = of_property_read_u64(node, "opp-hz", &freq);
422 	else {
423 		if (of_property_read_u64(node, "opp-hz-real", &freq))
424 			err = of_property_read_u64(node, "opp-hz", &freq);
425 	}
426 
427 	if (WARN_ON(err || !freq))
428 		return;
429 
430 	kbdev->pm.backend.gpu_clock_suspend_freq = freq;
431 	dev_info(kbdev->dev,
432 		"suspend clock %llu by opp-mali-errata-1485982", freq);
433 }
434 
kbase_devfreq_init_core_mask_table(struct kbase_device * kbdev)435 static int kbase_devfreq_init_core_mask_table(struct kbase_device *kbdev)
436 {
437 #ifndef CONFIG_OF
438 	/* OPP table initialization requires at least the capability to get
439 	 * regulators and clocks from the device tree, as well as parsing
440 	 * arrays of unsigned integer values.
441 	 *
442 	 * The whole initialization process shall simply be skipped if the
443 	 * minimum capability is not available.
444 	 */
445 	return 0;
446 #else
447 	struct device_node *opp_node = of_parse_phandle(kbdev->dev->of_node,
448 			"operating-points-v2", 0);
449 	struct device_node *node;
450 	int i = 0;
451 	int count;
452 	u64 shader_present = kbdev->gpu_props.props.raw_props.shader_present;
453 
454 	if (!opp_node)
455 		return 0;
456 	if (!of_device_is_compatible(opp_node, "operating-points-v2-mali"))
457 		return 0;
458 
459 	count = dev_pm_opp_get_opp_count(kbdev->dev);
460 	kbdev->devfreq_table = kmalloc_array(count,
461 			sizeof(struct kbase_devfreq_opp), GFP_KERNEL);
462 	if (!kbdev->devfreq_table)
463 		return -ENOMEM;
464 
465 	for_each_available_child_of_node(opp_node, node) {
466 		const void *core_count_p;
467 		u64 core_mask, opp_freq,
468 			real_freqs[BASE_MAX_NR_CLOCKS_REGULATORS];
469 		int err;
470 #if IS_ENABLED(CONFIG_REGULATOR)
471 		u32 opp_volts[BASE_MAX_NR_CLOCKS_REGULATORS];
472 #endif
473 
474 		/* Read suspend clock from opp table */
475 		if (kbdev->pm.backend.gpu_clock_slow_down_wa)
476 			kbasep_devfreq_read_suspend_clock(kbdev, node);
477 
478 		err = of_property_read_u64(node, "opp-hz", &opp_freq);
479 		if (err) {
480 			dev_warn(kbdev->dev, "Failed to read opp-hz property with error %d\n",
481 					err);
482 			continue;
483 		}
484 
485 
486 #if BASE_MAX_NR_CLOCKS_REGULATORS > 1
487 		err = of_property_read_u64_array(node, "opp-hz-real",
488 				real_freqs, kbdev->nr_clocks);
489 #else
490 		WARN_ON(kbdev->nr_clocks != 1);
491 		err = of_property_read_u64(node, "opp-hz-real", real_freqs);
492 #endif
493 		if (err < 0) {
494 			dev_warn(kbdev->dev, "Failed to read opp-hz-real property with error %d\n",
495 					err);
496 			continue;
497 		}
498 #if IS_ENABLED(CONFIG_REGULATOR)
499 		err = of_property_read_u32_array(node,
500 			"opp-microvolt", opp_volts, kbdev->nr_regulators);
501 		if (err < 0) {
502 			dev_warn(kbdev->dev, "Failed to read opp-microvolt property with error %d\n",
503 					err);
504 			continue;
505 		}
506 #endif
507 
508 		if (of_property_read_u64(node, "opp-core-mask", &core_mask))
509 			core_mask = shader_present;
510 		if (core_mask != shader_present && corestack_driver_control) {
511 
512 			dev_warn(kbdev->dev, "Ignoring OPP %llu - Dynamic Core Scaling not supported on this GPU\n",
513 					opp_freq);
514 			continue;
515 		}
516 
517 		core_count_p = of_get_property(node, "opp-core-count", NULL);
518 		if (core_count_p) {
519 			u64 remaining_core_mask =
520 				kbdev->gpu_props.props.raw_props.shader_present;
521 			int core_count = be32_to_cpup(core_count_p);
522 
523 			core_mask = 0;
524 
525 			for (; core_count > 0; core_count--) {
526 				int core = ffs(remaining_core_mask);
527 
528 				if (!core) {
529 					dev_err(kbdev->dev, "OPP has more cores than GPU\n");
530 					return -ENODEV;
531 				}
532 
533 				core_mask |= (1ull << (core-1));
534 				remaining_core_mask &= ~(1ull << (core-1));
535 			}
536 		}
537 
538 		if (!core_mask) {
539 			dev_err(kbdev->dev, "OPP has invalid core mask of 0\n");
540 			return -ENODEV;
541 		}
542 
543 		kbdev->devfreq_table[i].opp_freq = opp_freq;
544 		kbdev->devfreq_table[i].core_mask = core_mask;
545 		if (kbdev->nr_clocks > 0) {
546 			int j;
547 
548 			for (j = 0; j < kbdev->nr_clocks; j++)
549 				kbdev->devfreq_table[i].real_freqs[j] =
550 					real_freqs[j];
551 		}
552 #if IS_ENABLED(CONFIG_REGULATOR)
553 		if (kbdev->nr_regulators > 0) {
554 			int j;
555 
556 			for (j = 0; j < kbdev->nr_regulators; j++)
557 				kbdev->devfreq_table[i].opp_volts[j] =
558 						opp_volts[j];
559 		}
560 #endif
561 
562 		dev_info(kbdev->dev, "OPP %d : opp_freq=%llu core_mask=%llx\n",
563 				i, opp_freq, core_mask);
564 
565 		i++;
566 	}
567 
568 	kbdev->num_opps = i;
569 
570 	return 0;
571 #endif /* CONFIG_OF */
572 }
573 
kbase_devfreq_req_type_name(enum kbase_devfreq_work_type type)574 static const char *kbase_devfreq_req_type_name(enum kbase_devfreq_work_type type)
575 {
576 	const char *p;
577 
578 	switch (type) {
579 	case DEVFREQ_WORK_NONE:
580 		p = "devfreq_none";
581 		break;
582 	case DEVFREQ_WORK_SUSPEND:
583 		p = "devfreq_suspend";
584 		break;
585 	case DEVFREQ_WORK_RESUME:
586 		p = "devfreq_resume";
587 		break;
588 	default:
589 		p = "Unknown devfreq_type";
590 	}
591 	return p;
592 }
593 
kbase_devfreq_suspend_resume_worker(struct work_struct * work)594 static void kbase_devfreq_suspend_resume_worker(struct work_struct *work)
595 {
596 	struct kbase_devfreq_queue_info *info = container_of(work,
597 			struct kbase_devfreq_queue_info, work);
598 	struct kbase_device *kbdev = container_of(info, struct kbase_device,
599 			devfreq_queue);
600 	unsigned long flags;
601 	enum kbase_devfreq_work_type type, acted_type;
602 
603 	spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
604 	type = kbdev->devfreq_queue.req_type;
605 	spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
606 
607 	acted_type = kbdev->devfreq_queue.acted_type;
608 	dev_dbg(kbdev->dev, "Worker handles queued req: %s (acted: %s)\n",
609 		kbase_devfreq_req_type_name(type),
610 		kbase_devfreq_req_type_name(acted_type));
611 	switch (type) {
612 	case DEVFREQ_WORK_SUSPEND:
613 	case DEVFREQ_WORK_RESUME:
614 		if (type != acted_type) {
615 			if (type == DEVFREQ_WORK_RESUME)
616 				devfreq_resume_device(kbdev->devfreq);
617 			else
618 				devfreq_suspend_device(kbdev->devfreq);
619 			dev_dbg(kbdev->dev, "Devfreq transition occured: %s => %s\n",
620 				kbase_devfreq_req_type_name(acted_type),
621 				kbase_devfreq_req_type_name(type));
622 			kbdev->devfreq_queue.acted_type = type;
623 		}
624 		break;
625 	default:
626 		WARN_ON(1);
627 	}
628 }
629 
kbase_devfreq_enqueue_work(struct kbase_device * kbdev,enum kbase_devfreq_work_type work_type)630 void kbase_devfreq_enqueue_work(struct kbase_device *kbdev,
631 				       enum kbase_devfreq_work_type work_type)
632 {
633 	unsigned long flags;
634 
635 	WARN_ON(work_type == DEVFREQ_WORK_NONE);
636 	spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
637 	/* Skip enqueuing a work if workqueue has already been terminated. */
638 	if (likely(kbdev->devfreq_queue.workq)) {
639 		kbdev->devfreq_queue.req_type = work_type;
640 		queue_work(kbdev->devfreq_queue.workq,
641 			   &kbdev->devfreq_queue.work);
642 	}
643 	spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
644 	dev_dbg(kbdev->dev, "Enqueuing devfreq req: %s\n",
645 		kbase_devfreq_req_type_name(work_type));
646 }
647 
kbase_devfreq_work_init(struct kbase_device * kbdev)648 static int kbase_devfreq_work_init(struct kbase_device *kbdev)
649 {
650 	kbdev->devfreq_queue.req_type = DEVFREQ_WORK_NONE;
651 	kbdev->devfreq_queue.acted_type = DEVFREQ_WORK_RESUME;
652 
653 	kbdev->devfreq_queue.workq = alloc_ordered_workqueue("devfreq_workq", 0);
654 	if (!kbdev->devfreq_queue.workq)
655 		return -ENOMEM;
656 
657 	INIT_WORK(&kbdev->devfreq_queue.work,
658 			kbase_devfreq_suspend_resume_worker);
659 	return 0;
660 }
661 
kbase_devfreq_work_term(struct kbase_device * kbdev)662 static void kbase_devfreq_work_term(struct kbase_device *kbdev)
663 {
664 	unsigned long flags;
665 	struct workqueue_struct *workq;
666 
667 	spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
668 	workq = kbdev->devfreq_queue.workq;
669 	kbdev->devfreq_queue.workq = NULL;
670 	spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
671 
672 	destroy_workqueue(workq);
673 }
674 
kbase_devfreq_get_static_power(struct devfreq * devfreq,unsigned long voltage)675 static unsigned long kbase_devfreq_get_static_power(struct devfreq *devfreq,
676 		unsigned long voltage)
677 {
678 	struct device *dev = devfreq->dev.parent;
679 	struct kbase_device *kbdev = dev_get_drvdata(dev);
680 
681 	return rockchip_ipa_get_static_power(kbdev->model_data, voltage);
682 }
683 
kbase_devfreq_init(struct kbase_device * kbdev)684 int kbase_devfreq_init(struct kbase_device *kbdev)
685 {
686 	struct devfreq_cooling_power *kbase_dcp = &kbdev->dfc_power;
687 	struct device_node *np = kbdev->dev->of_node;
688 	struct device_node *model_node;
689 	struct devfreq_dev_profile *dp;
690 	int err;
691 	struct dev_pm_opp *opp;
692 	unsigned int i;
693 	bool free_devfreq_freq_table = true;
694 
695 	if (kbdev->nr_clocks == 0) {
696 		dev_err(kbdev->dev, "Clock not available for devfreq\n");
697 		return -ENODEV;
698 	}
699 
700 	for (i = 0; i < kbdev->nr_clocks; i++) {
701 		if (kbdev->clocks[i])
702 			kbdev->current_freqs[i] =
703 				clk_get_rate(kbdev->clocks[i]);
704 		else
705 			kbdev->current_freqs[i] = 0;
706 	}
707 	if (strstr(__clk_get_name(kbdev->clocks[0]), "scmi"))
708 		kbdev->opp_info.scmi_clk = kbdev->clocks[0];
709 	kbdev->current_nominal_freq = kbdev->current_freqs[0];
710 
711 	opp = devfreq_recommended_opp(kbdev->dev, &kbdev->current_nominal_freq, 0);
712 	if (IS_ERR(opp))
713 		return PTR_ERR(opp);
714 	dev_pm_opp_put(opp);
715 
716 	dp = &kbdev->devfreq_profile;
717 
718 	dp->initial_freq = kbdev->current_nominal_freq;
719 	dp->polling_ms = 100;
720 	dp->target = kbase_devfreq_target;
721 	dp->get_dev_status = kbase_devfreq_status;
722 	dp->get_cur_freq = kbase_devfreq_cur_freq;
723 	dp->exit = kbase_devfreq_exit;
724 
725 	if (kbase_devfreq_init_freq_table(kbdev, dp))
726 		return -EFAULT;
727 
728 	if (dp->max_state > 0) {
729 		/* Record the maximum frequency possible */
730 		kbdev->gpu_props.props.core_props.gpu_freq_khz_max =
731 			dp->freq_table[0] / 1000;
732 	};
733 	err = kbase_devfreq_init_core_mask_table(kbdev);
734 	if (err)
735 		goto init_core_mask_table_failed;
736 
737 	of_property_read_u32(np, "upthreshold",
738 			     &ondemand_data.upthreshold);
739 	of_property_read_u32(np, "downdifferential",
740 			     &ondemand_data.downdifferential);
741 	kbdev->devfreq = devfreq_add_device(kbdev->dev, dp,
742 				"simple_ondemand", &ondemand_data);
743 	if (IS_ERR(kbdev->devfreq)) {
744 		err = PTR_ERR(kbdev->devfreq);
745 		kbdev->devfreq = NULL;
746 		dev_err(kbdev->dev, "Fail to add devfreq device(%d)", err);
747 		goto devfreq_add_dev_failed;
748 	}
749 
750 	/* Explicit free of freq table isn't needed after devfreq_add_device() */
751 	free_devfreq_freq_table = false;
752 
753 	/* Initialize devfreq suspend/resume workqueue */
754 	err = kbase_devfreq_work_init(kbdev);
755 	if (err) {
756 		dev_err(kbdev->dev, "Fail to init devfreq workqueue");
757 		goto devfreq_work_init_failed;
758 	}
759 
760 	/* devfreq_add_device only copies a few of kbdev->dev's fields, so
761 	 * set drvdata explicitly so IPA models can access kbdev.
762 	 */
763 	dev_set_drvdata(&kbdev->devfreq->dev, kbdev);
764 
765 	err = devfreq_register_opp_notifier(kbdev->dev, kbdev->devfreq);
766 	if (err) {
767 		dev_err(kbdev->dev,
768 			"Failed to register OPP notifier (%d)", err);
769 		goto opp_notifier_failed;
770 	}
771 
772 	mali_mdevp.data = kbdev->devfreq;
773 	mali_mdevp.opp_info = &kbdev->opp_info;
774 	kbdev->mdev_info = rockchip_system_monitor_register(kbdev->dev,
775 			&mali_mdevp);
776 	if (IS_ERR(kbdev->mdev_info)) {
777 		dev_dbg(kbdev->dev, "without system monitor\n");
778                kbdev->mdev_info = NULL;
779 	       mali_mdevp.is_checked = true;
780 	}
781 #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
782 	of_property_read_u32(kbdev->dev->of_node, "dynamic-power-coefficient",
783 			     (u32 *)&kbase_dcp->dyn_power_coeff);
784 	model_node = of_get_compatible_child(kbdev->dev->of_node,
785 					     "simple-power-model");
786 	if (model_node) {
787 		of_node_put(model_node);
788 		kbdev->model_data =
789 			rockchip_ipa_power_model_init(kbdev->dev,
790 						      "gpu_leakage");
791 		if (IS_ERR_OR_NULL(kbdev->model_data)) {
792 			kbdev->model_data = NULL;
793 			if (kbase_dcp->dyn_power_coeff)
794 				dev_info(kbdev->dev,
795 					 "only calculate dynamic power\n");
796 			else
797 				dev_err(kbdev->dev,
798 					"failed to initialize power model\n");
799 		} else {
800 			kbase_dcp->get_static_power =
801 				kbase_devfreq_get_static_power;
802 			if (kbdev->model_data->dynamic_coefficient)
803 				kbase_dcp->dyn_power_coeff =
804 					kbdev->model_data->dynamic_coefficient;
805 		}
806 	}
807 
808 	if (kbase_dcp->dyn_power_coeff) {
809 		kbdev->devfreq_cooling =
810 			of_devfreq_cooling_register_power(kbdev->dev->of_node,
811 					kbdev->devfreq,
812 					kbase_dcp);
813 		if (IS_ERR(kbdev->devfreq_cooling)) {
814 			err = PTR_ERR(kbdev->devfreq_cooling);
815 			dev_err(kbdev->dev, "failed to register cooling device\n");
816 			goto ipa_init_failed;
817 		}
818 	} else {
819 		err = kbase_ipa_init(kbdev);
820 		if (err) {
821 			dev_err(kbdev->dev, "IPA initialization failed\n");
822 			goto ipa_init_failed;
823 		}
824 
825 		kbdev->devfreq_cooling = of_devfreq_cooling_register_power(
826 				kbdev->dev->of_node,
827 				kbdev->devfreq,
828 				&kbase_ipa_power_model_ops);
829 		if (IS_ERR(kbdev->devfreq_cooling)) {
830 			err = PTR_ERR(kbdev->devfreq_cooling);
831 			dev_err(kbdev->dev,
832 					"Failed to register cooling device (%d)\n",
833 					err);
834 			goto cooling_reg_failed;
835                }
836 	}
837 #endif
838 
839 	return 0;
840 
841 #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
842 cooling_reg_failed:
843 	kbase_ipa_term(kbdev);
844 ipa_init_failed:
845 	devfreq_unregister_opp_notifier(kbdev->dev, kbdev->devfreq);
846 #endif /* CONFIG_DEVFREQ_THERMAL */
847 
848 opp_notifier_failed:
849 	kbase_devfreq_work_term(kbdev);
850 
851 devfreq_work_init_failed:
852 	if (devfreq_remove_device(kbdev->devfreq))
853 		dev_err(kbdev->dev, "Failed to terminate devfreq (%d)", err);
854 
855 	kbdev->devfreq = NULL;
856 
857 devfreq_add_dev_failed:
858 	kbase_devfreq_term_core_mask_table(kbdev);
859 
860 init_core_mask_table_failed:
861 	if (free_devfreq_freq_table)
862 		kbase_devfreq_term_freq_table(kbdev);
863 
864 	return err;
865 }
866 
kbase_devfreq_term(struct kbase_device * kbdev)867 void kbase_devfreq_term(struct kbase_device *kbdev)
868 {
869 	int err;
870 
871 	dev_dbg(kbdev->dev, "Term Mali devfreq\n");
872 
873 #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
874 	if (kbdev->devfreq_cooling)
875 		devfreq_cooling_unregister(kbdev->devfreq_cooling);
876 #endif
877 
878 	devfreq_unregister_opp_notifier(kbdev->dev, kbdev->devfreq);
879 
880 	kbase_devfreq_work_term(kbdev);
881 
882 	err = devfreq_remove_device(kbdev->devfreq);
883 	if (err)
884 		dev_err(kbdev->dev, "Failed to terminate devfreq (%d)\n", err);
885 	else
886 		kbdev->devfreq = NULL;
887 
888 	kbase_devfreq_term_core_mask_table(kbdev);
889 
890 #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
891 	if (!kbdev->model_data)
892 		kbase_ipa_term(kbdev);
893 	kfree(kbdev->model_data);
894 #endif
895 }
896