^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Scheduler code and data structures related to cpufreq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "sched.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) EXPORT_PER_CPU_SYMBOL_GPL(cpufreq_update_util_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @cpu: The CPU to set the pointer for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @data: New pointer value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @func: Callback function to set for the CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Set and publish the update_util_data pointer for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * The update_util_data pointer of @cpu is set to @data and the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * function pointer in the target struct update_util_data is set to @func.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * That function will be called by cpufreq_update_util() from RCU-sched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * read-side critical sections, so it must not sleep. @data will always be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * passed to it as the first argument which allows the function to get to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * target update_util_data structure and its container.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * The update_util_data pointer of @cpu must be NULL when this function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * called or it will WARN() and return with no effect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void (*func)(struct update_util_data *data, u64 time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (WARN_ON(!data || !func))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) data->func = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @cpu: The CPU to clear the pointer for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Clear the update_util_data pointer for the given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Callers must use RCU callbacks to free any memory that might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * accessed via the old update_util_data pointer or invoke synchronize_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * right after this function to avoid use-after-free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void cpufreq_remove_update_util_hook(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * cpufreq_this_cpu_can_update - Check if cpufreq policy can be updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @policy: cpufreq policy to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Return 'true' if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * - the local and remote CPUs share @policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * - dvfs_possible_from_any_cpu is set in @policy and the local CPU is not going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * offline (in which case it is not expected to run cpufreq updates any more).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return cpumask_test_cpu(smp_processor_id(), policy->cpus) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) (policy->dvfs_possible_from_any_cpu &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) EXPORT_SYMBOL_GPL(cpufreq_this_cpu_can_update);