cpu_cooling.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/module.h>
  24. #include <linux/thermal.h>
  25. #include <linux/cpufreq.h>
  26. #include <linux/err.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpu.h>
  29. #include <linux/cpu_cooling.h>
  30. /**
  31. * struct cpufreq_cooling_device
  32. * @id: unique integer value corresponding to each cpufreq_cooling_device
  33. * registered.
  34. * @cool_dev: thermal_cooling_device pointer to keep track of the the
  35. * egistered cooling device.
  36. * @cpufreq_state: integer value representing the current state of cpufreq
  37. * cooling devices.
  38. * @cpufreq_val: integer value representing the absolute value of the clipped
  39. * frequency.
  40. * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
  41. * @node: list_head to link all cpufreq_cooling_device together.
  42. *
  43. * This structure is required for keeping information of each
  44. * cpufreq_cooling_device registered as a list whose head is represented by
  45. * cooling_cpufreq_list. In order to prevent corruption of this list a
  46. * mutex lock cooling_cpufreq_lock is used.
  47. */
  48. struct cpufreq_cooling_device {
  49. int id;
  50. struct thermal_cooling_device *cool_dev;
  51. unsigned int cpufreq_state;
  52. unsigned int cpufreq_val;
  53. struct cpumask allowed_cpus;
  54. struct list_head node;
  55. };
  56. static LIST_HEAD(cooling_cpufreq_list);
  57. static DEFINE_IDR(cpufreq_idr);
  58. static DEFINE_MUTEX(cooling_cpufreq_lock);
  59. static unsigned int cpufreq_dev_count;
  60. /* notify_table passes value to the CPUFREQ_ADJUST callback function. */
  61. #define NOTIFY_INVALID NULL
  62. static struct cpufreq_cooling_device *notify_device;
  63. /**
  64. * get_idr - function to get a unique id.
  65. * @idr: struct idr * handle used to create a id.
  66. * @id: int * value generated by this function.
  67. */
  68. static int get_idr(struct idr *idr, int *id)
  69. {
  70. int ret;
  71. mutex_lock(&cooling_cpufreq_lock);
  72. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  73. mutex_unlock(&cooling_cpufreq_lock);
  74. if (unlikely(ret < 0))
  75. return ret;
  76. *id = ret;
  77. return 0;
  78. }
  79. /**
  80. * release_idr - function to free the unique id.
  81. * @idr: struct idr * handle used for creating the id.
  82. * @id: int value representing the unique id.
  83. */
  84. static void release_idr(struct idr *idr, int id)
  85. {
  86. mutex_lock(&cooling_cpufreq_lock);
  87. idr_remove(idr, id);
  88. mutex_unlock(&cooling_cpufreq_lock);
  89. }
  90. /* Below code defines functions to be used for cpufreq as cooling device */
  91. /**
  92. * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
  93. * @cpu: cpu for which check is needed.
  94. */
  95. static int is_cpufreq_valid(int cpu)
  96. {
  97. struct cpufreq_policy policy;
  98. return !cpufreq_get_policy(&policy, cpu);
  99. }
  100. enum cpufreq_cooling_property {
  101. GET_LEVEL,
  102. GET_FREQ,
  103. GET_MAXL,
  104. };
  105. /*
  106. * this is the common function to
  107. * 1. get maximum cpu cooling states
  108. * 2. translate frequency to cooling state
  109. * 3. translate cooling state to frequency
  110. * Note that the code may be not in good shape
  111. * but it is written in this way in order to:
  112. * a) reduce duplicate code as most of the code can be shared.
  113. * b) make sure the logic is consistent when translating between
  114. * cooling states and frequencies.
  115. */
  116. static int get_property(unsigned int cpu, unsigned long input,
  117. unsigned int* output, enum cpufreq_cooling_property property)
  118. {
  119. int i, j;
  120. unsigned long max_level = 0, level;
  121. unsigned int freq = CPUFREQ_ENTRY_INVALID;
  122. int descend = -1;
  123. struct cpufreq_frequency_table *table =
  124. cpufreq_frequency_get_table(cpu);
  125. if (!output)
  126. return -EINVAL;
  127. if (!table)
  128. return -EINVAL;
  129. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  130. /* ignore invalid entries */
  131. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  132. continue;
  133. /* ignore duplicate entry */
  134. if (freq == table[i].frequency)
  135. continue;
  136. /* get the frequency order */
  137. if (freq != CPUFREQ_ENTRY_INVALID && descend != -1)
  138. descend = !!(freq > table[i].frequency);
  139. freq = table[i].frequency;
  140. max_level++;
  141. }
  142. /* get max level */
  143. if (property == GET_MAXL) {
  144. *output = (unsigned int)max_level;
  145. return 0;
  146. }
  147. if (property == GET_FREQ)
  148. level = descend ? input : (max_level - input -1);
  149. for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  150. /* ignore invalid entry */
  151. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  152. continue;
  153. /* ignore duplicate entry */
  154. if (freq == table[i].frequency)
  155. continue;
  156. /* now we have a valid frequency entry */
  157. freq = table[i].frequency;
  158. if (property == GET_LEVEL && (unsigned int)input == freq) {
  159. /* get level by frequency */
  160. *output = descend ? j : (max_level - j - 1);
  161. return 0;
  162. }
  163. if (property == GET_FREQ && level == j) {
  164. /* get frequency by level */
  165. *output = freq;
  166. return 0;
  167. }
  168. j++;
  169. }
  170. return -EINVAL;
  171. }
  172. unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
  173. {
  174. unsigned int val;
  175. if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
  176. return THERMAL_CSTATE_INVALID;
  177. return (unsigned long)val;
  178. }
  179. EXPORT_SYMBOL(cpufreq_cooling_get_level);
  180. /**
  181. * get_cpu_frequency - get the absolute value of frequency from level.
  182. * @cpu: cpu for which frequency is fetched.
  183. * @level: level of frequency, equals cooling state of cpu cooling device
  184. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  185. */
  186. static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  187. {
  188. int ret = 0;
  189. unsigned int freq;
  190. ret = get_property(cpu, level, &freq, GET_FREQ);
  191. if (ret)
  192. return 0;
  193. return freq;
  194. }
  195. /**
  196. * cpufreq_apply_cooling - function to apply frequency clipping.
  197. * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  198. * clipping data.
  199. * @cooling_state: value of the cooling state.
  200. */
  201. static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  202. unsigned long cooling_state)
  203. {
  204. unsigned int cpuid, clip_freq;
  205. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  206. unsigned int cpu = cpumask_any(mask);
  207. /* Check if the old cooling action is same as new cooling action */
  208. if (cpufreq_device->cpufreq_state == cooling_state)
  209. return 0;
  210. clip_freq = get_cpu_frequency(cpu, cooling_state);
  211. if (!clip_freq)
  212. return -EINVAL;
  213. cpufreq_device->cpufreq_state = cooling_state;
  214. cpufreq_device->cpufreq_val = clip_freq;
  215. notify_device = cpufreq_device;
  216. for_each_cpu(cpuid, mask) {
  217. if (is_cpufreq_valid(cpuid))
  218. cpufreq_update_policy(cpuid);
  219. }
  220. notify_device = NOTIFY_INVALID;
  221. return 0;
  222. }
  223. /**
  224. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  225. * @nb: struct notifier_block * with callback info.
  226. * @event: value showing cpufreq event for which this function invoked.
  227. * @data: callback-specific data
  228. */
  229. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  230. unsigned long event, void *data)
  231. {
  232. struct cpufreq_policy *policy = data;
  233. unsigned long max_freq = 0;
  234. if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
  235. return 0;
  236. if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
  237. max_freq = notify_device->cpufreq_val;
  238. /* Never exceed user_policy.max*/
  239. if (max_freq > policy->user_policy.max)
  240. max_freq = policy->user_policy.max;
  241. if (policy->max != max_freq)
  242. cpufreq_verify_within_limits(policy, 0, max_freq);
  243. return 0;
  244. }
  245. /*
  246. * cpufreq cooling device callback functions are defined below
  247. */
  248. /**
  249. * cpufreq_get_max_state - callback function to get the max cooling state.
  250. * @cdev: thermal cooling device pointer.
  251. * @state: fill this variable with the max cooling state.
  252. */
  253. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  254. unsigned long *state)
  255. {
  256. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  257. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  258. unsigned int cpu;
  259. unsigned int count = 0;
  260. int ret;
  261. cpu = cpumask_any(mask);
  262. ret = get_property(cpu, 0, &count, GET_MAXL);
  263. if (count > 0)
  264. *state = count;
  265. return ret;
  266. }
  267. /**
  268. * cpufreq_get_cur_state - callback function to get the current cooling state.
  269. * @cdev: thermal cooling device pointer.
  270. * @state: fill this variable with the current cooling state.
  271. */
  272. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  273. unsigned long *state)
  274. {
  275. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  276. *state = cpufreq_device->cpufreq_state;
  277. return 0;
  278. }
  279. /**
  280. * cpufreq_set_cur_state - callback function to set the current cooling state.
  281. * @cdev: thermal cooling device pointer.
  282. * @state: set this variable to the current cooling state.
  283. */
  284. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  285. unsigned long state)
  286. {
  287. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  288. return cpufreq_apply_cooling(cpufreq_device, state);
  289. }
  290. /* Bind cpufreq callbacks to thermal cooling device ops */
  291. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  292. .get_max_state = cpufreq_get_max_state,
  293. .get_cur_state = cpufreq_get_cur_state,
  294. .set_cur_state = cpufreq_set_cur_state,
  295. };
  296. /* Notifier for cpufreq policy change */
  297. static struct notifier_block thermal_cpufreq_notifier_block = {
  298. .notifier_call = cpufreq_thermal_notifier,
  299. };
  300. /**
  301. * cpufreq_cooling_register - function to create cpufreq cooling device.
  302. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  303. */
  304. struct thermal_cooling_device *cpufreq_cooling_register(
  305. const struct cpumask *clip_cpus)
  306. {
  307. struct thermal_cooling_device *cool_dev;
  308. struct cpufreq_cooling_device *cpufreq_dev = NULL;
  309. unsigned int min = 0, max = 0;
  310. char dev_name[THERMAL_NAME_LENGTH];
  311. int ret = 0, i;
  312. struct cpufreq_policy policy;
  313. /*Verify that all the clip cpus have same freq_min, freq_max limit*/
  314. for_each_cpu(i, clip_cpus) {
  315. /*continue if cpufreq policy not found and not return error*/
  316. if (!cpufreq_get_policy(&policy, i))
  317. continue;
  318. if (min == 0 && max == 0) {
  319. min = policy.cpuinfo.min_freq;
  320. max = policy.cpuinfo.max_freq;
  321. } else {
  322. if (min != policy.cpuinfo.min_freq ||
  323. max != policy.cpuinfo.max_freq)
  324. return ERR_PTR(-EINVAL);
  325. }
  326. }
  327. cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
  328. GFP_KERNEL);
  329. if (!cpufreq_dev)
  330. return ERR_PTR(-ENOMEM);
  331. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  332. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  333. if (ret) {
  334. kfree(cpufreq_dev);
  335. return ERR_PTR(-EINVAL);
  336. }
  337. sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
  338. cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
  339. &cpufreq_cooling_ops);
  340. if (!cool_dev) {
  341. release_idr(&cpufreq_idr, cpufreq_dev->id);
  342. kfree(cpufreq_dev);
  343. return ERR_PTR(-EINVAL);
  344. }
  345. cpufreq_dev->cool_dev = cool_dev;
  346. cpufreq_dev->cpufreq_state = 0;
  347. mutex_lock(&cooling_cpufreq_lock);
  348. /* Register the notifier for first cpufreq cooling device */
  349. if (cpufreq_dev_count == 0)
  350. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  351. CPUFREQ_POLICY_NOTIFIER);
  352. cpufreq_dev_count++;
  353. mutex_unlock(&cooling_cpufreq_lock);
  354. return cool_dev;
  355. }
  356. EXPORT_SYMBOL(cpufreq_cooling_register);
  357. /**
  358. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  359. * @cdev: thermal cooling device pointer.
  360. */
  361. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  362. {
  363. struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
  364. mutex_lock(&cooling_cpufreq_lock);
  365. cpufreq_dev_count--;
  366. /* Unregister the notifier for the last cpufreq cooling device */
  367. if (cpufreq_dev_count == 0) {
  368. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  369. CPUFREQ_POLICY_NOTIFIER);
  370. }
  371. mutex_unlock(&cooling_cpufreq_lock);
  372. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  373. release_idr(&cpufreq_idr, cpufreq_dev->id);
  374. kfree(cpufreq_dev);
  375. }
  376. EXPORT_SYMBOL(cpufreq_cooling_unregister);