cpu_cooling.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. enum cpufreq_cooling_property {
  103. GET_LEVEL,
  104. GET_FREQ,
  105. GET_MAXL,
  106. };
  107. /*
  108. * this is the common function to
  109. * 1. get maximum cpu cooling states
  110. * 2. translate frequency to cooling state
  111. * 3. translate cooling state to frequency
  112. * Note that the code may be not in good shape
  113. * but it is written in this way in order to:
  114. * a) reduce duplicate code as most of the code can be shared.
  115. * b) make sure the logic is consistent when translating between
  116. * cooling states and frequencies.
  117. */
  118. static int get_property(unsigned int cpu, unsigned long input,
  119. unsigned int* output, enum cpufreq_cooling_property property)
  120. {
  121. int i, j;
  122. unsigned long max_level = 0, level;
  123. unsigned int freq = CPUFREQ_ENTRY_INVALID;
  124. int descend = -1;
  125. struct cpufreq_frequency_table *table =
  126. cpufreq_frequency_get_table(cpu);
  127. if (!output)
  128. return -EINVAL;
  129. if (!table)
  130. return -EINVAL;
  131. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  132. /* ignore invalid entries */
  133. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  134. continue;
  135. /* ignore duplicate entry */
  136. if (freq == table[i].frequency)
  137. continue;
  138. /* get the frequency order */
  139. if (freq != CPUFREQ_ENTRY_INVALID && descend != -1)
  140. descend = !!(freq > table[i].frequency);
  141. freq = table[i].frequency;
  142. max_level++;
  143. }
  144. /* get max level */
  145. if (property == GET_MAXL) {
  146. *output = (unsigned int)max_level;
  147. return 0;
  148. }
  149. if (property == GET_FREQ)
  150. level = descend ? input : (max_level - input -1);
  151. for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  152. /* ignore invalid entry */
  153. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  154. continue;
  155. /* ignore duplicate entry */
  156. if (freq == table[i].frequency)
  157. continue;
  158. /* now we have a valid frequency entry */
  159. freq = table[i].frequency;
  160. if (property == GET_LEVEL && (unsigned int)input == freq) {
  161. /* get level by frequency */
  162. *output = descend ? j : (max_level - j - 1);
  163. return 0;
  164. }
  165. if (property == GET_FREQ && level == j) {
  166. /* get frequency by level */
  167. *output = freq;
  168. return 0;
  169. }
  170. j++;
  171. }
  172. return -EINVAL;
  173. }
  174. unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
  175. {
  176. unsigned int val;
  177. if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
  178. return THERMAL_CSTATE_INVALID;
  179. return (unsigned long)val;
  180. }
  181. EXPORT_SYMBOL(cpufreq_cooling_get_level);
  182. /**
  183. * get_cpu_frequency - get the absolute value of frequency from level.
  184. * @cpu: cpu for which frequency is fetched.
  185. * @level: level of frequency, equals cooling state of cpu cooling device
  186. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  187. */
  188. static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  189. {
  190. int ret = 0;
  191. unsigned int freq;
  192. ret = get_property(cpu, level, &freq, GET_FREQ);
  193. if (ret)
  194. return 0;
  195. return freq;
  196. }
  197. /**
  198. * cpufreq_apply_cooling - function to apply frequency clipping.
  199. * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  200. * clipping data.
  201. * @cooling_state: value of the cooling state.
  202. */
  203. static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  204. unsigned long cooling_state)
  205. {
  206. unsigned int cpuid, clip_freq;
  207. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  208. unsigned int cpu = cpumask_any(mask);
  209. /* Check if the old cooling action is same as new cooling action */
  210. if (cpufreq_device->cpufreq_state == cooling_state)
  211. return 0;
  212. clip_freq = get_cpu_frequency(cpu, cooling_state);
  213. if (!clip_freq)
  214. return -EINVAL;
  215. cpufreq_device->cpufreq_state = cooling_state;
  216. cpufreq_device->cpufreq_val = clip_freq;
  217. notify_device = cpufreq_device;
  218. for_each_cpu(cpuid, mask) {
  219. if (is_cpufreq_valid(cpuid))
  220. cpufreq_update_policy(cpuid);
  221. }
  222. notify_device = NOTIFY_INVALID;
  223. return 0;
  224. }
  225. /**
  226. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  227. * @nb: struct notifier_block * with callback info.
  228. * @event: value showing cpufreq event for which this function invoked.
  229. * @data: callback-specific data
  230. */
  231. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  232. unsigned long event, void *data)
  233. {
  234. struct cpufreq_policy *policy = data;
  235. unsigned long max_freq = 0;
  236. if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
  237. return 0;
  238. if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
  239. max_freq = notify_device->cpufreq_val;
  240. /* Never exceed user_policy.max*/
  241. if (max_freq > policy->user_policy.max)
  242. max_freq = policy->user_policy.max;
  243. if (policy->max != max_freq)
  244. cpufreq_verify_within_limits(policy, 0, max_freq);
  245. return 0;
  246. }
  247. /*
  248. * cpufreq cooling device callback functions are defined below
  249. */
  250. /**
  251. * cpufreq_get_max_state - callback function to get the max cooling state.
  252. * @cdev: thermal cooling device pointer.
  253. * @state: fill this variable with the max cooling state.
  254. */
  255. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  256. unsigned long *state)
  257. {
  258. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  259. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  260. unsigned int cpu;
  261. unsigned int count = 0;
  262. int ret;
  263. cpu = cpumask_any(mask);
  264. ret = get_property(cpu, 0, &count, GET_MAXL);
  265. if (count > 0)
  266. *state = count;
  267. return ret;
  268. }
  269. /**
  270. * cpufreq_get_cur_state - callback function to get the current cooling state.
  271. * @cdev: thermal cooling device pointer.
  272. * @state: fill this variable with the current cooling state.
  273. */
  274. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  275. unsigned long *state)
  276. {
  277. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  278. *state = cpufreq_device->cpufreq_state;
  279. return 0;
  280. }
  281. /**
  282. * cpufreq_set_cur_state - callback function to set the current cooling state.
  283. * @cdev: thermal cooling device pointer.
  284. * @state: set this variable to the current cooling state.
  285. */
  286. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  287. unsigned long state)
  288. {
  289. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  290. return cpufreq_apply_cooling(cpufreq_device, state);
  291. }
  292. /* Bind cpufreq callbacks to thermal cooling device ops */
  293. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  294. .get_max_state = cpufreq_get_max_state,
  295. .get_cur_state = cpufreq_get_cur_state,
  296. .set_cur_state = cpufreq_set_cur_state,
  297. };
  298. /* Notifier for cpufreq policy change */
  299. static struct notifier_block thermal_cpufreq_notifier_block = {
  300. .notifier_call = cpufreq_thermal_notifier,
  301. };
  302. /**
  303. * cpufreq_cooling_register - function to create cpufreq cooling device.
  304. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  305. */
  306. struct thermal_cooling_device *cpufreq_cooling_register(
  307. const struct cpumask *clip_cpus)
  308. {
  309. struct thermal_cooling_device *cool_dev;
  310. struct cpufreq_cooling_device *cpufreq_dev = NULL;
  311. unsigned int min = 0, max = 0;
  312. char dev_name[THERMAL_NAME_LENGTH];
  313. int ret = 0, i;
  314. struct cpufreq_policy policy;
  315. /*Verify that all the clip cpus have same freq_min, freq_max limit*/
  316. for_each_cpu(i, clip_cpus) {
  317. /*continue if cpufreq policy not found and not return error*/
  318. if (!cpufreq_get_policy(&policy, i))
  319. continue;
  320. if (min == 0 && max == 0) {
  321. min = policy.cpuinfo.min_freq;
  322. max = policy.cpuinfo.max_freq;
  323. } else {
  324. if (min != policy.cpuinfo.min_freq ||
  325. max != policy.cpuinfo.max_freq)
  326. return ERR_PTR(-EINVAL);
  327. }
  328. }
  329. cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
  330. GFP_KERNEL);
  331. if (!cpufreq_dev)
  332. return ERR_PTR(-ENOMEM);
  333. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  334. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  335. if (ret) {
  336. kfree(cpufreq_dev);
  337. return ERR_PTR(-EINVAL);
  338. }
  339. sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
  340. cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
  341. &cpufreq_cooling_ops);
  342. if (!cool_dev) {
  343. release_idr(&cpufreq_idr, cpufreq_dev->id);
  344. kfree(cpufreq_dev);
  345. return ERR_PTR(-EINVAL);
  346. }
  347. cpufreq_dev->cool_dev = cool_dev;
  348. cpufreq_dev->cpufreq_state = 0;
  349. mutex_lock(&cooling_cpufreq_lock);
  350. /* Register the notifier for first cpufreq cooling device */
  351. if (cpufreq_dev_count == 0)
  352. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  353. CPUFREQ_POLICY_NOTIFIER);
  354. cpufreq_dev_count++;
  355. mutex_unlock(&cooling_cpufreq_lock);
  356. return cool_dev;
  357. }
  358. EXPORT_SYMBOL(cpufreq_cooling_register);
  359. /**
  360. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  361. * @cdev: thermal cooling device pointer.
  362. */
  363. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  364. {
  365. struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
  366. mutex_lock(&cooling_cpufreq_lock);
  367. cpufreq_dev_count--;
  368. /* Unregister the notifier for the last cpufreq cooling device */
  369. if (cpufreq_dev_count == 0) {
  370. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  371. CPUFREQ_POLICY_NOTIFIER);
  372. }
  373. mutex_unlock(&cooling_cpufreq_lock);
  374. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  375. release_idr(&cpufreq_idr, cpufreq_dev->id);
  376. kfree(cpufreq_dev);
  377. }
  378. EXPORT_SYMBOL(cpufreq_cooling_unregister);