1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/cpu.h>
14#include <linux/cpufreq.h>
15#include <linux/cpu_cooling.h>
16#include <linux/device.h>
17#include <linux/energy_model.h>
18#include <linux/err.h>
19#include <linux/export.h>
20#include <linux/pm_opp.h>
21#include <linux/pm_qos.h>
22#include <linux/slab.h>
23#include <linux/thermal.h>
24
25#include <trace/events/thermal.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46struct time_in_idle {
47 u64 time;
48 u64 timestamp;
49};
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68struct cpufreq_cooling_device {
69 u32 last_load;
70 unsigned int cpufreq_state;
71 unsigned int max_level;
72 struct em_perf_domain *em;
73 struct cpufreq_policy *policy;
74#ifndef CONFIG_SMP
75 struct time_in_idle *idle_time;
76#endif
77 struct freq_qos_request qos_req;
78};
79
80#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
81
82
83
84
85
86
87
88static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
89 unsigned int freq)
90{
91 int i;
92
93 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
94 if (freq > cpufreq_cdev->em->table[i].frequency)
95 break;
96 }
97
98 return cpufreq_cdev->max_level - i - 1;
99}
100
101static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
102 u32 freq)
103{
104 int i;
105
106 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
107 if (freq > cpufreq_cdev->em->table[i].frequency)
108 break;
109 }
110
111 return cpufreq_cdev->em->table[i + 1].power;
112}
113
114static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
115 u32 power)
116{
117 int i;
118
119 for (i = cpufreq_cdev->max_level; i > 0; i--) {
120 if (power >= cpufreq_cdev->em->table[i].power)
121 break;
122 }
123
124 return cpufreq_cdev->em->table[i].frequency;
125}
126
127
128
129
130
131
132
133
134
135
136#ifdef CONFIG_SMP
137static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
138 int cpu_idx)
139{
140 unsigned long max = arch_scale_cpu_capacity(cpu);
141 unsigned long util;
142
143 util = sched_cpu_util(cpu, max);
144 return (util * 100) / max;
145}
146#else
147static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
148 int cpu_idx)
149{
150 u32 load;
151 u64 now, now_idle, delta_time, delta_idle;
152 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
153
154 now_idle = get_cpu_idle_time(cpu, &now, 0);
155 delta_idle = now_idle - idle_time->time;
156 delta_time = now - idle_time->timestamp;
157
158 if (delta_time <= delta_idle)
159 load = 0;
160 else
161 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
162
163 idle_time->time = now_idle;
164 idle_time->timestamp = now;
165
166 return load;
167}
168#endif
169
170
171
172
173
174
175
176
177
178static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
179 unsigned long freq)
180{
181 u32 raw_cpu_power;
182
183 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
184 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
185}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
210 u32 *power)
211{
212 unsigned long freq;
213 int i = 0, cpu;
214 u32 total_load = 0;
215 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
216 struct cpufreq_policy *policy = cpufreq_cdev->policy;
217 u32 *load_cpu = NULL;
218
219 freq = cpufreq_quick_get(policy->cpu);
220
221 if (trace_thermal_power_cpu_get_power_enabled()) {
222 u32 ncpus = cpumask_weight(policy->related_cpus);
223
224 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
225 }
226
227 for_each_cpu(cpu, policy->related_cpus) {
228 u32 load;
229
230 if (cpu_online(cpu))
231 load = get_load(cpufreq_cdev, cpu, i);
232 else
233 load = 0;
234
235 total_load += load;
236 if (load_cpu)
237 load_cpu[i] = load;
238
239 i++;
240 }
241
242 cpufreq_cdev->last_load = total_load;
243
244 *power = get_dynamic_power(cpufreq_cdev, freq);
245
246 if (load_cpu) {
247 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
248 load_cpu, i, *power);
249
250 kfree(load_cpu);
251 }
252
253 return 0;
254}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270static int cpufreq_state2power(struct thermal_cooling_device *cdev,
271 unsigned long state, u32 *power)
272{
273 unsigned int freq, num_cpus, idx;
274 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
275
276
277 if (state > cpufreq_cdev->max_level)
278 return -EINVAL;
279
280 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
281
282 idx = cpufreq_cdev->max_level - state;
283 freq = cpufreq_cdev->em->table[idx].frequency;
284 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
285
286 return 0;
287}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308static int cpufreq_power2state(struct thermal_cooling_device *cdev,
309 u32 power, unsigned long *state)
310{
311 unsigned int target_freq;
312 u32 last_load, normalised_power;
313 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
314 struct cpufreq_policy *policy = cpufreq_cdev->policy;
315
316 last_load = cpufreq_cdev->last_load ?: 1;
317 normalised_power = (power * 100) / last_load;
318 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
319
320 *state = get_level(cpufreq_cdev, target_freq);
321 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
322 power);
323 return 0;
324}
325
326static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
327 struct em_perf_domain *em) {
328 struct cpufreq_policy *policy;
329 unsigned int nr_levels;
330
331 if (!em)
332 return false;
333
334 policy = cpufreq_cdev->policy;
335 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
336 pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
337 cpumask_pr_args(em_span_cpus(em)),
338 cpumask_pr_args(policy->related_cpus));
339 return false;
340 }
341
342 nr_levels = cpufreq_cdev->max_level + 1;
343 if (em_pd_nr_perf_states(em) != nr_levels) {
344 pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
345 cpumask_pr_args(em_span_cpus(em)),
346 em_pd_nr_perf_states(em), nr_levels);
347 return false;
348 }
349
350 return true;
351}
352#endif
353
354#ifdef CONFIG_SMP
355static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
356{
357 return 0;
358}
359
360static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
361{
362}
363#else
364static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
365{
366 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
367
368 cpufreq_cdev->idle_time = kcalloc(num_cpus,
369 sizeof(*cpufreq_cdev->idle_time),
370 GFP_KERNEL);
371 if (!cpufreq_cdev->idle_time)
372 return -ENOMEM;
373
374 return 0;
375}
376
377static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
378{
379 kfree(cpufreq_cdev->idle_time);
380 cpufreq_cdev->idle_time = NULL;
381}
382#endif
383
384static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
385 unsigned long state)
386{
387 struct cpufreq_policy *policy;
388 unsigned long idx;
389
390#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
391
392 if (cpufreq_cdev->em) {
393 idx = cpufreq_cdev->max_level - state;
394 return cpufreq_cdev->em->table[idx].frequency;
395 }
396#endif
397
398
399 policy = cpufreq_cdev->policy;
400 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
401 idx = cpufreq_cdev->max_level - state;
402 else
403 idx = state;
404
405 return policy->freq_table[idx].frequency;
406}
407
408
409
410
411
412
413
414
415
416
417
418
419
420static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
421 unsigned long *state)
422{
423 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
424
425 *state = cpufreq_cdev->max_level;
426 return 0;
427}
428
429
430
431
432
433
434
435
436
437
438
439static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
440 unsigned long *state)
441{
442 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
443
444 *state = cpufreq_cdev->cpufreq_state;
445
446 return 0;
447}
448
449
450
451
452
453
454
455
456
457
458
459static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
460 unsigned long state)
461{
462 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
463 struct cpumask *cpus;
464 unsigned int frequency;
465 unsigned long max_capacity, capacity;
466 int ret;
467
468
469 if (state > cpufreq_cdev->max_level)
470 return -EINVAL;
471
472
473 if (cpufreq_cdev->cpufreq_state == state)
474 return 0;
475
476 frequency = get_state_freq(cpufreq_cdev, state);
477
478 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
479 if (ret >= 0) {
480 cpufreq_cdev->cpufreq_state = state;
481 cpus = cpufreq_cdev->policy->related_cpus;
482 max_capacity = arch_scale_cpu_capacity(cpumask_first(cpus));
483 capacity = frequency * max_capacity;
484 capacity /= cpufreq_cdev->policy->cpuinfo.max_freq;
485 arch_set_thermal_pressure(cpus, max_capacity - capacity);
486 ret = 0;
487 }
488
489 return ret;
490}
491
492
493
494static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
495 .get_max_state = cpufreq_get_max_state,
496 .get_cur_state = cpufreq_get_cur_state,
497 .set_cur_state = cpufreq_set_cur_state,
498};
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515static struct thermal_cooling_device *
516__cpufreq_cooling_register(struct device_node *np,
517 struct cpufreq_policy *policy,
518 struct em_perf_domain *em)
519{
520 struct thermal_cooling_device *cdev;
521 struct cpufreq_cooling_device *cpufreq_cdev;
522 unsigned int i;
523 struct device *dev;
524 int ret;
525 struct thermal_cooling_device_ops *cooling_ops;
526 char *name;
527
528 dev = get_cpu_device(policy->cpu);
529 if (unlikely(!dev)) {
530 pr_warn("No cpu device for cpu %d\n", policy->cpu);
531 return ERR_PTR(-ENODEV);
532 }
533
534 if (IS_ERR_OR_NULL(policy)) {
535 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
536 return ERR_PTR(-EINVAL);
537 }
538
539 i = cpufreq_table_count_valid_entries(policy);
540 if (!i) {
541 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
542 __func__);
543 return ERR_PTR(-ENODEV);
544 }
545
546 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
547 if (!cpufreq_cdev)
548 return ERR_PTR(-ENOMEM);
549
550 cpufreq_cdev->policy = policy;
551
552 ret = allocate_idle_time(cpufreq_cdev);
553 if (ret) {
554 cdev = ERR_PTR(ret);
555 goto free_cdev;
556 }
557
558
559 cpufreq_cdev->max_level = i - 1;
560
561 cooling_ops = &cpufreq_cooling_ops;
562
563#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
564 if (em_is_sane(cpufreq_cdev, em)) {
565 cpufreq_cdev->em = em;
566 cooling_ops->get_requested_power = cpufreq_get_requested_power;
567 cooling_ops->state2power = cpufreq_state2power;
568 cooling_ops->power2state = cpufreq_power2state;
569 } else
570#endif
571 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
572 pr_err("%s: unsorted frequency tables are not supported\n",
573 __func__);
574 cdev = ERR_PTR(-EINVAL);
575 goto free_idle_time;
576 }
577
578 ret = freq_qos_add_request(&policy->constraints,
579 &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
580 get_state_freq(cpufreq_cdev, 0));
581 if (ret < 0) {
582 pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
583 ret);
584 cdev = ERR_PTR(ret);
585 goto free_idle_time;
586 }
587
588 cdev = ERR_PTR(-ENOMEM);
589 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
590 if (!name)
591 goto remove_qos_req;
592
593 cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
594 cooling_ops);
595 kfree(name);
596
597 if (IS_ERR(cdev))
598 goto remove_qos_req;
599
600 return cdev;
601
602remove_qos_req:
603 freq_qos_remove_request(&cpufreq_cdev->qos_req);
604free_idle_time:
605 free_idle_time(cpufreq_cdev);
606free_cdev:
607 kfree(cpufreq_cdev);
608 return cdev;
609}
610
611
612
613
614
615
616
617
618
619
620
621
622struct thermal_cooling_device *
623cpufreq_cooling_register(struct cpufreq_policy *policy)
624{
625 return __cpufreq_cooling_register(NULL, policy, NULL);
626}
627EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648struct thermal_cooling_device *
649of_cpufreq_cooling_register(struct cpufreq_policy *policy)
650{
651 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
652 struct thermal_cooling_device *cdev = NULL;
653
654 if (!np) {
655 pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
656 policy->cpu);
657 return NULL;
658 }
659
660 if (of_find_property(np, "#cooling-cells", NULL)) {
661 struct em_perf_domain *em = em_cpu_get(policy->cpu);
662
663 cdev = __cpufreq_cooling_register(np, policy, em);
664 if (IS_ERR(cdev)) {
665 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
666 policy->cpu, PTR_ERR(cdev));
667 cdev = NULL;
668 }
669 }
670
671 of_node_put(np);
672 return cdev;
673}
674EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
675
676
677
678
679
680
681
682void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
683{
684 struct cpufreq_cooling_device *cpufreq_cdev;
685
686 if (!cdev)
687 return;
688
689 cpufreq_cdev = cdev->devdata;
690
691 thermal_cooling_device_unregister(cdev);
692 freq_qos_remove_request(&cpufreq_cdev->qos_req);
693 free_idle_time(cpufreq_cdev);
694 kfree(cpufreq_cdev);
695}
696EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
697