Lines Matching +full:cooling +full:- +full:device
1 // SPDX-License-Identifier: GPL-2.0
7 * Copyright (C) 2012-2018 Linaro Limited.
16 #include <linux/device.h>
29 * Cooling state <-> CPUFreq frequency
31 * Cooling states are translated to frequencies throughout this driver and this
34 * Highest cooling state corresponds to lowest possible frequency.
37 * level 0 --> 1st Max Freq
38 * level 1 --> 2nd Max Freq
43 * struct time_in_idle - Idle time stats
53 * struct cpufreq_cooling_device - data for cooling device with cpufreq
56 * cooling devices.
57 * @max_level: maximum cooling level. One less than total number of valid
59 * @em: Reference on the Energy Model of the device
61 * @cooling_ops: cpufreq callbacks to thermal cooling device ops
96 table = em_perf_state_from_pd(cpufreq_cdev->em); in get_level()
97 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { in get_level()
103 return cpufreq_cdev->max_level - i - 1; in get_level()
114 table = em_perf_state_from_pd(cpufreq_cdev->em); in cpu_freq_to_power()
115 for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) { in cpu_freq_to_power()
136 table = em_perf_state_from_pd(cpufreq_cdev->em); in cpu_power_to_freq()
137 for (i = cpufreq_cdev->max_level; i > 0; i--) { in cpu_power_to_freq()
138 /* Convert EM power to milli-Watts to make safe comparison */ in cpu_power_to_freq()
151 * get_load() - get load for a cpu
173 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; in get_load()
176 delta_idle = now_idle - idle_time->time; in get_load()
177 delta_time = now - idle_time->timestamp; in get_load()
182 load = div64_u64(100 * (delta_time - delta_idle), delta_time); in get_load()
184 idle_time->time = now_idle; in get_load()
185 idle_time->timestamp = now; in get_load()
192 * get_dynamic_power() - calculate the dynamic power
205 return (raw_cpu_power * cpufreq_cdev->last_load) / 100; in get_dynamic_power()
209 * cpufreq_get_requested_power() - get the current power
236 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_get_requested_power()
237 struct cpufreq_policy *policy = cpufreq_cdev->policy; in cpufreq_get_requested_power()
239 freq = cpufreq_quick_get(policy->cpu); in cpufreq_get_requested_power()
241 for_each_cpu(cpu, policy->related_cpus) { in cpufreq_get_requested_power()
252 cpufreq_cdev->last_load = total_load; in cpufreq_get_requested_power()
256 trace_thermal_power_cpu_get_power_simple(policy->cpu, *power); in cpufreq_get_requested_power()
262 * cpufreq_state2power() - convert a cpu cdev state to power consumed
264 * @state: cooling device state to be converted
267 * Convert cooling device state @state into power consumption in
271 * Return: 0 on success, -EINVAL if the cooling device state is bigger
277 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_state2power()
282 if (state > cpufreq_cdev->max_level) in cpufreq_state2power()
283 return -EINVAL; in cpufreq_state2power()
285 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); in cpufreq_state2power()
287 idx = cpufreq_cdev->max_level - state; in cpufreq_state2power()
290 table = em_perf_state_from_pd(cpufreq_cdev->em); in cpufreq_state2power()
300 * cpufreq_power2state() - convert power to a cooling device state
305 * Calculate a cooling device state for the cpus described by @cdev
309 * as input can yield different cooling device states depending on those
319 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_power2state()
320 struct cpufreq_policy *policy = cpufreq_cdev->policy; in cpufreq_power2state()
322 last_load = cpufreq_cdev->last_load ?: 1; in cpufreq_power2state()
327 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, in cpufreq_power2state()
340 policy = cpufreq_cdev->policy; in em_is_sane()
341 if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) { in em_is_sane()
344 cpumask_pr_args(policy->related_cpus)); in em_is_sane()
348 nr_levels = cpufreq_cdev->max_level + 1; in em_is_sane()
350 …pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling leve… in em_is_sane()
372 unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus); in allocate_idle_time()
374 cpufreq_cdev->idle_time = kcalloc(num_cpus, in allocate_idle_time()
375 sizeof(*cpufreq_cdev->idle_time), in allocate_idle_time()
377 if (!cpufreq_cdev->idle_time) in allocate_idle_time()
378 return -ENOMEM; in allocate_idle_time()
385 kfree(cpufreq_cdev->idle_time); in free_idle_time()
386 cpufreq_cdev->idle_time = NULL; in free_idle_time()
398 if (cpufreq_cdev->em) { in get_state_freq()
402 idx = cpufreq_cdev->max_level - state; in get_state_freq()
405 table = em_perf_state_from_pd(cpufreq_cdev->em); in get_state_freq()
414 policy = cpufreq_cdev->policy; in get_state_freq()
415 if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) in get_state_freq()
416 idx = cpufreq_cdev->max_level - state; in get_state_freq()
420 return policy->freq_table[idx].frequency; in get_state_freq()
423 /* cpufreq cooling device callback functions are defined below */
426 * cpufreq_get_max_state - callback function to get the max cooling state.
427 * @cdev: thermal cooling device pointer.
428 * @state: fill this variable with the max cooling state.
430 * Callback for the thermal cooling device to return the cpufreq
431 * max cooling state.
438 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_get_max_state()
440 *state = cpufreq_cdev->max_level; in cpufreq_get_max_state()
445 * cpufreq_get_cur_state - callback function to get the current cooling state.
446 * @cdev: thermal cooling device pointer.
447 * @state: fill this variable with the current cooling state.
449 * Callback for the thermal cooling device to return the cpufreq
450 * current cooling state.
457 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_get_cur_state()
459 *state = cpufreq_cdev->cpufreq_state; in cpufreq_get_cur_state()
465 * cpufreq_set_cur_state - callback function to set the current cooling state.
466 * @cdev: thermal cooling device pointer.
467 * @state: set this variable to the current cooling state.
469 * Callback for the thermal cooling device to change the cpufreq
470 * current cooling state.
477 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; in cpufreq_set_cur_state()
482 if (state > cpufreq_cdev->max_level) in cpufreq_set_cur_state()
483 return -EINVAL; in cpufreq_set_cur_state()
485 /* Check if the old cooling action is same as new cooling action */ in cpufreq_set_cur_state()
486 if (cpufreq_cdev->cpufreq_state == state) in cpufreq_set_cur_state()
491 ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency); in cpufreq_set_cur_state()
493 cpufreq_cdev->cpufreq_state = state; in cpufreq_set_cur_state()
501 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
502 * @np: a valid struct device_node to the cooling device tree node
504 * Normally this should be same as cpufreq policy->related_cpus.
507 * This interface function registers the cpufreq cooling device with the name
508 * "cpufreq-%s". This API can support multiple instances of cpufreq
509 * cooling devices. It also gives the opportunity to link the cooling device
510 * with a device tree node, in order to bind it via the thermal DT code.
523 struct device *dev; in __cpufreq_cooling_register()
530 return ERR_PTR(-EINVAL); in __cpufreq_cooling_register()
533 dev = get_cpu_device(policy->cpu); in __cpufreq_cooling_register()
535 pr_warn("No cpu device for cpu %d\n", policy->cpu); in __cpufreq_cooling_register()
536 return ERR_PTR(-ENODEV); in __cpufreq_cooling_register()
543 return ERR_PTR(-ENODEV); in __cpufreq_cooling_register()
548 return ERR_PTR(-ENOMEM); in __cpufreq_cooling_register()
550 cpufreq_cdev->policy = policy; in __cpufreq_cooling_register()
559 cpufreq_cdev->max_level = i - 1; in __cpufreq_cooling_register()
561 cooling_ops = &cpufreq_cdev->cooling_ops; in __cpufreq_cooling_register()
562 cooling_ops->get_max_state = cpufreq_get_max_state; in __cpufreq_cooling_register()
563 cooling_ops->get_cur_state = cpufreq_get_cur_state; in __cpufreq_cooling_register()
564 cooling_ops->set_cur_state = cpufreq_set_cur_state; in __cpufreq_cooling_register()
568 cpufreq_cdev->em = em; in __cpufreq_cooling_register()
569 cooling_ops->get_requested_power = cpufreq_get_requested_power; in __cpufreq_cooling_register()
570 cooling_ops->state2power = cpufreq_state2power; in __cpufreq_cooling_register()
571 cooling_ops->power2state = cpufreq_power2state; in __cpufreq_cooling_register()
574 if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) { in __cpufreq_cooling_register()
577 cdev = ERR_PTR(-EINVAL); in __cpufreq_cooling_register()
581 ret = freq_qos_add_request(&policy->constraints, in __cpufreq_cooling_register()
582 &cpufreq_cdev->qos_req, FREQ_QOS_MAX, in __cpufreq_cooling_register()
591 cdev = ERR_PTR(-ENOMEM); in __cpufreq_cooling_register()
592 name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev)); in __cpufreq_cooling_register()
606 freq_qos_remove_request(&cpufreq_cdev->qos_req); in __cpufreq_cooling_register()
615 * cpufreq_cooling_register - function to create cpufreq cooling device.
618 * This interface function registers the cpufreq cooling device with the name
619 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
633 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
636 * This interface function registers the cpufreq cooling device with the name
637 * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
638 * devices. Using this API, the cpufreq cooling device will be linked to the
639 * device tree node provided.
641 * Using this function, the cooling device will implement the power
651 struct device_node *np = of_get_cpu_node(policy->cpu, NULL); in of_cpufreq_cooling_register()
656 policy->cpu); in of_cpufreq_cooling_register()
660 if (of_property_present(np, "#cooling-cells")) { in of_cpufreq_cooling_register()
661 struct em_perf_domain *em = em_cpu_get(policy->cpu); in of_cpufreq_cooling_register()
665 pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n", in of_cpufreq_cooling_register()
666 policy->cpu, PTR_ERR(cdev)); in of_cpufreq_cooling_register()
677 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
678 * @cdev: thermal cooling device pointer.
680 * This interface function unregisters the "cpufreq-%x" cooling device.
689 cpufreq_cdev = cdev->devdata; in cpufreq_cooling_unregister()
692 freq_qos_remove_request(&cpufreq_cdev->qos_req); in cpufreq_cooling_unregister()