cpu_cooling.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * linux/drivers/thermal/cpu_cooling.c
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
  5. * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/thermal.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/cpu.h>
  31. #include <linux/cpu_cooling.h>
  32. /**
  33. * struct cpufreq_cooling_device
  34. * @id: unique integer value corresponding to each cpufreq_cooling_device
  35. * registered.
  36. * @cool_dev: thermal_cooling_device pointer to keep track of the the
  37. * egistered cooling device.
  38. * @cpufreq_state: integer value representing the current state of cpufreq
  39. * cooling devices.
  40. * @cpufreq_val: integer value representing the absolute value of the clipped
  41. * frequency.
  42. * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
  43. * @node: list_head to link all cpufreq_cooling_device together.
  44. *
  45. * This structure is required for keeping information of each
  46. * cpufreq_cooling_device registered as a list whose head is represented by
  47. * cooling_cpufreq_list. In order to prevent corruption of this list a
  48. * mutex lock cooling_cpufreq_lock is used.
  49. */
  50. struct cpufreq_cooling_device {
  51. int id;
  52. struct thermal_cooling_device *cool_dev;
  53. unsigned int cpufreq_state;
  54. unsigned int cpufreq_val;
  55. struct cpumask allowed_cpus;
  56. struct list_head node;
  57. };
  58. static LIST_HEAD(cooling_cpufreq_list);
  59. static DEFINE_IDR(cpufreq_idr);
  60. static DEFINE_MUTEX(cooling_cpufreq_lock);
  61. static unsigned int cpufreq_dev_count;
  62. /* notify_table passes value to the CPUFREQ_ADJUST callback function. */
  63. #define NOTIFY_INVALID NULL
  64. static struct cpufreq_cooling_device *notify_device;
  65. /**
  66. * get_idr - function to get a unique id.
  67. * @idr: struct idr * handle used to create a id.
  68. * @id: int * value generated by this function.
  69. */
  70. static int get_idr(struct idr *idr, int *id)
  71. {
  72. int ret;
  73. mutex_lock(&cooling_cpufreq_lock);
  74. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  75. mutex_unlock(&cooling_cpufreq_lock);
  76. if (unlikely(ret < 0))
  77. return ret;
  78. *id = ret;
  79. return 0;
  80. }
  81. /**
  82. * release_idr - function to free the unique id.
  83. * @idr: struct idr * handle used for creating the id.
  84. * @id: int value representing the unique id.
  85. */
  86. static void release_idr(struct idr *idr, int id)
  87. {
  88. mutex_lock(&cooling_cpufreq_lock);
  89. idr_remove(idr, id);
  90. mutex_unlock(&cooling_cpufreq_lock);
  91. }
  92. /* Below code defines functions to be used for cpufreq as cooling device */
  93. /**
  94. * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
  95. * @cpu: cpu for which check is needed.
  96. */
  97. static int is_cpufreq_valid(int cpu)
  98. {
  99. struct cpufreq_policy policy;
  100. return !cpufreq_get_policy(&policy, cpu);
  101. }
  102. /**
  103. * get_cpu_frequency - get the absolute value of frequency from level.
  104. * @cpu: cpu for which frequency is fetched.
  105. * @level: level of frequency, equals cooling state of cpu cooling device
  106. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  107. */
  108. static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  109. {
  110. int ret = 0, i = 0;
  111. unsigned long level_index;
  112. bool descend = false;
  113. struct cpufreq_frequency_table *table =
  114. cpufreq_frequency_get_table(cpu);
  115. if (!table)
  116. return ret;
  117. while (table[i].frequency != CPUFREQ_TABLE_END) {
  118. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  119. continue;
  120. /*check if table in ascending or descending order*/
  121. if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
  122. (table[i + 1].frequency < table[i].frequency)
  123. && !descend) {
  124. descend = true;
  125. }
  126. /*return if level matched and table in descending order*/
  127. if (descend && i == level)
  128. return table[i].frequency;
  129. i++;
  130. }
  131. i--;
  132. if (level > i || descend)
  133. return ret;
  134. level_index = i - level;
  135. /*Scan the table in reverse order and match the level*/
  136. while (i >= 0) {
  137. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  138. continue;
  139. /*return if level matched*/
  140. if (i == level_index)
  141. return table[i].frequency;
  142. i--;
  143. }
  144. return ret;
  145. }
  146. /**
  147. * cpufreq_apply_cooling - function to apply frequency clipping.
  148. * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  149. * clipping data.
  150. * @cooling_state: value of the cooling state.
  151. */
  152. static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  153. unsigned long cooling_state)
  154. {
  155. unsigned int cpuid, clip_freq;
  156. struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
  157. unsigned int cpu = cpumask_any(maskPtr);
  158. /* Check if the old cooling action is same as new cooling action */
  159. if (cpufreq_device->cpufreq_state == cooling_state)
  160. return 0;
  161. clip_freq = get_cpu_frequency(cpu, cooling_state);
  162. if (!clip_freq)
  163. return -EINVAL;
  164. cpufreq_device->cpufreq_state = cooling_state;
  165. cpufreq_device->cpufreq_val = clip_freq;
  166. notify_device = cpufreq_device;
  167. for_each_cpu(cpuid, maskPtr) {
  168. if (is_cpufreq_valid(cpuid))
  169. cpufreq_update_policy(cpuid);
  170. }
  171. notify_device = NOTIFY_INVALID;
  172. return 0;
  173. }
  174. /**
  175. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  176. * @nb: struct notifier_block * with callback info.
  177. * @event: value showing cpufreq event for which this function invoked.
  178. * @data: callback-specific data
  179. */
  180. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  181. unsigned long event, void *data)
  182. {
  183. struct cpufreq_policy *policy = data;
  184. unsigned long max_freq = 0;
  185. if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
  186. return 0;
  187. if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
  188. max_freq = notify_device->cpufreq_val;
  189. /* Never exceed user_policy.max*/
  190. if (max_freq > policy->user_policy.max)
  191. max_freq = policy->user_policy.max;
  192. if (policy->max != max_freq)
  193. cpufreq_verify_within_limits(policy, 0, max_freq);
  194. return 0;
  195. }
  196. /*
  197. * cpufreq cooling device callback functions are defined below
  198. */
  199. /**
  200. * cpufreq_get_max_state - callback function to get the max cooling state.
  201. * @cdev: thermal cooling device pointer.
  202. * @state: fill this variable with the max cooling state.
  203. */
  204. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  205. unsigned long *state)
  206. {
  207. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  208. struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
  209. unsigned int cpu;
  210. struct cpufreq_frequency_table *table;
  211. unsigned long count = 0;
  212. int i = 0;
  213. cpu = cpumask_any(maskPtr);
  214. table = cpufreq_frequency_get_table(cpu);
  215. if (!table) {
  216. *state = 0;
  217. return 0;
  218. }
  219. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  220. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  221. continue;
  222. count++;
  223. }
  224. if (count > 0) {
  225. *state = --count;
  226. return 0;
  227. }
  228. return -EINVAL;
  229. }
  230. /**
  231. * cpufreq_get_cur_state - callback function to get the current cooling state.
  232. * @cdev: thermal cooling device pointer.
  233. * @state: fill this variable with the current cooling state.
  234. */
  235. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  236. unsigned long *state)
  237. {
  238. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  239. *state = cpufreq_device->cpufreq_state;
  240. return 0;
  241. }
  242. /**
  243. * cpufreq_set_cur_state - callback function to set the current cooling state.
  244. * @cdev: thermal cooling device pointer.
  245. * @state: set this variable to the current cooling state.
  246. */
  247. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  248. unsigned long state)
  249. {
  250. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  251. return cpufreq_apply_cooling(cpufreq_device, state);
  252. }
  253. /* Bind cpufreq callbacks to thermal cooling device ops */
  254. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  255. .get_max_state = cpufreq_get_max_state,
  256. .get_cur_state = cpufreq_get_cur_state,
  257. .set_cur_state = cpufreq_set_cur_state,
  258. };
  259. /* Notifier for cpufreq policy change */
  260. static struct notifier_block thermal_cpufreq_notifier_block = {
  261. .notifier_call = cpufreq_thermal_notifier,
  262. };
  263. /**
  264. * cpufreq_cooling_register - function to create cpufreq cooling device.
  265. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  266. */
  267. struct thermal_cooling_device *cpufreq_cooling_register(
  268. const struct cpumask *clip_cpus)
  269. {
  270. struct thermal_cooling_device *cool_dev;
  271. struct cpufreq_cooling_device *cpufreq_dev = NULL;
  272. unsigned int min = 0, max = 0;
  273. char dev_name[THERMAL_NAME_LENGTH];
  274. int ret = 0, i;
  275. struct cpufreq_policy policy;
  276. /*Verify that all the clip cpus have same freq_min, freq_max limit*/
  277. for_each_cpu(i, clip_cpus) {
  278. /*continue if cpufreq policy not found and not return error*/
  279. if (!cpufreq_get_policy(&policy, i))
  280. continue;
  281. if (min == 0 && max == 0) {
  282. min = policy.cpuinfo.min_freq;
  283. max = policy.cpuinfo.max_freq;
  284. } else {
  285. if (min != policy.cpuinfo.min_freq ||
  286. max != policy.cpuinfo.max_freq)
  287. return ERR_PTR(-EINVAL);
  288. }
  289. }
  290. cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
  291. GFP_KERNEL);
  292. if (!cpufreq_dev)
  293. return ERR_PTR(-ENOMEM);
  294. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  295. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  296. if (ret) {
  297. kfree(cpufreq_dev);
  298. return ERR_PTR(-EINVAL);
  299. }
  300. sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
  301. cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
  302. &cpufreq_cooling_ops);
  303. if (!cool_dev) {
  304. release_idr(&cpufreq_idr, cpufreq_dev->id);
  305. kfree(cpufreq_dev);
  306. return ERR_PTR(-EINVAL);
  307. }
  308. cpufreq_dev->cool_dev = cool_dev;
  309. cpufreq_dev->cpufreq_state = 0;
  310. mutex_lock(&cooling_cpufreq_lock);
  311. /* Register the notifier for first cpufreq cooling device */
  312. if (cpufreq_dev_count == 0)
  313. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  314. CPUFREQ_POLICY_NOTIFIER);
  315. cpufreq_dev_count++;
  316. mutex_unlock(&cooling_cpufreq_lock);
  317. return cool_dev;
  318. }
  319. EXPORT_SYMBOL(cpufreq_cooling_register);
  320. /**
  321. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  322. * @cdev: thermal cooling device pointer.
  323. */
  324. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  325. {
  326. struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
  327. mutex_lock(&cooling_cpufreq_lock);
  328. cpufreq_dev_count--;
  329. /* Unregister the notifier for the last cpufreq cooling device */
  330. if (cpufreq_dev_count == 0) {
  331. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  332. CPUFREQ_POLICY_NOTIFIER);
  333. }
  334. mutex_unlock(&cooling_cpufreq_lock);
  335. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  336. release_idr(&cpufreq_idr, cpufreq_dev->id);
  337. kfree(cpufreq_dev);
  338. }
  339. EXPORT_SYMBOL(cpufreq_cooling_unregister);