rk312x, mali_400: make it more 'difficult' to dump down in mali_dvfs_list.
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / platform / rk30 / mali_dvfs.c
1 /**
2  * Rockchip SoC Mali-450 DVFS driver
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software FoundatIon.
7  */
8
9
10 /* #define ENABLE_DEBUG_LOG */
11 #include "custom_log.h"
12
13 #include "mali_platform.h"
14 #include "mali_dvfs.h"
15
16 /*---------------------------------------------------------------------------*/
17
18 /**
19  * 若 count_of_continuous_requests_to_jump_up_in_dvfs_level_table 达到 value,
20  * 将触发一次 current_dvfs_level jump_up.
21  */
22 #define NUM_OF_CONTINUOUS_REQUESTS_TO_TRIGGER_REAL_JUMPING_UP           (1)
23 /**
24  * 若 count_of_continuous_requests_to_jump_down_in_dvfs_level_table 达到 value,
25  * 将触发一次 current_dvfs_level jump_down.
26  */
27 #define NUM_OF_CONTINUOUS_REQUESTS_TO_TRIGGER_REAL_JUMPING_DOWN         (30)
28
29 #define level0_min 0
30 #define level0_max 70
31 #define levelf_max 100
32
33 #define mali_dividend 7
34 #define mali_fix_float(a) \
35         ((((a) * mali_dividend) % 10) \
36                 ? ((((a) * mali_dividend) / 10) + 1) \
37                 : (((a) * mali_dividend) / 10))
38
39 #define work_to_dvfs(w) container_of(w, struct mali_dvfs, work)
40 #define dvfs_to_drv_data(dvfs) \
41         container_of(dvfs, struct mali_platform_drv_data, dvfs)
42
43 /*---------------------------------------------------------------------------*/
44
45 static bool mali_dvfs_should_jump_up(const struct mali_dvfs *p_dvfs)
46 {
47         return (p_dvfs->m_count_of_requests_to_jump_up
48                 >= NUM_OF_CONTINUOUS_REQUESTS_TO_TRIGGER_REAL_JUMPING_UP);
49 }
50
51 static bool mali_dvfs_should_jump_down(const struct mali_dvfs *p_dvfs)
52 {
53         return (p_dvfs->m_count_of_requests_to_jump_down
54                 >= NUM_OF_CONTINUOUS_REQUESTS_TO_TRIGGER_REAL_JUMPING_DOWN);
55 }
56
57 /**
58  * .KP :
59  * work_to_handle_mali_utilization_event 的实现主体,
60  * 将根据 current_mali_utilization_data, 改变 current_dvfs_level.
61  */
62 static void mali_dvfs_event_proc(struct work_struct *w)
63 {
64         struct mali_dvfs *dvfs = work_to_dvfs(w);/* mali_dvfs_context. */
65         struct mali_platform_drv_data *drv_data = dvfs_to_drv_data(dvfs);
66         unsigned int utilisation = dvfs->utilisation;
67         int ret = 0;
68         /* index_of_current_dvfs_level. */
69         unsigned int level = dvfs->current_level;
70         /* current_dvfs_level. */
71         const struct mali_fv_info *threshold = &drv_data->fv_info[level];
72         /* utilization_in_percentage. */
73         utilisation = utilisation * 100 / 256;
74
75         V("mali_utilization_in_percentage : %d; index_of_current_level : %d;"
76                         "min: %d, max : %d",
77           utilisation,
78           level,
79           threshold->min,
80           threshold->max);
81         V("num_of_requests_to_jump_up : %d; num_of_requests_to_jump_down : %d",
82           dvfs->m_count_of_requests_to_jump_up,
83           dvfs->m_count_of_requests_to_jump_down);
84
85         if (utilisation > threshold->max) {
86                 dvfs->m_count_of_requests_to_jump_down = 0;
87
88                 /* 若 current_dvfs_level 还 "有" 上跳的空间, 则... */
89                 if (level < (drv_data->fv_info_length - 1)) {
90                         V("to count one request_to_jump_up.");
91                         dvfs->m_count_of_requests_to_jump_up++;
92
93                         /* 若足够触发一次 real_jump_up, 则... */
94                         if (mali_dvfs_should_jump_up(dvfs)) {
95                                 V("to preset real_jump_up.");
96                                 level += 1;
97
98                                 dvfs->m_count_of_requests_to_jump_up = 0;
99
100                                 goto MAKE_DVFS_LEVEL_UPDATE_EFFECTIVE;
101                         }
102                         /* 否则, 即还 "不" 足以 real_jump_up, 则... */
103                         else
104                                 return;
105                 }
106                 /* 否则, 即 "没有" 上跳的空间了, 则... */
107                 else
108                         return;
109         } else if (utilisation < (threshold->min)) {
110                 dvfs->m_count_of_requests_to_jump_up = 0;
111
112                 /* 若 current_dvfs_level 还有 下跳的空间, 则... */
113                 if (level > 0) {
114                         V("to count one request_to_jump_down.");
115                         dvfs->m_count_of_requests_to_jump_down++;
116
117                         /* 若足够触发一次 real_jump_down, 则... */
118                         if (mali_dvfs_should_jump_down(dvfs)) {
119                                 V("to preset real_jump_down.");
120                                 level -= 1;
121
122                                 dvfs->m_count_of_requests_to_jump_down = 0;
123
124                                 goto MAKE_DVFS_LEVEL_UPDATE_EFFECTIVE;
125                         } else {
126                                 return;
127                         }
128                 }
129                 /* 否则, 即 "没有" 下跳的空间了, 则... */
130                 else
131                         return;
132         }
133         /* 否则,
134          * 即 mali_utilization 在 'min' 和 'max' 之间,
135          * 即 current_dvfs_level 合适, 则 ... */
136         else {
137                 dvfs->m_count_of_requests_to_jump_up = 0;
138                 dvfs->m_count_of_requests_to_jump_down = 0;
139                 return;
140         }
141
142         /*
143         dev_dbg(drv_data->dev, "Setting dvfs level %u: freq = %lu Hz\n",
144                 level, drv_data->fv_info[level].freq);
145         */
146
147 MAKE_DVFS_LEVEL_UPDATE_EFFECTIVE:
148         ret = mali_set_level(drv_data->dev, level);
149         if (ret) {
150                 dev_err(drv_data->dev, "set freq error, %d", ret);
151                 return;
152         }
153 }
154
155 /**
156  * 返回 mali_dvfs_facility 当前是否被使能.
157  */
158 bool mali_dvfs_is_enabled(struct device *dev)
159 {
160         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
161         struct mali_dvfs *dvfs = &drv_data->dvfs;
162
163         return dvfs->enabled;
164 }
165
166 /**
167  * 使能 mali_dvfs_facility.
168  */
169 void mali_dvfs_enable(struct device *dev)
170 {
171         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
172         struct mali_dvfs *dvfs = &drv_data->dvfs;
173
174         dvfs->enabled = true;
175 }
176
177 /**
178  * 禁用 mali_dvfs_facility.
179  */
180 void mali_dvfs_disable(struct device *dev)
181 {
182         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
183         struct mali_dvfs *dvfs = &drv_data->dvfs;
184
185         dvfs->enabled = false;
186         cancel_work_sync(&dvfs->work);
187
188         /* 使用 lowest_dvfs_level. */
189         if (0 != mali_set_level(dev, 0))
190                 W("fail to set current_dvfs_level to index 0.");
191 }
192
193 /**
194  * 返回当前 mali_dvfs_context 中最后记录的 mali_utilization.
195  */
196 unsigned int mali_dvfs_utilisation(struct device *dev)
197 {
198         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
199         struct mali_dvfs *dvfs = &drv_data->dvfs;
200
201         return dvfs->utilisation;
202 }
203
204 /**
205  * 根据当前的 mali_utilization_event, 对应调整 mali_dvfs_facility.
206  * 运行在 context_of_common_part_calling_back_mali_utilization_event 中.
207  */
208 int mali_dvfs_event(struct device *dev, u32 utilisation)
209 {
210         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
211         struct mali_dvfs *dvfs = &drv_data->dvfs;
212
213         dvfs->utilisation = utilisation;
214         V("mali_utilization_in_percentage : %d", utilisation * 100 / 256);
215
216         if (dvfs->enabled) {
217                 /* 将 work_to_handle_mali_utilization_event,
218                  * 放置到 kernel_global_workqueue, 待执行. */
219                 schedule_work(&dvfs->work);
220         }
221
222         return MALI_TRUE;
223 }
224
225 static void mali_dvfs_threshold(u32 div,
226                                 struct mali_platform_drv_data *drv_data)
227 {
228         int length = drv_data->fv_info_length;
229         u32 pre_level;
230         u32 tmp;
231         int level;
232
233         for (level = 0; level < length; level++) {
234                 if (level == 0) {
235                         drv_data->fv_info[level].min = level0_min;
236                         if (length == 1)
237                                 drv_data->fv_info[level].max = levelf_max;
238                         else
239                                 drv_data->fv_info[level].max = level0_max;
240                 } else {
241                         pre_level = level - 1;
242                         if (level == length - 1)
243                                 drv_data->fv_info[level].max = levelf_max;
244                         else
245                                 drv_data->fv_info[level].max
246                                 = drv_data->fv_info[pre_level].max + div;
247
248                         drv_data->fv_info[level].min
249                         = drv_data->fv_info[pre_level].max
250                                 * drv_data->fv_info[pre_level].freq
251                                 / drv_data->fv_info[level].freq;
252
253                         tmp
254                         = drv_data->fv_info[level].max
255                                 - drv_data->fv_info[level].min;
256                         drv_data->fv_info[level].min += mali_fix_float(tmp);
257                 }
258
259                 dev_info(drv_data->dev, "freq: %lu, min_threshold: %d, max_threshold: %d\n",
260                          drv_data->fv_info[level].freq,
261                          drv_data->fv_info[level].min,
262                          drv_data->fv_info[level].max);
263         }
264 }
265
266 /*---------------------------------------------------------------------------*/
267
268 int mali_dvfs_init(struct device *dev)
269 {
270         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
271         /*mali_dvfs_context. */
272         struct mali_dvfs *dvfs = &drv_data->dvfs;
273         /* gpu_clk_freq_table. */
274         struct cpufreq_frequency_table *freq_table;
275         int i = 0;
276         int div_dvfs;
277         int ret;
278
279         freq_table = dvfs_get_freq_volt_table(drv_data->clk);
280         if (!freq_table) {
281                 dev_err(dev, "Can't find dvfs table in dts\n");
282                 return -1;
283         }
284
285         /* 确定 len_of_avaialble_dvfs_level_list. */
286         while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
287                 drv_data->fv_info_length++;
288                 i++;
289         }
290
291         drv_data->fv_info = devm_kcalloc(dev, drv_data->fv_info_length,
292                                          sizeof(*drv_data->fv_info),
293                                          GFP_KERNEL);
294         if (!drv_data->fv_info)
295                 return -ENOMEM;
296
297         for (i = 0; i < drv_data->fv_info_length; i++)
298                 drv_data->fv_info[i].freq = freq_table[i].frequency * 1000;
299
300         if (drv_data->fv_info_length > 1)
301                 div_dvfs
302                 = round_up(((levelf_max - level0_max)
303                                 / (drv_data->fv_info_length - 1)),
304                            1);
305
306         mali_dvfs_threshold(div_dvfs, drv_data);
307
308         ret = dvfs_clk_set_rate(drv_data->clk, drv_data->fv_info[0].freq);
309         if (ret)
310                 return ret;
311
312         drv_data->dvfs.current_level = 0;
313
314         dev_info(dev, "initial freq = %lu\n",
315                  dvfs_clk_get_rate(drv_data->clk));
316
317         INIT_WORK(&dvfs->work, mali_dvfs_event_proc);
318         dvfs->enabled = true;
319
320         dvfs->m_count_of_requests_to_jump_up = 0;
321         dvfs->m_count_of_requests_to_jump_down = 0;
322
323         return 0;
324 }
325
326 void mali_dvfs_term(struct device *dev)
327 {
328         struct mali_platform_drv_data *drv_data = dev_get_drvdata(dev);
329         struct mali_dvfs *dvfs = &drv_data->dvfs;
330
331         dvfs->m_count_of_requests_to_jump_up = 0;
332         dvfs->m_count_of_requests_to_jump_down = 0;
333
334         dvfs->enabled = false;
335         cancel_work_sync(&dvfs->work);
336 }
337