cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic OPP helper interface for CPU device
  4. *
  5. * Copyright (C) 2009-2014 Texas Instruments Incorporated.
  6. * Nishanth Menon
  7. * Romit Dasgupta
  8. * Kevin Hilman
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/cpu.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/export.h>
  16. #include <linux/slab.h>
  17. #include "opp.h"
  18. #ifdef CONFIG_CPU_FREQ
  19. /**
  20. * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device
  21. * @dev: device for which we do this operation
  22. * @opp_table: Cpufreq table returned back to caller
  23. *
  24. * Generate a cpufreq table for a provided device- this assumes that the
  25. * opp table is already initialized and ready for usage.
  26. *
  27. * This function allocates required memory for the cpufreq table. It is
  28. * expected that the caller does the required maintenance such as freeing
  29. * the table as required.
  30. *
  31. * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
  32. * if no memory available for the operation (table is not populated), returns 0
  33. * if successful and table is populated.
  34. *
  35. * WARNING: It is important for the callers to ensure refreshing their copy of
  36. * the table if any of the mentioned functions have been invoked in the interim.
  37. */
  38. int dev_pm_opp_init_cpufreq_table(struct device *dev,
  39. struct cpufreq_frequency_table **opp_table)
  40. {
  41. struct cpufreq_frequency_table *freq_table = NULL;
  42. int i, max_opps, ret = 0;
  43. unsigned long rate;
  44. max_opps = dev_pm_opp_get_opp_count(dev);
  45. if (max_opps <= 0)
  46. return max_opps ? max_opps : -ENODATA;
  47. freq_table = kzalloc_objs(*freq_table, (max_opps + 1));
  48. if (!freq_table)
  49. return -ENOMEM;
  50. for (i = 0, rate = 0; i < max_opps; i++, rate++) {
  51. /* find next rate */
  52. struct dev_pm_opp *opp __free(put_opp) =
  53. dev_pm_opp_find_freq_ceil(dev, &rate);
  54. if (IS_ERR(opp)) {
  55. ret = PTR_ERR(opp);
  56. goto out;
  57. }
  58. freq_table[i].driver_data = i;
  59. freq_table[i].frequency = rate / 1000;
  60. /* Is Boost/turbo opp ? */
  61. if (dev_pm_opp_is_turbo(opp))
  62. freq_table[i].flags = CPUFREQ_BOOST_FREQ;
  63. }
  64. freq_table[i].driver_data = i;
  65. freq_table[i].frequency = CPUFREQ_TABLE_END;
  66. *opp_table = &freq_table[0];
  67. out:
  68. if (ret)
  69. kfree(freq_table);
  70. return ret;
  71. }
  72. EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
  73. /**
  74. * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
  75. * @dev: device for which we do this operation
  76. * @opp_table: table to free
  77. *
  78. * Free up the table allocated by dev_pm_opp_init_cpufreq_table
  79. */
  80. void dev_pm_opp_free_cpufreq_table(struct device *dev,
  81. struct cpufreq_frequency_table **opp_table)
  82. {
  83. if (!opp_table)
  84. return;
  85. kfree(*opp_table);
  86. *opp_table = NULL;
  87. }
  88. EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
  89. #endif /* CONFIG_CPU_FREQ */
  90. void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask,
  91. int last_cpu)
  92. {
  93. struct device *cpu_dev;
  94. int cpu;
  95. WARN_ON(cpumask_empty(cpumask));
  96. for_each_cpu(cpu, cpumask) {
  97. if (cpu == last_cpu)
  98. break;
  99. cpu_dev = get_cpu_device(cpu);
  100. if (!cpu_dev) {
  101. pr_err("%s: failed to get cpu%d device\n", __func__,
  102. cpu);
  103. continue;
  104. }
  105. dev_pm_opp_remove_table(cpu_dev);
  106. }
  107. }
  108. /**
  109. * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask
  110. * @cpumask: cpumask for which OPP table needs to be removed
  111. *
  112. * This removes the OPP tables for CPUs present in the @cpumask.
  113. * This should be used to remove all the OPPs entries associated with
  114. * the cpus in @cpumask.
  115. */
  116. void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
  117. {
  118. _dev_pm_opp_cpumask_remove_table(cpumask, -1);
  119. }
  120. EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
  121. /**
  122. * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
  123. * @cpu_dev: CPU device for which we do this operation
  124. * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev
  125. *
  126. * This marks OPP table of the @cpu_dev as shared by the CPUs present in
  127. * @cpumask.
  128. *
  129. * Returns -ENODEV if OPP table isn't already present.
  130. */
  131. int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
  132. const struct cpumask *cpumask)
  133. {
  134. struct opp_device *opp_dev;
  135. struct device *dev;
  136. int cpu;
  137. struct opp_table *opp_table __free(put_opp_table) =
  138. _find_opp_table(cpu_dev);
  139. if (IS_ERR(opp_table))
  140. return PTR_ERR(opp_table);
  141. for_each_cpu(cpu, cpumask) {
  142. if (cpu == cpu_dev->id)
  143. continue;
  144. dev = get_cpu_device(cpu);
  145. if (!dev) {
  146. dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
  147. __func__, cpu);
  148. continue;
  149. }
  150. opp_dev = _add_opp_dev(dev, opp_table);
  151. if (!opp_dev) {
  152. dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
  153. __func__, cpu);
  154. continue;
  155. }
  156. /* Mark opp-table as multiple CPUs are sharing it now */
  157. opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
  158. }
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
  162. /**
  163. * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev
  164. * @cpu_dev: CPU device for which we do this operation
  165. * @cpumask: cpumask to update with information of sharing CPUs
  166. *
  167. * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
  168. *
  169. * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP
  170. * table's status is access-unknown.
  171. */
  172. int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
  173. {
  174. struct opp_device *opp_dev;
  175. struct opp_table *opp_table __free(put_opp_table) =
  176. _find_opp_table(cpu_dev);
  177. if (IS_ERR(opp_table))
  178. return PTR_ERR(opp_table);
  179. if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN)
  180. return -EINVAL;
  181. cpumask_clear(cpumask);
  182. if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
  183. guard(mutex)(&opp_table->lock);
  184. list_for_each_entry(opp_dev, &opp_table->dev_list, node)
  185. cpumask_set_cpu(opp_dev->dev->id, cpumask);
  186. } else {
  187. cpumask_set_cpu(cpu_dev->id, cpumask);
  188. }
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);