cpu_cooling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/module.h>
  26. #include <linux/thermal.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. * Cooling state <-> CPUFreq frequency
  34. *
  35. * Cooling states are translated to frequencies throughout this driver and this
  36. * is the relation between them.
  37. *
  38. * Highest cooling state corresponds to lowest possible frequency.
  39. *
  40. * i.e.
  41. * level 0 --> 1st Max Freq
  42. * level 1 --> 2nd Max Freq
  43. * ...
  44. */
  45. /**
  46. * struct cpufreq_cooling_device - data for cooling device with cpufreq
  47. * @id: unique integer value corresponding to each cpufreq_cooling_device
  48. * registered.
  49. * @cool_dev: thermal_cooling_device pointer to keep track of the
  50. * registered cooling device.
  51. * @cpufreq_state: integer value representing the current state of cpufreq
  52. * cooling devices.
  53. * @cpufreq_val: integer value representing the absolute value of the clipped
  54. * frequency.
  55. * @max_level: maximum cooling level. One less than total number of valid
  56. * cpufreq frequencies.
  57. * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
  58. * @node: list_head to link all cpufreq_cooling_device together.
  59. *
  60. * This structure is required for keeping information of each registered
  61. * cpufreq_cooling_device.
  62. */
  63. struct cpufreq_cooling_device {
  64. int id;
  65. struct thermal_cooling_device *cool_dev;
  66. unsigned int cpufreq_state;
  67. unsigned int cpufreq_val;
  68. unsigned int max_level;
  69. unsigned int *freq_table; /* In descending order */
  70. struct cpumask allowed_cpus;
  71. struct list_head node;
  72. };
  73. static DEFINE_IDR(cpufreq_idr);
  74. static DEFINE_MUTEX(cooling_cpufreq_lock);
  75. static LIST_HEAD(cpufreq_dev_list);
  76. /**
  77. * get_idr - function to get a unique id.
  78. * @idr: struct idr * handle used to create a id.
  79. * @id: int * value generated by this function.
  80. *
  81. * This function will populate @id with an unique
  82. * id, using the idr API.
  83. *
  84. * Return: 0 on success, an error code on failure.
  85. */
  86. static int get_idr(struct idr *idr, int *id)
  87. {
  88. int ret;
  89. mutex_lock(&cooling_cpufreq_lock);
  90. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  91. mutex_unlock(&cooling_cpufreq_lock);
  92. if (unlikely(ret < 0))
  93. return ret;
  94. *id = ret;
  95. return 0;
  96. }
  97. /**
  98. * release_idr - function to free the unique id.
  99. * @idr: struct idr * handle used for creating the id.
  100. * @id: int value representing the unique id.
  101. */
  102. static void release_idr(struct idr *idr, int id)
  103. {
  104. mutex_lock(&cooling_cpufreq_lock);
  105. idr_remove(idr, id);
  106. mutex_unlock(&cooling_cpufreq_lock);
  107. }
  108. /* Below code defines functions to be used for cpufreq as cooling device */
  109. /**
  110. * get_level: Find the level for a particular frequency
  111. * @cpufreq_dev: cpufreq_dev for which the property is required
  112. * @freq: Frequency
  113. *
  114. * Return: level on success, THERMAL_CSTATE_INVALID on error.
  115. */
  116. static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
  117. unsigned int freq)
  118. {
  119. unsigned long level;
  120. for (level = 0; level <= cpufreq_dev->max_level; level++) {
  121. if (freq == cpufreq_dev->freq_table[level])
  122. return level;
  123. if (freq > cpufreq_dev->freq_table[level])
  124. break;
  125. }
  126. return THERMAL_CSTATE_INVALID;
  127. }
  128. /**
  129. * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
  130. * @cpu: cpu for which the level is required
  131. * @freq: the frequency of interest
  132. *
  133. * This function will match the cooling level corresponding to the
  134. * requested @freq and return it.
  135. *
  136. * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
  137. * otherwise.
  138. */
  139. unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
  140. {
  141. struct cpufreq_cooling_device *cpufreq_dev;
  142. mutex_lock(&cooling_cpufreq_lock);
  143. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  144. if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
  145. mutex_unlock(&cooling_cpufreq_lock);
  146. return get_level(cpufreq_dev, freq);
  147. }
  148. }
  149. mutex_unlock(&cooling_cpufreq_lock);
  150. pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
  151. return THERMAL_CSTATE_INVALID;
  152. }
  153. EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
  154. /**
  155. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  156. * @nb: struct notifier_block * with callback info.
  157. * @event: value showing cpufreq event for which this function invoked.
  158. * @data: callback-specific data
  159. *
  160. * Callback to hijack the notification on cpufreq policy transition.
  161. * Every time there is a change in policy, we will intercept and
  162. * update the cpufreq policy with thermal constraints.
  163. *
  164. * Return: 0 (success)
  165. */
  166. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  167. unsigned long event, void *data)
  168. {
  169. struct cpufreq_policy *policy = data;
  170. unsigned long max_freq = 0;
  171. struct cpufreq_cooling_device *cpufreq_dev;
  172. if (event != CPUFREQ_ADJUST)
  173. return 0;
  174. mutex_lock(&cooling_cpufreq_lock);
  175. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  176. if (!cpumask_test_cpu(policy->cpu,
  177. &cpufreq_dev->allowed_cpus))
  178. continue;
  179. max_freq = cpufreq_dev->cpufreq_val;
  180. if (policy->max != max_freq)
  181. cpufreq_verify_within_limits(policy, 0, max_freq);
  182. }
  183. mutex_unlock(&cooling_cpufreq_lock);
  184. return 0;
  185. }
  186. /* cpufreq cooling device callback functions are defined below */
  187. /**
  188. * cpufreq_get_max_state - callback function to get the max cooling state.
  189. * @cdev: thermal cooling device pointer.
  190. * @state: fill this variable with the max cooling state.
  191. *
  192. * Callback for the thermal cooling device to return the cpufreq
  193. * max cooling state.
  194. *
  195. * Return: 0 on success, an error code otherwise.
  196. */
  197. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  198. unsigned long *state)
  199. {
  200. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  201. *state = cpufreq_device->max_level;
  202. return 0;
  203. }
  204. /**
  205. * cpufreq_get_cur_state - callback function to get the current cooling state.
  206. * @cdev: thermal cooling device pointer.
  207. * @state: fill this variable with the current cooling state.
  208. *
  209. * Callback for the thermal cooling device to return the cpufreq
  210. * current cooling state.
  211. *
  212. * Return: 0 on success, an error code otherwise.
  213. */
  214. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  215. unsigned long *state)
  216. {
  217. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  218. *state = cpufreq_device->cpufreq_state;
  219. return 0;
  220. }
  221. /**
  222. * cpufreq_set_cur_state - callback function to set the current cooling state.
  223. * @cdev: thermal cooling device pointer.
  224. * @state: set this variable to the current cooling state.
  225. *
  226. * Callback for the thermal cooling device to change the cpufreq
  227. * current cooling state.
  228. *
  229. * Return: 0 on success, an error code otherwise.
  230. */
  231. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  232. unsigned long state)
  233. {
  234. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  235. unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
  236. unsigned int clip_freq;
  237. /* Request state should be less than max_level */
  238. if (WARN_ON(state > cpufreq_device->max_level))
  239. return -EINVAL;
  240. /* Check if the old cooling action is same as new cooling action */
  241. if (cpufreq_device->cpufreq_state == state)
  242. return 0;
  243. clip_freq = cpufreq_device->freq_table[state];
  244. cpufreq_device->cpufreq_state = state;
  245. cpufreq_device->cpufreq_val = clip_freq;
  246. cpufreq_update_policy(cpu);
  247. return 0;
  248. }
  249. /* Bind cpufreq callbacks to thermal cooling device ops */
  250. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  251. .get_max_state = cpufreq_get_max_state,
  252. .get_cur_state = cpufreq_get_cur_state,
  253. .set_cur_state = cpufreq_set_cur_state,
  254. };
  255. /* Notifier for cpufreq policy change */
  256. static struct notifier_block thermal_cpufreq_notifier_block = {
  257. .notifier_call = cpufreq_thermal_notifier,
  258. };
  259. static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  260. unsigned int prev_max)
  261. {
  262. struct cpufreq_frequency_table *pos;
  263. unsigned int max = 0;
  264. cpufreq_for_each_valid_entry(pos, table) {
  265. if (pos->frequency > max && pos->frequency < prev_max)
  266. max = pos->frequency;
  267. }
  268. return max;
  269. }
  270. /**
  271. * __cpufreq_cooling_register - helper function to create cpufreq cooling device
  272. * @np: a valid struct device_node to the cooling device device tree node
  273. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  274. * Normally this should be same as cpufreq policy->related_cpus.
  275. *
  276. * This interface function registers the cpufreq cooling device with the name
  277. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  278. * cooling devices. It also gives the opportunity to link the cooling device
  279. * with a device tree node, in order to bind it via the thermal DT code.
  280. *
  281. * Return: a valid struct thermal_cooling_device pointer on success,
  282. * on failure, it returns a corresponding ERR_PTR().
  283. */
  284. static struct thermal_cooling_device *
  285. __cpufreq_cooling_register(struct device_node *np,
  286. const struct cpumask *clip_cpus)
  287. {
  288. struct thermal_cooling_device *cool_dev;
  289. struct cpufreq_cooling_device *cpufreq_dev;
  290. char dev_name[THERMAL_NAME_LENGTH];
  291. struct cpufreq_frequency_table *pos, *table;
  292. unsigned int freq, i;
  293. int ret;
  294. table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
  295. if (!table) {
  296. pr_debug("%s: CPUFreq table not found\n", __func__);
  297. return ERR_PTR(-EPROBE_DEFER);
  298. }
  299. cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
  300. if (!cpufreq_dev)
  301. return ERR_PTR(-ENOMEM);
  302. /* Find max levels */
  303. cpufreq_for_each_valid_entry(pos, table)
  304. cpufreq_dev->max_level++;
  305. cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
  306. cpufreq_dev->max_level, GFP_KERNEL);
  307. if (!cpufreq_dev->freq_table) {
  308. cool_dev = ERR_PTR(-ENOMEM);
  309. goto free_cdev;
  310. }
  311. /* max_level is an index, not a counter */
  312. cpufreq_dev->max_level--;
  313. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  314. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  315. if (ret) {
  316. cool_dev = ERR_PTR(ret);
  317. goto free_table;
  318. }
  319. snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
  320. cpufreq_dev->id);
  321. cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
  322. &cpufreq_cooling_ops);
  323. if (IS_ERR(cool_dev))
  324. goto remove_idr;
  325. /* Fill freq-table in descending order of frequencies */
  326. for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
  327. freq = find_next_max(table, freq);
  328. cpufreq_dev->freq_table[i] = freq;
  329. /* Warn for duplicate entries */
  330. if (!freq)
  331. pr_warn("%s: table has duplicate entries\n", __func__);
  332. else
  333. pr_debug("%s: freq:%u KHz\n", __func__, freq);
  334. }
  335. cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0];
  336. cpufreq_dev->cool_dev = cool_dev;
  337. mutex_lock(&cooling_cpufreq_lock);
  338. /* Register the notifier for first cpufreq cooling device */
  339. if (list_empty(&cpufreq_dev_list))
  340. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  341. CPUFREQ_POLICY_NOTIFIER);
  342. list_add(&cpufreq_dev->node, &cpufreq_dev_list);
  343. mutex_unlock(&cooling_cpufreq_lock);
  344. return cool_dev;
  345. remove_idr:
  346. release_idr(&cpufreq_idr, cpufreq_dev->id);
  347. free_table:
  348. kfree(cpufreq_dev->freq_table);
  349. free_cdev:
  350. kfree(cpufreq_dev);
  351. return cool_dev;
  352. }
  353. /**
  354. * cpufreq_cooling_register - function to create cpufreq cooling device.
  355. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  356. *
  357. * This interface function registers the cpufreq cooling device with the name
  358. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  359. * cooling devices.
  360. *
  361. * Return: a valid struct thermal_cooling_device pointer on success,
  362. * on failure, it returns a corresponding ERR_PTR().
  363. */
  364. struct thermal_cooling_device *
  365. cpufreq_cooling_register(const struct cpumask *clip_cpus)
  366. {
  367. return __cpufreq_cooling_register(NULL, clip_cpus);
  368. }
  369. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  370. /**
  371. * of_cpufreq_cooling_register - function to create cpufreq cooling device.
  372. * @np: a valid struct device_node to the cooling device device tree node
  373. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  374. *
  375. * This interface function registers the cpufreq cooling device with the name
  376. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  377. * cooling devices. Using this API, the cpufreq cooling device will be
  378. * linked to the device tree node provided.
  379. *
  380. * Return: a valid struct thermal_cooling_device pointer on success,
  381. * on failure, it returns a corresponding ERR_PTR().
  382. */
  383. struct thermal_cooling_device *
  384. of_cpufreq_cooling_register(struct device_node *np,
  385. const struct cpumask *clip_cpus)
  386. {
  387. if (!np)
  388. return ERR_PTR(-EINVAL);
  389. return __cpufreq_cooling_register(np, clip_cpus);
  390. }
  391. EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
  392. /**
  393. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  394. * @cdev: thermal cooling device pointer.
  395. *
  396. * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  397. */
  398. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  399. {
  400. struct cpufreq_cooling_device *cpufreq_dev;
  401. if (!cdev)
  402. return;
  403. cpufreq_dev = cdev->devdata;
  404. mutex_lock(&cooling_cpufreq_lock);
  405. list_del(&cpufreq_dev->node);
  406. /* Unregister the notifier for the last cpufreq cooling device */
  407. if (list_empty(&cpufreq_dev_list))
  408. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  409. CPUFREQ_POLICY_NOTIFIER);
  410. mutex_unlock(&cooling_cpufreq_lock);
  411. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  412. release_idr(&cpufreq_idr, cpufreq_dev->id);
  413. kfree(cpufreq_dev->freq_table);
  414. kfree(cpufreq_dev);
  415. }
  416. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);