cpu.c 6.0 KB

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