cpu_cooling.c 13 KB

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