cpu_cooling.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 err;
  73. again:
  74. if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
  75. return -ENOMEM;
  76. mutex_lock(&cooling_cpufreq_lock);
  77. err = idr_get_new(idr, NULL, id);
  78. mutex_unlock(&cooling_cpufreq_lock);
  79. if (unlikely(err == -EAGAIN))
  80. goto again;
  81. else if (unlikely(err))
  82. return err;
  83. *id = *id & MAX_IDR_MASK;
  84. return 0;
  85. }
  86. /**
  87. * release_idr - function to free the unique id.
  88. * @idr: struct idr * handle used for creating the id.
  89. * @id: int value representing the unique id.
  90. */
  91. static void release_idr(struct idr *idr, int id)
  92. {
  93. mutex_lock(&cooling_cpufreq_lock);
  94. idr_remove(idr, id);
  95. mutex_unlock(&cooling_cpufreq_lock);
  96. }
  97. /* Below code defines functions to be used for cpufreq as cooling device */
  98. /**
  99. * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
  100. * @cpu: cpu for which check is needed.
  101. */
  102. static int is_cpufreq_valid(int cpu)
  103. {
  104. struct cpufreq_policy policy;
  105. return !cpufreq_get_policy(&policy, cpu);
  106. }
  107. /**
  108. * get_cpu_frequency - get the absolute value of frequency from level.
  109. * @cpu: cpu for which frequency is fetched.
  110. * @level: level of frequency of the CPU
  111. * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
  112. */
  113. static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  114. {
  115. int ret = 0, i = 0;
  116. unsigned long level_index;
  117. bool descend = false;
  118. struct cpufreq_frequency_table *table =
  119. cpufreq_frequency_get_table(cpu);
  120. if (!table)
  121. return ret;
  122. while (table[i].frequency != CPUFREQ_TABLE_END) {
  123. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  124. continue;
  125. /*check if table in ascending or descending order*/
  126. if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
  127. (table[i + 1].frequency < table[i].frequency)
  128. && !descend) {
  129. descend = true;
  130. }
  131. /*return if level matched and table in descending order*/
  132. if (descend && i == level)
  133. return table[i].frequency;
  134. i++;
  135. }
  136. i--;
  137. if (level > i || descend)
  138. return ret;
  139. level_index = i - level;
  140. /*Scan the table in reverse order and match the level*/
  141. while (i >= 0) {
  142. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  143. continue;
  144. /*return if level matched*/
  145. if (i == level_index)
  146. return table[i].frequency;
  147. i--;
  148. }
  149. return ret;
  150. }
  151. /**
  152. * cpufreq_apply_cooling - function to apply frequency clipping.
  153. * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  154. * clipping data.
  155. * @cooling_state: value of the cooling state.
  156. */
  157. static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  158. unsigned long cooling_state)
  159. {
  160. unsigned int cpuid, clip_freq;
  161. struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
  162. unsigned int cpu = cpumask_any(maskPtr);
  163. /* Check if the old cooling action is same as new cooling action */
  164. if (cpufreq_device->cpufreq_state == cooling_state)
  165. return 0;
  166. clip_freq = get_cpu_frequency(cpu, cooling_state);
  167. if (!clip_freq)
  168. return -EINVAL;
  169. cpufreq_device->cpufreq_state = cooling_state;
  170. cpufreq_device->cpufreq_val = clip_freq;
  171. notify_device = cpufreq_device;
  172. for_each_cpu(cpuid, maskPtr) {
  173. if (is_cpufreq_valid(cpuid))
  174. cpufreq_update_policy(cpuid);
  175. }
  176. notify_device = NOTIFY_INVALID;
  177. return 0;
  178. }
  179. /**
  180. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  181. * @nb: struct notifier_block * with callback info.
  182. * @event: value showing cpufreq event for which this function invoked.
  183. * @data: callback-specific data
  184. */
  185. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  186. unsigned long event, void *data)
  187. {
  188. struct cpufreq_policy *policy = data;
  189. unsigned long max_freq = 0;
  190. if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
  191. return 0;
  192. if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
  193. max_freq = notify_device->cpufreq_val;
  194. /* Never exceed user_policy.max*/
  195. if (max_freq > policy->user_policy.max)
  196. max_freq = policy->user_policy.max;
  197. if (policy->max != max_freq)
  198. cpufreq_verify_within_limits(policy, 0, max_freq);
  199. return 0;
  200. }
  201. /*
  202. * cpufreq cooling device callback functions are defined below
  203. */
  204. /**
  205. * cpufreq_get_max_state - callback function to get the max cooling state.
  206. * @cdev: thermal cooling device pointer.
  207. * @state: fill this variable with the max cooling state.
  208. */
  209. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  210. unsigned long *state)
  211. {
  212. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  213. struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
  214. unsigned int cpu;
  215. struct cpufreq_frequency_table *table;
  216. unsigned long count = 0;
  217. int i = 0;
  218. cpu = cpumask_any(maskPtr);
  219. table = cpufreq_frequency_get_table(cpu);
  220. if (!table) {
  221. *state = 0;
  222. return 0;
  223. }
  224. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  225. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  226. continue;
  227. count++;
  228. }
  229. if (count > 0) {
  230. *state = --count;
  231. return 0;
  232. }
  233. return -EINVAL;
  234. }
  235. /**
  236. * cpufreq_get_cur_state - callback function to get the current cooling state.
  237. * @cdev: thermal cooling device pointer.
  238. * @state: fill this variable with the current cooling state.
  239. */
  240. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  241. unsigned long *state)
  242. {
  243. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  244. *state = cpufreq_device->cpufreq_state;
  245. return 0;
  246. }
  247. /**
  248. * cpufreq_set_cur_state - callback function to set the current cooling state.
  249. * @cdev: thermal cooling device pointer.
  250. * @state: set this variable to the current cooling state.
  251. */
  252. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  253. unsigned long state)
  254. {
  255. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  256. return cpufreq_apply_cooling(cpufreq_device, state);
  257. }
  258. /* Bind cpufreq callbacks to thermal cooling device ops */
  259. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  260. .get_max_state = cpufreq_get_max_state,
  261. .get_cur_state = cpufreq_get_cur_state,
  262. .set_cur_state = cpufreq_set_cur_state,
  263. };
  264. /* Notifier for cpufreq policy change */
  265. static struct notifier_block thermal_cpufreq_notifier_block = {
  266. .notifier_call = cpufreq_thermal_notifier,
  267. };
  268. /**
  269. * cpufreq_cooling_register - function to create cpufreq cooling device.
  270. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  271. */
  272. struct thermal_cooling_device *cpufreq_cooling_register(
  273. const struct cpumask *clip_cpus)
  274. {
  275. struct thermal_cooling_device *cool_dev;
  276. struct cpufreq_cooling_device *cpufreq_dev = NULL;
  277. unsigned int min = 0, max = 0;
  278. char dev_name[THERMAL_NAME_LENGTH];
  279. int ret = 0, i;
  280. struct cpufreq_policy policy;
  281. /*Verify that all the clip cpus have same freq_min, freq_max limit*/
  282. for_each_cpu(i, clip_cpus) {
  283. /*continue if cpufreq policy not found and not return error*/
  284. if (!cpufreq_get_policy(&policy, i))
  285. continue;
  286. if (min == 0 && max == 0) {
  287. min = policy.cpuinfo.min_freq;
  288. max = policy.cpuinfo.max_freq;
  289. } else {
  290. if (min != policy.cpuinfo.min_freq ||
  291. max != policy.cpuinfo.max_freq)
  292. return ERR_PTR(-EINVAL);
  293. }
  294. }
  295. cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
  296. GFP_KERNEL);
  297. if (!cpufreq_dev)
  298. return ERR_PTR(-ENOMEM);
  299. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  300. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  301. if (ret) {
  302. kfree(cpufreq_dev);
  303. return ERR_PTR(-EINVAL);
  304. }
  305. sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
  306. cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
  307. &cpufreq_cooling_ops);
  308. if (!cool_dev) {
  309. release_idr(&cpufreq_idr, cpufreq_dev->id);
  310. kfree(cpufreq_dev);
  311. return ERR_PTR(-EINVAL);
  312. }
  313. cpufreq_dev->cool_dev = cool_dev;
  314. cpufreq_dev->cpufreq_state = 0;
  315. mutex_lock(&cooling_cpufreq_lock);
  316. /* Register the notifier for first cpufreq cooling device */
  317. if (cpufreq_dev_count == 0)
  318. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  319. CPUFREQ_POLICY_NOTIFIER);
  320. cpufreq_dev_count++;
  321. mutex_unlock(&cooling_cpufreq_lock);
  322. return cool_dev;
  323. }
  324. EXPORT_SYMBOL(cpufreq_cooling_register);
  325. /**
  326. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  327. * @cdev: thermal cooling device pointer.
  328. */
  329. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  330. {
  331. struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
  332. mutex_lock(&cooling_cpufreq_lock);
  333. cpufreq_dev_count--;
  334. /* Unregister the notifier for the last cpufreq cooling device */
  335. if (cpufreq_dev_count == 0) {
  336. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  337. CPUFREQ_POLICY_NOTIFIER);
  338. }
  339. mutex_unlock(&cooling_cpufreq_lock);
  340. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  341. release_idr(&cpufreq_idr, cpufreq_dev->id);
  342. kfree(cpufreq_dev);
  343. }
  344. EXPORT_SYMBOL(cpufreq_cooling_unregister);