cpufreq.c 2.7 KB

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