cpu_cooling.c 17 KB

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