Daniel Lezcano | 108c35a | 2018-12-03 11:29:29 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 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> |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 7 | */ |
Rafael J. Wysocki | 85572c2 | 2019-12-11 11:28:41 +0100 | [diff] [blame] | 8 | #include <linux/cpufreq.h> |
| 9 | |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 10 | #include "sched.h" |
| 11 | |
Joel Fernandes (Google) | b10abd0 | 2019-03-20 20:34:23 -0400 | [diff] [blame] | 12 | DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 13 | |
| 14 | /** |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 15 | * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 16 | * @cpu: The CPU to set the pointer for. |
| 17 | * @data: New pointer value. |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 18 | * @func: Callback function to set for the CPU. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 19 | * |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 20 | * Set and publish the update_util_data pointer for the given CPU. |
| 21 | * |
| 22 | * The update_util_data pointer of @cpu is set to @data and the callback |
| 23 | * function pointer in the target struct update_util_data is set to @func. |
| 24 | * That function will be called by cpufreq_update_util() from RCU-sched |
| 25 | * read-side critical sections, so it must not sleep. @data will always be |
| 26 | * passed to it as the first argument which allows the function to get to the |
| 27 | * target update_util_data structure and its container. |
| 28 | * |
| 29 | * The update_util_data pointer of @cpu must be NULL when this function is |
| 30 | * called or it will WARN() and return with no effect. |
| 31 | */ |
| 32 | void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data, |
| 33 | void (*func)(struct update_util_data *data, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 34 | unsigned int flags)) |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 35 | { |
| 36 | if (WARN_ON(!data || !func)) |
| 37 | return; |
| 38 | |
| 39 | if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu))) |
| 40 | return; |
| 41 | |
| 42 | data->func = func; |
| 43 | rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data); |
| 44 | } |
| 45 | EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook); |
| 46 | |
| 47 | /** |
| 48 | * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer. |
| 49 | * @cpu: The CPU to clear the pointer for. |
| 50 | * |
| 51 | * Clear the update_util_data pointer for the given CPU. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 52 | * |
Paul E. McKenney | b290ebc | 2018-11-06 19:13:54 -0800 | [diff] [blame] | 53 | * Callers must use RCU callbacks to free any memory that might be |
| 54 | * accessed via the old update_util_data pointer or invoke synchronize_rcu() |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 55 | * right after this function to avoid use-after-free. |
| 56 | */ |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 57 | void cpufreq_remove_update_util_hook(int cpu) |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 58 | { |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 59 | rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL); |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 60 | } |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook); |
Rafael J. Wysocki | 85572c2 | 2019-12-11 11:28:41 +0100 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * cpufreq_this_cpu_can_update - Check if cpufreq policy can be updated. |
| 65 | * @policy: cpufreq policy to check. |
| 66 | * |
| 67 | * Return 'true' if: |
| 68 | * - the local and remote CPUs share @policy, |
| 69 | * - dvfs_possible_from_any_cpu is set in @policy and the local CPU is not going |
| 70 | * offline (in which case it is not expected to run cpufreq updates any more). |
| 71 | */ |
| 72 | bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy) |
| 73 | { |
| 74 | return cpumask_test_cpu(smp_processor_id(), policy->cpus) || |
| 75 | (policy->dvfs_possible_from_any_cpu && |
| 76 | rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data))); |
| 77 | } |