3 * (C) COPYRIGHT 2011-2015 ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * A copy of the licence is included with the program, and can also be obtained
11 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12 * Boston, MA 02110-1301, USA.
18 #include <linux/devfreq_cooling.h>
19 #include <linux/thermal.h>
21 #include <mali_kbase.h>
22 #include <mali_kbase_defs.h>
23 #include <backend/gpu/mali_kbase_power_model_simple.h>
26 * This model is primarily designed for the Juno platform. It may not be
27 * suitable for other platforms.
30 #define FALLBACK_STATIC_TEMPERATURE 55000
32 static u32 dynamic_coefficient;
33 static u32 static_coefficient;
35 static struct thermal_zone_device *gpu_tz;
37 static unsigned long model_static_power(unsigned long voltage)
39 unsigned long temperature, temp;
40 unsigned long temp_squared, temp_cubed, temp_scaling_factor;
41 const unsigned long voltage_cubed = (voltage * voltage * voltage) >> 10;
46 ret = gpu_tz->ops->get_temp(gpu_tz, &temperature);
48 pr_warn_ratelimited("Error reading temperature for gpu thermal zone: %d\n",
50 temperature = FALLBACK_STATIC_TEMPERATURE;
53 temperature = FALLBACK_STATIC_TEMPERATURE;
56 /* Calculate the temperature scaling factor. To be applied to the
57 * voltage scaled power.
59 temp = temperature / 1000;
60 temp_squared = temp * temp;
61 temp_cubed = temp_squared * temp;
64 + (ts[2] * temp_squared)
68 return (((static_coefficient * voltage_cubed) >> 20)
69 * temp_scaling_factor)
73 static unsigned long model_dynamic_power(unsigned long freq,
74 unsigned long voltage)
76 /* The inputs: freq (f) is in Hz, and voltage (v) in mV.
77 * The coefficient (c) is in mW/(MHz mV mV).
79 * This function calculates the dynamic power after this formula:
80 * Pdyn (mW) = c (mW/(MHz*mV*mV)) * v (mV) * v (mV) * f (MHz)
82 const unsigned long v2 = (voltage * voltage) / 1000; /* m*(V*V) */
83 const unsigned long f_mhz = freq / 1000000; /* MHz */
85 return (dynamic_coefficient * v2 * f_mhz) / 1000000; /* mW */
88 struct devfreq_cooling_ops power_model_simple_ops = {
89 .get_static_power = model_static_power,
90 .get_dynamic_power = model_dynamic_power,
93 int kbase_power_model_simple_init(struct kbase_device *kbdev)
95 struct device_node *power_model_node;
97 u32 static_power, dynamic_power;
98 u32 voltage, voltage_squared, voltage_cubed, frequency;
100 power_model_node = of_get_child_by_name(kbdev->dev->of_node,
102 if (!power_model_node) {
103 dev_err(kbdev->dev, "could not find power_model node\n");
106 if (!of_device_is_compatible(power_model_node,
107 "arm,mali-simple-power-model")) {
108 dev_err(kbdev->dev, "power_model incompatible with simple power model\n");
112 if (of_property_read_string(power_model_node, "thermal-zone",
114 dev_err(kbdev->dev, "ts in power_model not available\n");
118 gpu_tz = thermal_zone_get_zone_by_name(tz_name);
119 if (IS_ERR(gpu_tz)) {
120 pr_warn_ratelimited("Error getting gpu thermal zone (%ld), not yet ready?\n",
124 return -EPROBE_DEFER;
127 if (of_property_read_u32(power_model_node, "static-power",
129 dev_err(kbdev->dev, "static-power in power_model not available\n");
132 if (of_property_read_u32(power_model_node, "dynamic-power",
134 dev_err(kbdev->dev, "dynamic-power in power_model not available\n");
137 if (of_property_read_u32(power_model_node, "voltage",
139 dev_err(kbdev->dev, "voltage in power_model not available\n");
142 if (of_property_read_u32(power_model_node, "frequency",
144 dev_err(kbdev->dev, "frequency in power_model not available\n");
147 voltage_squared = (voltage * voltage) / 1000;
148 voltage_cubed = voltage * voltage * voltage;
149 static_coefficient = (static_power << 20) / (voltage_cubed >> 10);
150 dynamic_coefficient = (((dynamic_power * 1000) / voltage_squared)
153 if (of_property_read_u32_array(power_model_node, "ts", ts, 4)) {
154 dev_err(kbdev->dev, "ts in power_model not available\n");