cpu_cooling.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. *
  42. * This structure is required for keeping information of each
  43. * cpufreq_cooling_device registered. In order to prevent corruption of this a
  44. * mutex lock cooling_cpufreq_lock is used.
  45. */
  46. struct cpufreq_cooling_device {
  47. int id;
  48. struct thermal_cooling_device *cool_dev;
  49. unsigned int cpufreq_state;
  50. unsigned int cpufreq_val;
  51. struct cpumask allowed_cpus;
  52. };
  53. static DEFINE_IDR(cpufreq_idr);
  54. static DEFINE_MUTEX(cooling_cpufreq_lock);
  55. static unsigned int cpufreq_dev_count;
  56. /* notify_table passes value to the CPUFREQ_ADJUST callback function. */
  57. #define NOTIFY_INVALID NULL
  58. static struct cpufreq_cooling_device *notify_device;
  59. /**
  60. * get_idr - function to get a unique id.
  61. * @idr: struct idr * handle used to create a id.
  62. * @id: int * value generated by this function.
  63. */
  64. static int get_idr(struct idr *idr, int *id)
  65. {
  66. int ret;
  67. mutex_lock(&cooling_cpufreq_lock);
  68. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  69. mutex_unlock(&cooling_cpufreq_lock);
  70. if (unlikely(ret < 0))
  71. return ret;
  72. *id = ret;
  73. return 0;
  74. }
  75. /**
  76. * release_idr - function to free the unique id.
  77. * @idr: struct idr * handle used for creating the id.
  78. * @id: int value representing the unique id.
  79. */
  80. static void release_idr(struct idr *idr, int id)
  81. {
  82. mutex_lock(&cooling_cpufreq_lock);
  83. idr_remove(idr, id);
  84. mutex_unlock(&cooling_cpufreq_lock);
  85. }
  86. /* Below code defines functions to be used for cpufreq as cooling device */
  87. /**
  88. * is_cpufreq_valid - function to check frequency transitioning capability.
  89. * @cpu: cpu for which check is needed.
  90. *
  91. * This function will check the current state of the system if
  92. * it is capable of changing the frequency for a given @cpu.
  93. *
  94. * Return: 0 if the system is not currently capable of changing
  95. * the frequency of given cpu. !0 in case the frequency is changeable.
  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. * get_property - fetch a property of interest for a give cpu.
  109. * @cpu: cpu for which the property is required
  110. * @input: query parameter
  111. * @output: query return
  112. * @property: type of query (frequency, level, max level)
  113. *
  114. * This is the common function to
  115. * 1. get maximum cpu cooling states
  116. * 2. translate frequency to cooling state
  117. * 3. translate cooling state to frequency
  118. * Note that the code may be not in good shape
  119. * but it is written in this way in order to:
  120. * a) reduce duplicate code as most of the code can be shared.
  121. * b) make sure the logic is consistent when translating between
  122. * cooling states and frequencies.
  123. *
  124. * Return: 0 on success, -EINVAL when invalid parameters are passed.
  125. */
  126. static int get_property(unsigned int cpu, unsigned long input,
  127. unsigned int* output, enum cpufreq_cooling_property property)
  128. {
  129. int i, j;
  130. unsigned long max_level = 0, level = 0;
  131. unsigned int freq = CPUFREQ_ENTRY_INVALID;
  132. int descend = -1;
  133. struct cpufreq_frequency_table *table =
  134. cpufreq_frequency_get_table(cpu);
  135. if (!output)
  136. return -EINVAL;
  137. if (!table)
  138. return -EINVAL;
  139. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  140. /* ignore invalid entries */
  141. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  142. continue;
  143. /* ignore duplicate entry */
  144. if (freq == table[i].frequency)
  145. continue;
  146. /* get the frequency order */
  147. if (freq != CPUFREQ_ENTRY_INVALID && descend != -1)
  148. descend = !!(freq > table[i].frequency);
  149. freq = table[i].frequency;
  150. max_level++;
  151. }
  152. /* get max level */
  153. if (property == GET_MAXL) {
  154. *output = (unsigned int)max_level;
  155. return 0;
  156. }
  157. if (property == GET_FREQ)
  158. level = descend ? input : (max_level - input -1);
  159. for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  160. /* ignore invalid entry */
  161. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  162. continue;
  163. /* ignore duplicate entry */
  164. if (freq == table[i].frequency)
  165. continue;
  166. /* now we have a valid frequency entry */
  167. freq = table[i].frequency;
  168. if (property == GET_LEVEL && (unsigned int)input == freq) {
  169. /* get level by frequency */
  170. *output = descend ? j : (max_level - j - 1);
  171. return 0;
  172. }
  173. if (property == GET_FREQ && level == j) {
  174. /* get frequency by level */
  175. *output = freq;
  176. return 0;
  177. }
  178. j++;
  179. }
  180. return -EINVAL;
  181. }
  182. /**
  183. * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
  184. * @cpu: cpu for which the level is required
  185. * @freq: the frequency of interest
  186. *
  187. * This function will match the cooling level corresponding to the
  188. * requested @freq and return it.
  189. *
  190. * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
  191. * otherwise.
  192. */
  193. unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
  194. {
  195. unsigned int val;
  196. if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
  197. return THERMAL_CSTATE_INVALID;
  198. return (unsigned long)val;
  199. }
  200. EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
  201. /**
  202. * get_cpu_frequency - get the absolute value of frequency from level.
  203. * @cpu: cpu for which frequency is fetched.
  204. * @level: cooling level
  205. *
  206. * This function matches cooling level with frequency. Based on a cooling level
  207. * of frequency, equals cooling state of cpu cooling device, it will return
  208. * the corresponding frequency.
  209. * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
  210. *
  211. * Return: 0 on error, the corresponding frequency otherwise.
  212. */
  213. static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  214. {
  215. int ret = 0;
  216. unsigned int freq;
  217. ret = get_property(cpu, level, &freq, GET_FREQ);
  218. if (ret)
  219. return 0;
  220. return freq;
  221. }
  222. /**
  223. * cpufreq_apply_cooling - function to apply frequency clipping.
  224. * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  225. * clipping data.
  226. * @cooling_state: value of the cooling state.
  227. *
  228. * Function used to make sure the cpufreq layer is aware of current thermal
  229. * limits. The limits are applied by updating the cpufreq policy.
  230. *
  231. * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
  232. * cooling state).
  233. */
  234. static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  235. unsigned long cooling_state)
  236. {
  237. unsigned int cpuid, clip_freq;
  238. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  239. unsigned int cpu = cpumask_any(mask);
  240. /* Check if the old cooling action is same as new cooling action */
  241. if (cpufreq_device->cpufreq_state == cooling_state)
  242. return 0;
  243. clip_freq = get_cpu_frequency(cpu, cooling_state);
  244. if (!clip_freq)
  245. return -EINVAL;
  246. cpufreq_device->cpufreq_state = cooling_state;
  247. cpufreq_device->cpufreq_val = clip_freq;
  248. notify_device = cpufreq_device;
  249. for_each_cpu(cpuid, mask) {
  250. if (is_cpufreq_valid(cpuid))
  251. cpufreq_update_policy(cpuid);
  252. }
  253. notify_device = NOTIFY_INVALID;
  254. return 0;
  255. }
  256. /**
  257. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  258. * @nb: struct notifier_block * with callback info.
  259. * @event: value showing cpufreq event for which this function invoked.
  260. * @data: callback-specific data
  261. *
  262. * Callback to highjack the notification on cpufreq policy transition.
  263. * Every time there is a change in policy, we will intercept and
  264. * update the cpufreq policy with thermal constraints.
  265. *
  266. * Return: 0 (success)
  267. */
  268. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  269. unsigned long event, void *data)
  270. {
  271. struct cpufreq_policy *policy = data;
  272. unsigned long max_freq = 0;
  273. if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
  274. return 0;
  275. if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
  276. max_freq = notify_device->cpufreq_val;
  277. /* Never exceed user_policy.max*/
  278. if (max_freq > policy->user_policy.max)
  279. max_freq = policy->user_policy.max;
  280. if (policy->max != max_freq)
  281. cpufreq_verify_within_limits(policy, 0, max_freq);
  282. return 0;
  283. }
  284. /*
  285. * cpufreq cooling device callback functions are defined below
  286. */
  287. /**
  288. * cpufreq_get_max_state - callback function to get the max cooling state.
  289. * @cdev: thermal cooling device pointer.
  290. * @state: fill this variable with the max cooling state.
  291. *
  292. * Callback for the thermal cooling device to return the cpufreq
  293. * max cooling state.
  294. *
  295. * Return: 0 on success, an error code otherwise.
  296. */
  297. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  298. unsigned long *state)
  299. {
  300. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  301. struct cpumask *mask = &cpufreq_device->allowed_cpus;
  302. unsigned int cpu;
  303. unsigned int count = 0;
  304. int ret;
  305. cpu = cpumask_any(mask);
  306. ret = get_property(cpu, 0, &count, GET_MAXL);
  307. if (count > 0)
  308. *state = count;
  309. return ret;
  310. }
  311. /**
  312. * cpufreq_get_cur_state - callback function to get the current cooling state.
  313. * @cdev: thermal cooling device pointer.
  314. * @state: fill this variable with the current cooling state.
  315. *
  316. * Callback for the thermal cooling device to return the cpufreq
  317. * current cooling state.
  318. *
  319. * Return: 0 on success, an error code otherwise.
  320. */
  321. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  322. unsigned long *state)
  323. {
  324. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  325. *state = cpufreq_device->cpufreq_state;
  326. return 0;
  327. }
  328. /**
  329. * cpufreq_set_cur_state - callback function to set the current cooling state.
  330. * @cdev: thermal cooling device pointer.
  331. * @state: set this variable to the current cooling state.
  332. *
  333. * Callback for the thermal cooling device to change the cpufreq
  334. * current cooling state.
  335. *
  336. * Return: 0 on success, an error code otherwise.
  337. */
  338. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  339. unsigned long state)
  340. {
  341. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  342. return cpufreq_apply_cooling(cpufreq_device, state);
  343. }
  344. /* Bind cpufreq callbacks to thermal cooling device ops */
  345. static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
  346. .get_max_state = cpufreq_get_max_state,
  347. .get_cur_state = cpufreq_get_cur_state,
  348. .set_cur_state = cpufreq_set_cur_state,
  349. };
  350. /* Notifier for cpufreq policy change */
  351. static struct notifier_block thermal_cpufreq_notifier_block = {
  352. .notifier_call = cpufreq_thermal_notifier,
  353. };
  354. /**
  355. * cpufreq_cooling_register - function to create cpufreq cooling device.
  356. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  357. *
  358. * This interface function registers the cpufreq cooling device with the name
  359. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  360. * cooling devices.
  361. *
  362. * Return: a valid struct thermal_cooling_device pointer on success,
  363. * on failure, it returns a corresponding ERR_PTR().
  364. */
  365. struct thermal_cooling_device *cpufreq_cooling_register(
  366. const struct cpumask *clip_cpus)
  367. {
  368. struct thermal_cooling_device *cool_dev;
  369. struct cpufreq_cooling_device *cpufreq_dev = NULL;
  370. unsigned int min = 0, max = 0;
  371. char dev_name[THERMAL_NAME_LENGTH];
  372. int ret = 0, i;
  373. struct cpufreq_policy policy;
  374. /*Verify that all the clip cpus have same freq_min, freq_max limit*/
  375. for_each_cpu(i, clip_cpus) {
  376. /*continue if cpufreq policy not found and not return error*/
  377. if (!cpufreq_get_policy(&policy, i))
  378. continue;
  379. if (min == 0 && max == 0) {
  380. min = policy.cpuinfo.min_freq;
  381. max = policy.cpuinfo.max_freq;
  382. } else {
  383. if (min != policy.cpuinfo.min_freq ||
  384. max != policy.cpuinfo.max_freq)
  385. return ERR_PTR(-EINVAL);
  386. }
  387. }
  388. cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
  389. GFP_KERNEL);
  390. if (!cpufreq_dev)
  391. return ERR_PTR(-ENOMEM);
  392. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  393. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  394. if (ret) {
  395. kfree(cpufreq_dev);
  396. return ERR_PTR(-EINVAL);
  397. }
  398. snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
  399. cpufreq_dev->id);
  400. cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
  401. &cpufreq_cooling_ops);
  402. if (!cool_dev) {
  403. release_idr(&cpufreq_idr, cpufreq_dev->id);
  404. kfree(cpufreq_dev);
  405. return ERR_PTR(-EINVAL);
  406. }
  407. cpufreq_dev->cool_dev = cool_dev;
  408. cpufreq_dev->cpufreq_state = 0;
  409. mutex_lock(&cooling_cpufreq_lock);
  410. /* Register the notifier for first cpufreq cooling device */
  411. if (cpufreq_dev_count == 0)
  412. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  413. CPUFREQ_POLICY_NOTIFIER);
  414. cpufreq_dev_count++;
  415. mutex_unlock(&cooling_cpufreq_lock);
  416. return cool_dev;
  417. }
  418. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  419. /**
  420. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  421. * @cdev: thermal cooling device pointer.
  422. *
  423. * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  424. */
  425. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  426. {
  427. struct cpufreq_cooling_device *cpufreq_dev = cdev->devdata;
  428. mutex_lock(&cooling_cpufreq_lock);
  429. cpufreq_dev_count--;
  430. /* Unregister the notifier for the last cpufreq cooling device */
  431. if (cpufreq_dev_count == 0)
  432. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  433. CPUFREQ_POLICY_NOTIFIER);
  434. mutex_unlock(&cooling_cpufreq_lock);
  435. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  436. release_idr(&cpufreq_idr, cpufreq_dev->id);
  437. kfree(cpufreq_dev);
  438. }
  439. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);