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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
/*
* linux/drivers/thermal/gpu_cooling.c
*
* Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
* Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <linux/module.h>
#include <linux/thermal.h>
#include <linux/cpufreq.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/gpu_cooling.h>
#include <soc/samsung/tmu.h>
#include <trace/events/thermal.h>
#include <soc/samsung/cal-if.h>
#include <soc/samsung/ect_parser.h>
#if defined(CONFIG_SOC_EXYNOS8895) && defined(CONFIG_SOC_EMULATOR8895)
#include <dt-bindings/clock/emulator8895.h>
#elif defined(CONFIG_SOC_EXYNOS8895) && !defined(CONFIG_SOC_EMULATOR8895)
#include <dt-bindings/clock/exynos8895.h>
#endif
/**
* struct power_table - frequency to power conversion
* @frequency: frequency in KHz
* @power: power in mW
*
* This structure is built when the cooling device registers and helps
* in translating frequency to power and viceversa.
*/
struct power_table {
u32 frequency;
u32 power;
};
/**
* struct gpufreq_cooling_device - data for cooling device with gpufreq
* @id: unique integer value corresponding to each gpufreq_cooling_device
* registered.
* @cool_dev: thermal_cooling_device pointer to keep track of the
* registered cooling device.
* @gpufreq_state: integer value representing the current state of gpufreq
* cooling devices.
* @gpufreq_val: integer value representing the absolute value of the clipped
* frequency.
* @allowed_gpus: all the gpus involved for this gpufreq_cooling_device.
*
* This structure is required for keeping information of each
* gpufreq_cooling_device registered. In order to prevent corruption of this a
* mutex lock cooling_gpu_lock is used.
*/
struct gpufreq_cooling_device {
int id;
struct thermal_cooling_device *cool_dev;
unsigned int gpufreq_state;
unsigned int gpufreq_val;
u32 last_load;
struct power_table *dyn_power_table;
int dyn_power_table_entries;
get_static_t plat_get_static_power;
int *var_table;
int *var_coeff;
int *asv_coeff;
unsigned int var_volt_size;
unsigned int var_temp_size;
};
static DEFINE_IDR(gpufreq_idr);
static DEFINE_MUTEX(cooling_gpu_lock);
static BLOCKING_NOTIFIER_HEAD(gpu_notifier);
static unsigned int gpufreq_dev_count;
extern struct cpufreq_frequency_table gpu_freq_table[];
/**
* get_idr - function to get a unique id.
* @idr: struct idr * handle used to create a id.
* @id: int * value generated by this function.
*
* This function will populate @id with an unique
* id, using the idr API.
*
* Return: 0 on success, an error code on failure.
*/
static int get_idr(struct idr *idr, int *id)
{
int ret;
mutex_lock(&cooling_gpu_lock);
ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
mutex_unlock(&cooling_gpu_lock);
if (unlikely(ret < 0))
return ret;
*id = ret;
return 0;
}
/**
* release_idr - function to free the unique id.
* @idr: struct idr * handle used for creating the id.
* @id: int value representing the unique id.
*/
static void release_idr(struct idr *idr, int id)
{
mutex_lock(&cooling_gpu_lock);
idr_remove(idr, id);
mutex_unlock(&cooling_gpu_lock);
}
/* Below code defines functions to be used for gpufreq as cooling device */
enum gpufreq_cooling_property {
GET_LEVEL,
GET_FREQ,
GET_MAXL,
};
/**
* get_property - fetch a property of interest for a give gpu.
* @gpu: gpu for which the property is required
* @input: query parameter
* @output: query return
* @property: type of query (frequency, level, max level)
*
* This is the common function to
* 1. get maximum gpu cooling states
* 2. translate frequency to cooling state
* 3. translate cooling state to frequency
* Note that the code may be not in good shape
* but it is written in this way in order to:
* a) reduce duplicate code as most of the code can be shared.
* b) make sure the logic is consistent when translating between
* cooling states and frequencies.
*
* Return: 0 on success, -EINVAL when invalid parameters are passed.
*/
static int get_property(unsigned int gpu, unsigned long input,
unsigned int *output,
enum gpufreq_cooling_property property)
{
int i;
unsigned long max_level = 0, level = 0;
unsigned int freq = CPUFREQ_ENTRY_INVALID;
int descend = -1;
struct cpufreq_frequency_table *pos, *table =
gpu_freq_table;
if (!output)
return -EINVAL;
cpufreq_for_each_valid_entry(pos, table) {
/* ignore duplicate entry */
if (freq == pos->frequency)
continue;
/* get the frequency order */
if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
descend = freq > pos->frequency;
freq = pos->frequency;
max_level++;
}
/* No valid cpu frequency entry */
if (max_level == 0)
return -EINVAL;
/* max_level is an index, not a counter */
max_level--;
/* get max level */
if (property == GET_MAXL) {
*output = (unsigned int)max_level;
return 0;
}
if (property == GET_FREQ)
level = descend ? input : (max_level - input);
i = 0;
cpufreq_for_each_valid_entry(pos, table) {
/* ignore duplicate entry */
if (freq == pos->frequency)
continue;
/* now we have a valid frequency entry */
freq = pos->frequency;
if (property == GET_LEVEL && (unsigned int)input == freq) {
/* get level by frequency */
*output = (unsigned int)(descend ? i : (max_level - i));
return 0;
}
if (property == GET_FREQ && level == i) {
/* get frequency by level */
*output = freq;
return 0;
}
i++;
}
return -EINVAL;
}
/**
* gpufreq_cooling_get_level - for a give gpu, return the cooling level.
* @gpu: gpu for which the level is required
* @freq: the frequency of interest
*
* This function will match the cooling level corresponding to the
* requested @freq and return it.
*
* Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
* otherwise.
*/
unsigned long gpufreq_cooling_get_level(unsigned int gpu, unsigned int freq)
{
unsigned int val;
if (get_property(gpu, (unsigned long)freq, &val, GET_LEVEL))
return THERMAL_CSTATE_INVALID;
return (unsigned long)val;
}
EXPORT_SYMBOL_GPL(gpufreq_cooling_get_level);
/**
* build_dyn_power_table() - create a dynamic power to frequency table
* @gpufreq_device: the gpufreq cooling device in which to store the table
* @capacitance: dynamic power coefficient for these gpus
*
* Build a dynamic power to frequency table for this gpu and store it
* in @gpufreq_device. This table will be used in gpu_power_to_freq() and
* gpu_freq_to_power() to convert between power and frequency
* efficiently. Power is stored in mW, frequency in KHz. The
* resulting table is in ascending order.
*
* Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
* -ENOMEM if we run out of memory or -EAGAIN if an OPP was
* added/enabled while the function was executing.
*/
static int build_dyn_power_table(struct gpufreq_cooling_device *gpufreq_device,
u32 capacitance)
{
struct power_table *power_table;
int num_opps = 0, i, cnt = 0;
unsigned long freq;
num_opps = gpu_dvfs_get_step();
if (num_opps == 0)
return -EINVAL;
power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
if (!power_table)
return -ENOMEM;
for (freq = 0, i = 0; i < num_opps; i++) {
u32 voltage_mv;
u64 power;
freq = gpu_dvfs_get_clock(num_opps - i - 1);
if (freq > gpu_dvfs_get_max_freq())
continue;
voltage_mv = gpu_dvfs_get_voltage(freq) / 1000;
/*
* Do the multiplication with MHz and millivolt so as
* to not overflow.
*/
power = (u64)capacitance * freq * voltage_mv * voltage_mv;
do_div(power, 1000000000);
power_table[i].frequency = (unsigned int)freq;
/* power is stored in mW */
power_table[i].power = power;
cnt++;
}
gpufreq_device->dyn_power_table = power_table;
gpufreq_device->dyn_power_table_entries = cnt;
return 0;
}
static int build_static_power_table(struct gpufreq_cooling_device *gpufreq_device)
{
int i, j;
int ratio = cal_asv_get_ids_info(ACPM_DVFS_G3D);
int asv_group = cal_asv_get_grp(ACPM_DVFS_G3D);
void *gen_block;
struct ect_gen_param_table *volt_temp_param, *asv_param;
int ratio_table[16] = { 0, 25, 29, 35, 41, 48, 57, 67, 79, 94, 110, 130, 151, 162, 162, 162};
if (asv_group < 0 || asv_group > 15)
asv_group = 0;
if (!ratio)
ratio = ratio_table[asv_group];
gen_block = ect_get_block("GEN");
if (gen_block == NULL) {
pr_err("%s: Failed to get gen block from ECT\n", __func__);
return -EINVAL;
}
volt_temp_param = ect_gen_param_get_table(gen_block, "DTM_G3D_VOLT_TEMP");
asv_param = ect_gen_param_get_table(gen_block, "DTM_G3D_ASV");
if (volt_temp_param && asv_param) {
gpufreq_device->var_volt_size = volt_temp_param->num_of_row - 1;
gpufreq_device->var_temp_size = volt_temp_param->num_of_col - 1;
gpufreq_device->var_coeff = kzalloc(sizeof(int) *
volt_temp_param->num_of_row *
volt_temp_param->num_of_col,
GFP_KERNEL);
if (!gpufreq_device->var_coeff)
goto err_mem;
gpufreq_device->asv_coeff = kzalloc(sizeof(int) *
asv_param->num_of_row *
asv_param->num_of_col,
GFP_KERNEL);
if (!gpufreq_device->asv_coeff)
goto free_var_coeff;
gpufreq_device->var_table = kzalloc(sizeof(int) *
volt_temp_param->num_of_row *
volt_temp_param->num_of_col,
GFP_KERNEL);
if (!gpufreq_device->var_table)
goto free_asv_coeff;
memcpy(gpufreq_device->var_coeff, volt_temp_param->parameter,
sizeof(int) * volt_temp_param->num_of_row * volt_temp_param->num_of_col);
memcpy(gpufreq_device->asv_coeff, asv_param->parameter,
sizeof(int) * asv_param->num_of_row * asv_param->num_of_col);
memcpy(gpufreq_device->var_table, volt_temp_param->parameter,
sizeof(int) * volt_temp_param->num_of_row * volt_temp_param->num_of_col);
} else {
pr_err("%s: Failed to get param table from ECT\n", __func__);
return -EINVAL;
}
for (i = 1; i <= gpufreq_device->var_volt_size; i++) {
long asv_coeff = (long)gpufreq_device->asv_coeff[3 * i + 0] * asv_group * asv_group
+ (long)gpufreq_device->asv_coeff[3 * i + 1] * asv_group
+ (long)gpufreq_device->asv_coeff[3 * i + 2];
asv_coeff = asv_coeff / 100;
for (j = 1; j <= gpufreq_device->var_temp_size; j++) {
long var_coeff = (long)gpufreq_device->var_coeff[i * (gpufreq_device->var_temp_size + 1) + j];
var_coeff = ratio * var_coeff * asv_coeff;
var_coeff = var_coeff / 100000;
gpufreq_device->var_table[i * (gpufreq_device->var_temp_size + 1) + j] = (int)var_coeff;
}
}
return 0;
free_asv_coeff:
kfree(gpufreq_device->asv_coeff);
free_var_coeff:
kfree(gpufreq_device->var_coeff);
err_mem:
return -ENOMEM;
}
static int lookup_static_power(struct gpufreq_cooling_device *gpufreq_device,
unsigned long voltage, int temperature, u32 *power)
{
int volt_index = 0, temp_index = 0;
int index = 0;
voltage = voltage / 1000;
temperature = temperature / 1000;
for (volt_index = 0; volt_index <= gpufreq_device->var_volt_size; volt_index++) {
if (voltage < gpufreq_device->var_table[volt_index * (gpufreq_device->var_temp_size + 1)]) {
volt_index = volt_index - 1;
break;
}
}
if (volt_index == 0)
volt_index = 1;
if (volt_index > gpufreq_device->var_volt_size)
volt_index = gpufreq_device->var_volt_size;
for (temp_index = 0; temp_index <= gpufreq_device->var_temp_size; temp_index++) {
if (temperature < gpufreq_device->var_table[temp_index]) {
temp_index = temp_index - 1;
break;
}
}
if (temp_index == 0)
temp_index = 1;
if (temp_index > gpufreq_device->var_temp_size)
temp_index = gpufreq_device->var_temp_size;
index = (int)(volt_index * (gpufreq_device->var_temp_size + 1) + temp_index);
*power = (unsigned int)gpufreq_device->var_table[index];
return 0;
}
static u32 gpu_freq_to_power(struct gpufreq_cooling_device *gpufreq_device,
u32 freq)
{
int i;
struct power_table *pt = gpufreq_device->dyn_power_table;
for (i = 1; i < gpufreq_device->dyn_power_table_entries; i++)
if (freq < pt[i].frequency)
break;
return pt[i - 1].power;
}
static u32 gpu_power_to_freq(struct gpufreq_cooling_device *gpufreq_device,
u32 power)
{
int i;
struct power_table *pt = gpufreq_device->dyn_power_table;
for (i = 1; i < gpufreq_device->dyn_power_table_entries; i++)
if (power < pt[i].power)
break;
return pt[i - 1].frequency;
}
/**
* get_static_power() - calculate the static power consumed by the gpus
* @gpufreq_device: struct &gpufreq_cooling_device for this gpu cdev
* @tz: thermal zone device in which we're operating
* @freq: frequency in KHz
* @power: pointer in which to store the calculated static power
*
* Calculate the static power consumed by the gpus described by
* @gpu_actor running at frequency @freq. This function relies on a
* platform specific function that should have been provided when the
* actor was registered. If it wasn't, the static power is assumed to
* be negligible. The calculated static power is stored in @power.
*
* Return: 0 on success, -E* on failure.
*/
static int get_static_power(struct gpufreq_cooling_device *gpufreq_device,
struct thermal_zone_device *tz, unsigned long freq,
u32 *power)
{
unsigned long voltage;
if (freq == 0) {
*power = 0;
return 0;
}
voltage = gpu_dvfs_get_voltage(freq);
if (voltage == 0) {
pr_warn("Failed to get voltage for frequency %lu\n", freq);
return -EINVAL;
}
return lookup_static_power(gpufreq_device, voltage, tz->temperature, power);
}
/**
* get_dynamic_power() - calculate the dynamic power
* @gpufreq_device: &gpufreq_cooling_device for this cdev
* @freq: current frequency
*
* Return: the dynamic power consumed by the gpus described by
* @gpufreq_device.
*/
static u32 get_dynamic_power(struct gpufreq_cooling_device *gpufreq_device,
unsigned long freq)
{
u32 raw_gpu_power;
raw_gpu_power = gpu_freq_to_power(gpufreq_device, freq);
return (raw_gpu_power * gpufreq_device->last_load) / 100;
}
/**
* gpufreq_apply_cooling - function to apply frequency clipping.
* @gpufreq_device: gpufreq_cooling_device pointer containing frequency
* clipping data.
* @cooling_state: value of the cooling state.
*
* Function used to make sure the gpufreq layer is aware of current thermal
* limits. The limits are applied by updating the gpufreq policy.
*
* Return: 0 on success, an error code otherwise (-EINVAL in case wrong
* cooling state).
*/
#if defined(CONFIG_SEC_DEBUG_HW_PARAM)
static u64 last_time[THERMAL_ZONE_MAX], curr_time[THERMAL_ZONE_MAX];
extern struct thermal_data_devices thermal_data_info[THERMAL_ZONE_MAX];
#endif
static int gpufreq_apply_cooling(struct gpufreq_cooling_device *gpufreq_device,
unsigned long cooling_state)
{
#if defined(CONFIG_SEC_DEBUG_HW_PARAM)
int tid = gpufreq_device->id + THERMAL_ZONE_GPU;
#endif
/* Check if the old cooling action is same as new cooling action */
if (gpufreq_device->gpufreq_state == cooling_state)
return 0;
gpufreq_device->gpufreq_state = cooling_state;
#if defined(CONFIG_SEC_DEBUG_HW_PARAM)
curr_time[tid] = ktime_to_ns(ktime_get()) / 1000000;
if (last_time[tid]) {
thermal_data_info[tid].freq_level[cooling_state] +=
curr_time[tid] - last_time[tid];
}
last_time[tid] = curr_time[tid];
#endif
blocking_notifier_call_chain(&gpu_notifier, GPU_THROTTLING, &cooling_state);
return 0;
}
/* gpufreq cooling device callback functions are defined below */
/**
* gpufreq_get_max_state - callback function to get the max cooling state.
* @cdev: thermal cooling device pointer.
* @state: fill this variable with the max cooling state.
*
* Callback for the thermal cooling device to return the gpufreq
* max cooling state.
*
* Return: 0 on success, an error code otherwise.
*/
static int gpufreq_get_max_state(struct thermal_cooling_device *cdev,
unsigned long *state)
{
unsigned int count = 0;
int ret;
ret = get_property(0, 0, &count, GET_MAXL);
if (count > 0)
*state = count;
return ret;
}
/**
* gpufreq_get_cur_state - callback function to get the current cooling state.
* @cdev: thermal cooling device pointer.
* @state: fill this variable with the current cooling state.
*
* Callback for the thermal cooling device to return the gpufreq
* current cooling state.
*
* Return: 0 on success, an error code otherwise.
*/
static int gpufreq_get_cur_state(struct thermal_cooling_device *cdev,
unsigned long *state)
{
struct gpufreq_cooling_device *gpufreq_device = cdev->devdata;
*state = gpufreq_device->gpufreq_state;
return 0;
}
/**
* gpufreq_set_cur_state - callback function to set the current cooling state.
* @cdev: thermal cooling device pointer.
* @state: set this variable to the current cooling state.
*
* Callback for the thermal cooling device to change the gpufreq
* current cooling state.
*
* Return: 0 on success, an error code otherwise.
*/
static int gpufreq_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long state)
{
struct gpufreq_cooling_device *gpufreq_device = cdev->devdata;
#if defined(CONFIG_SEC_DEBUG_HW_PARAM)
unsigned long max_level;
gpufreq_get_max_state(cdev, &max_level);
if (WARN_ON(state > max_level))
return -EINVAL;
thermal_data_info[gpufreq_device->id + THERMAL_ZONE_GPU].max_level = max_level;
#endif
return gpufreq_apply_cooling(gpufreq_device, state);
}
static enum gpu_noti_state_t gpu_tstate = GPU_COLD;
static int gpufreq_set_cur_temp(struct thermal_cooling_device *cdev,
bool suspended, int temp)
{
enum gpu_noti_state_t tstate;
unsigned long value;
if (suspended || temp < EXYNOS_COLD_TEMP)
tstate = GPU_COLD;
else
tstate = GPU_NORMAL;
if (gpu_tstate == tstate)
return 0;
gpu_tstate = tstate;
value = tstate;
blocking_notifier_call_chain(&gpu_notifier, tstate, &value);
return 0;
}
/**
* gpufreq_get_requested_power() - get the current power
* @cdev: &thermal_cooling_device pointer
* @tz: a valid thermal zone device pointer
* @power: pointer in which to store the resulting power
*
* Calculate the current power consumption of the gpus in milliwatts
* and store it in @power. This function should actually calculate
* the requested power, but it's hard to get the frequency that
* gpufreq would have assigned if there were no thermal limits.
* Instead, we calculate the current power on the assumption that the
* immediate future will look like the immediate past.
*
* We use the current frequency and the average load since this
* function was last called. In reality, there could have been
* multiple opps since this function was last called and that affects
* the load calculation. While it's not perfectly accurate, this
* simplification is good enough and works. REVISIT this, as more
* complex code may be needed if experiments show that it's not
* accurate enough.
*
* Return: 0 on success, -E* if getting the static power failed.
*/
static int gpufreq_get_requested_power(struct thermal_cooling_device *cdev,
struct thermal_zone_device *tz,
u32 *power)
{
unsigned long freq;
int ret = 0;
u32 static_power, dynamic_power;
struct gpufreq_cooling_device *gpufreq_device = cdev->devdata;
u32 load_gpu = 0;
freq = gpu_dvfs_get_cur_clock();
load_gpu = gpu_dvfs_get_utilization();;
gpufreq_device->last_load = load_gpu;
dynamic_power = get_dynamic_power(gpufreq_device, freq);
ret = get_static_power(gpufreq_device, tz, freq, &static_power);
if (ret)
return ret;
if (trace_thermal_power_gpu_get_power_enabled()) {
trace_thermal_power_gpu_get_power(
freq, load_gpu, dynamic_power, static_power);
}
*power = static_power + dynamic_power;
return 0;
}
/**
* gpufreq_state2power() - convert a gpu cdev state to power consumed
* @cdev: &thermal_cooling_device pointer
* @tz: a valid thermal zone device pointer
* @state: cooling device state to be converted
* @power: pointer in which to store the resulting power
*
* Convert cooling device state @state into power consumption in
* milliwatts assuming 100% load. Store the calculated power in
* @power.
*
* Return: 0 on success, -EINVAL if the cooling device state could not
* be converted into a frequency or other -E* if there was an error
* when calculating the static power.
*/
static int gpufreq_state2power(struct thermal_cooling_device *cdev,
struct thermal_zone_device *tz,
unsigned long state, u32 *power)
{
unsigned int freq;
u32 static_power, dynamic_power;
int ret;
struct gpufreq_cooling_device *gpufreq_device = cdev->devdata;
freq = gpu_freq_table[state].frequency / 1000;
if (!freq)
return -EINVAL;
dynamic_power = gpu_freq_to_power(gpufreq_device, freq);
ret = get_static_power(gpufreq_device, tz, freq, &static_power);
if (ret)
return ret;
*power = static_power + dynamic_power;
return 0;
}
/**
* gpufreq_power2state() - convert power to a cooling device state
* @cdev: &thermal_cooling_device pointer
* @tz: a valid thermal zone device pointer
* @power: power in milliwatts to be converted
* @state: pointer in which to store the resulting state
*
* Calculate a cooling device state for the gpus described by @cdev
* that would allow them to consume at most @power mW and store it in
* @state. Note that this calculation depends on external factors
* such as the gpu load or the current static power. Calling this
* function with the same power as input can yield different cooling
* device states depending on those external factors.
*
* Return: 0 on success, -ENODEV if no gpus are online or -EINVAL if
* the calculated frequency could not be converted to a valid state.
* The latter should not happen unless the frequencies available to
* gpufreq have changed since the initialization of the gpu cooling
* device.
*/
static int gpufreq_power2state(struct thermal_cooling_device *cdev,
struct thermal_zone_device *tz, u32 power,
unsigned long *state)
{
unsigned int cur_freq, target_freq;
int ret;
s32 dyn_power;
u32 static_power;
struct gpufreq_cooling_device *gpufreq_device = cdev->devdata;
cur_freq = gpu_dvfs_get_cur_clock();
ret = get_static_power(gpufreq_device, tz, cur_freq, &static_power);
if (ret)
return ret;
dyn_power = power - static_power;
dyn_power = dyn_power > 0 ? dyn_power : 0;
target_freq = gpu_power_to_freq(gpufreq_device, dyn_power);
*state = gpufreq_cooling_get_level(0, target_freq * 1000);
if (*state == THERMAL_CSTATE_INVALID) {
pr_warn("Failed to convert %dKHz for gpu into a cdev state\n",
target_freq);
return -EINVAL;
}
trace_thermal_power_gpu_limit(target_freq, *state, power);
return 0;
}
/* Bind gpufreq callbacks to thermal cooling device ops */
static struct thermal_cooling_device_ops gpufreq_cooling_ops = {
.get_max_state = gpufreq_get_max_state,
.get_cur_state = gpufreq_get_cur_state,
.set_cur_state = gpufreq_set_cur_state,
.set_cur_temp = gpufreq_set_cur_temp,
};
int exynos_gpu_add_notifier(struct notifier_block *n)
{
return blocking_notifier_chain_register(&gpu_notifier, n);
}
/**
* __gpufreq_cooling_register - helper function to create gpufreq cooling device
* @np: a valid struct device_node to the cooling device device tree node
* @clip_gpus: gpumask of gpus where the frequency constraints will happen.
* @capacitance: dynamic power coefficient for these gpus
* @plat_static_func: function to calculate the static power consumed by these
* gpus (optional)
*
* This interface function registers the gpufreq cooling device with the name
* "thermal-gpufreq-%x". This api can support multiple instances of gpufreq
* cooling devices. It also gives the opportunity to link the cooling device
* with a device tree node, in order to bind it via the thermal DT code.
*
* Return: a valid struct thermal_cooling_device pointer on success,
* on failure, it returns a corresponding ERR_PTR().
*/
static struct thermal_cooling_device *
__gpufreq_cooling_register(struct device_node *np,
const struct cpumask *clip_gpus, u32 capacitance,
get_static_t plat_static_func)
{
struct thermal_cooling_device *cool_dev;
struct gpufreq_cooling_device *gpufreq_dev = NULL;
char dev_name[THERMAL_NAME_LENGTH];
int ret = 0;
gpufreq_dev = kzalloc(sizeof(struct gpufreq_cooling_device),
GFP_KERNEL);
if (!gpufreq_dev)
return ERR_PTR(-ENOMEM);
ret = get_idr(&gpufreq_idr, &gpufreq_dev->id);
if (ret) {
kfree(gpufreq_dev);
return ERR_PTR(-EINVAL);
}
if (capacitance) {
gpufreq_cooling_ops.get_requested_power =
gpufreq_get_requested_power;
gpufreq_cooling_ops.state2power = gpufreq_state2power;
gpufreq_cooling_ops.power2state = gpufreq_power2state;
ret = build_dyn_power_table(gpufreq_dev, capacitance);
if (ret)
return ERR_PTR(ret);
ret = build_static_power_table(gpufreq_dev);
if (ret)
return ERR_PTR(ret);
}
snprintf(dev_name, sizeof(dev_name), "thermal-gpufreq-%d",
gpufreq_dev->id);
cool_dev = thermal_of_cooling_device_register(np, dev_name, gpufreq_dev,
&gpufreq_cooling_ops);
if (IS_ERR(cool_dev)) {
release_idr(&gpufreq_idr, gpufreq_dev->id);
kfree(gpufreq_dev);
return cool_dev;
}
gpufreq_dev->cool_dev = cool_dev;
gpufreq_dev->gpufreq_state = 0;
mutex_lock(&cooling_gpu_lock);
gpufreq_dev_count++;
mutex_unlock(&cooling_gpu_lock);
return cool_dev;
}
/**
* gpufreq_cooling_register - function to create gpufreq cooling device.
* @clip_gpus: cpumask of gpus where the frequency constraints will happen.
*
* This interface function registers the gpufreq cooling device with the name
* "thermal-gpufreq-%x". This api can support multiple instances of gpufreq
* cooling devices.
*
* Return: a valid struct thermal_cooling_device pointer on success,
* on failure, it returns a corresponding ERR_PTR().
*/
struct thermal_cooling_device *
gpufreq_cooling_register(const struct cpumask *clip_gpus)
{
return __gpufreq_cooling_register(NULL, clip_gpus, 0, NULL);
}
EXPORT_SYMBOL_GPL(gpufreq_cooling_register);
/**
* of_gpufreq_cooling_register - function to create gpufreq cooling device.
* @np: a valid struct device_node to the cooling device device tree node
* @clip_gpus: cpumask of gpus where the frequency constraints will happen.
*
* This interface function registers the gpufreq cooling device with the name
* "thermal-gpufreq-%x". This api can support multiple instances of gpufreq
* cooling devices. Using this API, the gpufreq cooling device will be
* linked to the device tree node provided.
*
* Return: a valid struct thermal_cooling_device pointer on success,
* on failure, it returns a corresponding ERR_PTR().
*/
struct thermal_cooling_device *
of_gpufreq_cooling_register(struct device_node *np,
const struct cpumask *clip_gpus)
{
if (!np)
return ERR_PTR(-EINVAL);
return __gpufreq_cooling_register(np, clip_gpus, 0, NULL);
}
EXPORT_SYMBOL_GPL(of_gpufreq_cooling_register);
/**
* gpufreq_power_cooling_register() - create gpufreq cooling device with power extensions
* @clip_gpus: gpumask of gpus where the frequency constraints will happen
* @capacitance: dynamic power coefficient for these gpus
* @plat_static_func: function to calculate the static power consumed by these
* gpus (optional)
*
* This interface function registers the gpufreq cooling device with
* the name "thermal-gpufreq-%x". This api can support multiple
* instances of gpufreq cooling devices. Using this function, the
* cooling device will implement the power extensions by using a
* simple gpu power model. The gpus must have registered their OPPs
* using the OPP library.
*
* An optional @plat_static_func may be provided to calculate the
* static power consumed by these gpus. If the platform's static
* power consumption is unknown or negligible, make it NULL.
*
* Return: a valid struct thermal_cooling_device pointer on success,
* on failure, it returns a corresponding ERR_PTR().
*/
struct thermal_cooling_device *
gpufreq_power_cooling_register(const struct cpumask *clip_gpus, u32 capacitance,
get_static_t plat_static_func)
{
return __gpufreq_cooling_register(NULL, clip_gpus, capacitance,
plat_static_func);
}
EXPORT_SYMBOL(gpufreq_power_cooling_register);
/**
* of_gpufreq_power_cooling_register() - create gpufreq cooling device with power extensions
* @np: a valid struct device_node to the cooling device device tree node
* @clip_gpus: gpumask of gpus where the frequency constraints will happen
* @capacitance: dynamic power coefficient for these gpus
* @plat_static_func: function to calculate the static power consumed by these
* gpus (optional)
*
* This interface function registers the gpufreq cooling device with
* the name "thermal-gpufreq-%x". This api can support multiple
* instances of gpufreq cooling devices. Using this API, the gpufreq
* cooling device will be linked to the device tree node provided.
* Using this function, the cooling device will implement the power
* extensions by using a simple gpu power model. The gpus must have
* registered their OPPs using the OPP library.
*
* An optional @plat_static_func may be provided to calculate the
* static power consumed by these gpus. If the platform's static
* power consumption is unknown or negligible, make it NULL.
*
* Return: a valid struct thermal_cooling_device pointer on success,
* on failure, it returns a corresponding ERR_PTR().
*/
struct thermal_cooling_device *
of_gpufreq_power_cooling_register(struct device_node *np,
const struct cpumask *clip_gpus,
u32 capacitance,
get_static_t plat_static_func)
{
if (!np)
return ERR_PTR(-EINVAL);
return __gpufreq_cooling_register(np, clip_gpus, capacitance,
plat_static_func);
}
EXPORT_SYMBOL(of_gpufreq_power_cooling_register);
/**
* gpufreq_cooling_unregister - function to remove gpufreq cooling device.
* @cdev: thermal cooling device pointer.
*
* This interface function unregisters the "thermal-gpufreq-%x" cooling device.
*/
void gpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
{
struct gpufreq_cooling_device *gpufreq_dev;
if (!cdev)
return;
gpufreq_dev = cdev->devdata;
mutex_lock(&cooling_gpu_lock);
gpufreq_dev_count--;
mutex_unlock(&cooling_gpu_lock);
thermal_cooling_device_unregister(gpufreq_dev->cool_dev);
release_idr(&gpufreq_idr, gpufreq_dev->id);
kfree(gpufreq_dev);
}
EXPORT_SYMBOL_GPL(gpufreq_cooling_unregister);
|