cpufreq.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
  9. /**
  10. * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
  11. * @cpu: The CPU to set the pointer for.
  12. * @data: New pointer value.
  13. * @func: Callback function to set for the CPU.
  14. *
  15. * Set and publish the update_util_data pointer for the given CPU.
  16. *
  17. * The update_util_data pointer of @cpu is set to @data and the callback
  18. * function pointer in the target struct update_util_data is set to @func.
  19. * That function will be called by cpufreq_update_util() from RCU-sched
  20. * read-side critical sections, so it must not sleep. @data will always be
  21. * passed to it as the first argument which allows the function to get to the
  22. * target update_util_data structure and its container.
  23. *
  24. * The update_util_data pointer of @cpu must be NULL when this function is
  25. * called or it will WARN() and return with no effect.
  26. */
  27. void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
  28. void (*func)(struct update_util_data *data, u64 time,
  29. unsigned int flags))
  30. {
  31. if (WARN_ON(!data || !func))
  32. return;
  33. if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
  34. return;
  35. data->func = func;
  36. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
  37. }
  38. EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
  39. /**
  40. * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer.
  41. * @cpu: The CPU to clear the pointer for.
  42. *
  43. * Clear the update_util_data pointer for the given CPU.
  44. *
  45. * Callers must use RCU callbacks to free any memory that might be
  46. * accessed via the old update_util_data pointer or invoke synchronize_rcu()
  47. * right after this function to avoid use-after-free.
  48. */
  49. void cpufreq_remove_update_util_hook(int cpu)
  50. {
  51. rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
  52. }
  53. EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);
  54. /**
  55. * cpufreq_this_cpu_can_update - Check if cpufreq policy can be updated.
  56. * @policy: cpufreq policy to check.
  57. *
  58. * Return 'true' if:
  59. * - the local and remote CPUs share @policy,
  60. * - dvfs_possible_from_any_cpu is set in @policy and the local CPU is not going
  61. * offline (in which case it is not expected to run cpufreq updates any more).
  62. */
  63. bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy)
  64. {
  65. return cpumask_test_cpu(smp_processor_id(), policy->cpus) ||
  66. (policy->dvfs_possible_from_any_cpu &&
  67. rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data)));
  68. }