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