cd4f0a2bb24f44885150302e76ad506dc2cdd044
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / backend / gpu / mali_kbase_power_model_simple.c
1 /*
2  *
3  * (C) COPYRIGHT 2011-2015 ARM Limited. All rights reserved.
4  *
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
8  * of such GNU licence.
9  *
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.
13  *
14  */
15
16
17
18 #include <linux/devfreq_cooling.h>
19 #include <linux/thermal.h>
20 #include <linux/of.h>
21 #include <mali_kbase.h>
22 #include <mali_kbase_defs.h>
23 #include <backend/gpu/mali_kbase_power_model_simple.h>
24
25 /*
26  * This model is primarily designed for the Juno platform. It may not be
27  * suitable for other platforms.
28  */
29
30 #define FALLBACK_STATIC_TEMPERATURE 55000
31
32 static u32 dynamic_coefficient;
33 static u32 static_coefficient;
34 static s32 ts[4];
35 static struct thermal_zone_device *gpu_tz;
36
37 static unsigned long model_static_power(unsigned long voltage)
38 {
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;
42
43         if (gpu_tz) {
44                 int ret;
45
46                 ret = gpu_tz->ops->get_temp(gpu_tz, &temperature);
47                 if (ret) {
48                         pr_warn_ratelimited("Error reading temperature for gpu thermal zone: %d\n",
49                                         ret);
50                         temperature = FALLBACK_STATIC_TEMPERATURE;
51                 }
52         } else {
53                 temperature = FALLBACK_STATIC_TEMPERATURE;
54         }
55
56         /* Calculate the temperature scaling factor. To be applied to the
57          * voltage scaled power.
58          */
59         temp = temperature / 1000;
60         temp_squared = temp * temp;
61         temp_cubed = temp_squared * temp;
62         temp_scaling_factor =
63                         (ts[3] * temp_cubed)
64                         + (ts[2] * temp_squared)
65                         + (ts[1] * temp)
66                         + ts[0];
67
68         return (((static_coefficient * voltage_cubed) >> 20)
69                         * temp_scaling_factor)
70                                 / 1000000;
71 }
72
73 static unsigned long model_dynamic_power(unsigned long freq,
74                 unsigned long voltage)
75 {
76         /* The inputs: freq (f) is in Hz, and voltage (v) in mV.
77          * The coefficient (c) is in mW/(MHz mV mV).
78          *
79          * This function calculates the dynamic power after this formula:
80          * Pdyn (mW) = c (mW/(MHz*mV*mV)) * v (mV) * v (mV) * f (MHz)
81          */
82         const unsigned long v2 = (voltage * voltage) / 1000; /* m*(V*V) */
83         const unsigned long f_mhz = freq / 1000000; /* MHz */
84
85         return (dynamic_coefficient * v2 * f_mhz) / 1000000; /* mW */
86 }
87
88 struct devfreq_cooling_ops power_model_simple_ops = {
89         .get_static_power = model_static_power,
90         .get_dynamic_power = model_dynamic_power,
91 };
92
93 int kbase_power_model_simple_init(struct kbase_device *kbdev)
94 {
95         struct device_node *power_model_node;
96         const char *tz_name;
97         u32 static_power, dynamic_power;
98         u32 voltage, voltage_squared, voltage_cubed, frequency;
99
100         power_model_node = of_get_child_by_name(kbdev->dev->of_node,
101                         "power_model");
102         if (!power_model_node) {
103                 dev_err(kbdev->dev, "could not find power_model node\n");
104                 return -ENODEV;
105         }
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");
109                 return -ENODEV;
110         }
111
112         if (of_property_read_string(power_model_node, "thermal-zone",
113                         &tz_name)) {
114                 dev_err(kbdev->dev, "ts in power_model not available\n");
115                 return -EINVAL;
116         }
117
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",
121                                 PTR_ERR(gpu_tz));
122                 gpu_tz = NULL;
123
124                 return -EPROBE_DEFER;
125         }
126
127         if (of_property_read_u32(power_model_node, "static-power",
128                         &static_power)) {
129                 dev_err(kbdev->dev, "static-power in power_model not available\n");
130                 return -EINVAL;
131         }
132         if (of_property_read_u32(power_model_node, "dynamic-power",
133                         &dynamic_power)) {
134                 dev_err(kbdev->dev, "dynamic-power in power_model not available\n");
135                 return -EINVAL;
136         }
137         if (of_property_read_u32(power_model_node, "voltage",
138                         &voltage)) {
139                 dev_err(kbdev->dev, "voltage in power_model not available\n");
140                 return -EINVAL;
141         }
142         if (of_property_read_u32(power_model_node, "frequency",
143                         &frequency)) {
144                 dev_err(kbdev->dev, "frequency in power_model not available\n");
145                 return -EINVAL;
146         }
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)
151                         * 1000) / frequency;
152
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");
155                 return -EINVAL;
156         }
157
158         return 0;
159 }
160