hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (C) 2011-2017 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
#include "mali_osk_mali.h"
#include "mali_kernel_common.h"
 
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/devfreq.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h>
#ifdef CONFIG_DEVFREQ_THERMAL
#include <linux/devfreq_cooling.h>
#endif
 
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
#include <linux/pm_opp.h>
#else /* Linux >= 3.13 */
/* In 3.13 the OPP include header file, types, and functions were all
 * renamed. Use the old filename for the include, and define the new names to
 * the old, when an old kernel is detected.
 */
#include <linux/opp.h>
#define dev_pm_opp opp
#define dev_pm_opp_get_voltage opp_get_voltage
#define dev_pm_opp_get_opp_count opp_get_opp_count
#define dev_pm_opp_find_freq_ceil opp_find_freq_ceil
#endif /* Linux >= 3.13 */
 
#include "mali_pm_metrics.h"
 
#include <soc/rockchip/rockchip_opp_select.h>
#include <soc/rockchip/rockchip_system_monitor.h>
 
static struct monitor_dev_profile mali_mdevp = {
   .type = MONITOR_TPYE_DEV,
   .low_temp_adjust = rockchip_monitor_dev_low_temp_adjust,
   .high_temp_adjust = rockchip_monitor_dev_high_temp_adjust,
};
 
static struct devfreq_simple_ondemand_data ondemand_data;
 
static int
mali_devfreq_target(struct device *dev, unsigned long *target_freq, u32 flags)
{
   struct mali_device *mdev = dev_get_drvdata(dev);
   struct dev_pm_opp *opp;
   unsigned long freq = 0;
   unsigned long old_freq = mdev->current_freq;
   unsigned long voltage;
   int err;
 
   freq = *target_freq;
 
   opp = devfreq_recommended_opp(dev, &freq, flags);
   if (IS_ERR(opp)) {
       MALI_PRINT_ERROR(("Failed to get opp (%ld)\n", PTR_ERR(opp)));
       return PTR_ERR(opp);
   }
   voltage = dev_pm_opp_get_voltage(opp);
   dev_pm_opp_put(opp);
 
   MALI_DEBUG_PRINT(2, ("mali_devfreq_target:set_freq = %lld flags = 0x%x\n", freq, flags));
   /*
    * Only update if there is a change of frequency
    */
   if (old_freq == freq) {
       *target_freq = freq;
       mali_pm_reset_dvfs_utilisation(mdev);
#ifdef CONFIG_REGULATOR
       if (mdev->current_voltage == voltage)
           return 0;
       err = regulator_set_voltage(mdev->regulator, voltage, INT_MAX);
       if (err) {
           dev_err(dev, "Failed to set voltage (%d)\n", err);
           return err;
       }
       mdev->current_voltage = voltage;
#endif
       return 0;
   }
 
   err = clk_bulk_enable(mdev->num_clks, mdev->clks);
   if (err)
       return err;
 
#ifdef CONFIG_REGULATOR
   if (mdev->regulator && mdev->current_voltage != voltage &&
       old_freq < freq) {
       err = regulator_set_voltage(mdev->regulator, voltage, INT_MAX);
       if (err) {
           MALI_PRINT_ERROR(("Failed to increase voltage (%d)\n", err));
           goto err;
       }
   }
#endif
 
   err = clk_set_rate(mdev->clock, freq);
   if (err) {
       MALI_PRINT_ERROR(("Failed to set clock %lu (target %lu)\n", freq, *target_freq));
       goto err;
   }
 
   *target_freq = freq;
   mdev->current_freq = freq;
   if (mdev->devfreq)
       mdev->devfreq->last_status.current_frequency = freq;
 
#ifdef CONFIG_REGULATOR
   if (mdev->regulator && mdev->current_voltage != voltage &&
       old_freq > freq) {
       err = regulator_set_voltage(mdev->regulator, voltage, INT_MAX);
       if (err) {
           MALI_PRINT_ERROR(("Failed to decrease voltage (%d)\n", err));
           goto err;
       }
   }
#endif
 
   mdev->current_voltage = voltage;
 
   mali_pm_reset_dvfs_utilisation(mdev);
err:
   clk_bulk_disable(mdev->num_clks, mdev->clks);
 
   return err;
}
 
static int
mali_devfreq_cur_freq(struct device *dev, unsigned long *freq)
{
   struct mali_device *mdev = dev_get_drvdata(dev);
 
   *freq = mdev->current_freq;
 
   MALI_DEBUG_PRINT(2, ("mali_devfreq_cur_freq: freq = %d \n", *freq));
   return 0;
}
 
static int
mali_devfreq_status(struct device *dev, struct devfreq_dev_status *stat)
{
   struct mali_device *mdev = dev_get_drvdata(dev);
 
   stat->current_frequency = mdev->current_freq;
 
   mali_pm_get_dvfs_utilisation(mdev,
                    &stat->total_time, &stat->busy_time);
 
   stat->private_data = NULL;
 
#ifdef CONFIG_DEVFREQ_THERMAL
   memcpy(&mdev->devfreq->last_status, stat, sizeof(*stat));
#endif
 
   return 0;
}
 
/* setup platform specific opp in platform.c*/
int __weak setup_opps(void)
{
   return 0;
}
 
/* term platform specific opp in platform.c*/
int __weak term_opps(struct device *dev)
{
   return 0;
}
 
static int mali_devfreq_init_freq_table(struct mali_device *mdev,
                   struct devfreq_dev_profile *dp)
{
   int err, count;
   int i = 0;
   unsigned long freq = 0;
   struct dev_pm_opp *opp;
 
   err = setup_opps();
   if (err)
       return err;
 
   count = dev_pm_opp_get_opp_count(mdev->dev);
   if (count < 0) {
       return count;
   }
 
   MALI_DEBUG_PRINT(2, ("mali devfreq table count %d\n", count));
 
   dp->freq_table = kmalloc_array(count, sizeof(dp->freq_table[0]),
                      GFP_KERNEL);
   if (!dp->freq_table)
       return -ENOMEM;
 
   for (i = 0; i < count; i++, freq++) {
       opp = dev_pm_opp_find_freq_ceil(mdev->dev, &freq);
       if (IS_ERR(opp))
           break;
       dev_pm_opp_put(opp);
 
       dp->freq_table[i] = freq;
       MALI_DEBUG_PRINT(2, ("mali devfreq table array[%d] = %d\n", i, freq));
   }
 
   if (count != i)
       MALI_PRINT_ERROR(("Unable to enumerate all OPPs (%d!=%d)\n",
                 count, i));
 
   dp->max_state = i;
 
   return 0;
}
 
static void mali_devfreq_term_freq_table(struct mali_device *mdev)
{
   struct devfreq_dev_profile *dp = mdev->devfreq->profile;
 
   kfree(dp->freq_table);
   term_opps(mdev->dev);
}
 
static void mali_devfreq_exit(struct device *dev)
{
   struct mali_device *mdev = dev_get_drvdata(dev);
 
   mali_devfreq_term_freq_table(mdev);
}
 
int mali_devfreq_init(struct mali_device *mdev)
{
   struct device_node *np = mdev->dev->of_node;
#ifdef CONFIG_DEVFREQ_THERMAL
   struct devfreq_cooling_power *callbacks = NULL;
   _mali_osk_device_data data;
#endif
   struct devfreq_dev_profile *dp;
   struct dev_pm_opp *opp;
   unsigned long opp_rate;
   int err;
 
   MALI_DEBUG_PRINT(2, ("Init Mali devfreq\n"));
 
   if (!mdev->clock)
       return -ENODEV;
 
   mdev->current_freq = clk_get_rate(mdev->clock);
 
   dp = &mdev->devfreq_profile;
 
   dp->initial_freq = mdev->current_freq;
   dp->polling_ms = 100;
   dp->target = mali_devfreq_target;
   dp->get_dev_status = mali_devfreq_status;
   dp->get_cur_freq = mali_devfreq_cur_freq;
   dp->exit = mali_devfreq_exit;
 
   if (mali_devfreq_init_freq_table(mdev, dp))
       return -EFAULT;
 
   of_property_read_u32(np, "upthreshold",
                &ondemand_data.upthreshold);
   of_property_read_u32(np, "downdifferential",
                &ondemand_data.downdifferential);
 
   mdev->devfreq = devfreq_add_device(mdev->dev, dp,
                      "simple_ondemand", &ondemand_data);
   if (IS_ERR(mdev->devfreq)) {
       mali_devfreq_term_freq_table(mdev);
       return PTR_ERR(mdev->devfreq);
   }
 
   err = devfreq_register_opp_notifier(mdev->dev, mdev->devfreq);
   if (err) {
       MALI_PRINT_ERROR(("Failed to register OPP notifier (%d)\n", err));
       goto opp_notifier_failed;
   }
 
   opp_rate = mdev->current_freq;
   opp = devfreq_recommended_opp(mdev->dev, &opp_rate, 0);
   if (!IS_ERR(opp))
       dev_pm_opp_put(opp);
   mdev->devfreq->last_status.current_frequency = opp_rate;
 
   mali_mdevp.data = mdev->devfreq;
   mdev->mdev_info = rockchip_system_monitor_register(mdev->dev,
                              &mali_mdevp);
   if (IS_ERR(mdev->mdev_info)) {
       dev_dbg(mdev->dev, "without system monitor\n");
       mdev->mdev_info = NULL;
   }
#ifdef CONFIG_DEVFREQ_THERMAL
   if (of_machine_is_compatible("rockchip,rk3036"))
       return 0;
 
   /* Initilization last_status it will be used when first power allocate called */
   mdev->devfreq->last_status.current_frequency = mdev->current_freq;
 
   if (_MALI_OSK_ERR_OK == _mali_osk_device_data_get(&data)) {
       if (NULL != data.gpu_cooling_ops) {
           callbacks = data.gpu_cooling_ops;
           MALI_DEBUG_PRINT(2, ("Mali GPU Thermal: Callback handler installed \n"));
       }
   }
 
   if (callbacks) {
       mdev->devfreq_cooling = of_devfreq_cooling_register_power(
                       mdev->dev->of_node,
                       mdev->devfreq,
                       callbacks);
       if (IS_ERR_OR_NULL(mdev->devfreq_cooling)) {
           err = PTR_ERR(mdev->devfreq_cooling);
           MALI_PRINT_ERROR(("Failed to register cooling device (%d)\n", err));
           goto cooling_failed;
       } else {
           MALI_DEBUG_PRINT(2, ("Mali GPU Thermal Cooling installed \n"));
       }
   }
#endif
 
   return 0;
 
#ifdef CONFIG_DEVFREQ_THERMAL
cooling_failed:
   devfreq_unregister_opp_notifier(mdev->dev, mdev->devfreq);
#endif /* CONFIG_DEVFREQ_THERMAL */
opp_notifier_failed:
   err = devfreq_remove_device(mdev->devfreq);
   if (err)
       MALI_PRINT_ERROR(("Failed to terminate devfreq (%d)\n", err));
   else
       mdev->devfreq = NULL;
 
   return err;
}
 
void mali_devfreq_term(struct mali_device *mdev)
{
   int err;
 
   MALI_DEBUG_PRINT(2, ("Term Mali devfreq\n"));
 
   rockchip_system_monitor_unregister(mdev->mdev_info);
#ifdef CONFIG_DEVFREQ_THERMAL
   devfreq_cooling_unregister(mdev->devfreq_cooling);
#endif
 
   devfreq_unregister_opp_notifier(mdev->dev, mdev->devfreq);
 
   err = devfreq_remove_device(mdev->devfreq);
   if (err)
       MALI_PRINT_ERROR(("Failed to terminate devfreq (%d)\n", err));
   else
       mdev->devfreq = NULL;
}