cpu_cooling.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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/idr.h>
  30. #include <linux/pm_opp.h>
  31. #include <linux/slab.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpu_cooling.h>
  34. #include <trace/events/thermal.h>
  35. /*
  36. * Cooling state <-> CPUFreq frequency
  37. *
  38. * Cooling states are translated to frequencies throughout this driver and this
  39. * is the relation between them.
  40. *
  41. * Highest cooling state corresponds to lowest possible frequency.
  42. *
  43. * i.e.
  44. * level 0 --> 1st Max Freq
  45. * level 1 --> 2nd Max Freq
  46. * ...
  47. */
  48. /**
  49. * struct freq_table - frequency table along with power entries
  50. * @frequency: frequency in KHz
  51. * @power: power in mW
  52. *
  53. * This structure is built when the cooling device registers and helps
  54. * in translating frequency to power and vice versa.
  55. */
  56. struct freq_table {
  57. u32 frequency;
  58. u32 power;
  59. };
  60. /**
  61. * struct time_in_idle - Idle time stats
  62. * @time: previous reading of the absolute time that this cpu was idle
  63. * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
  64. */
  65. struct time_in_idle {
  66. u64 time;
  67. u64 timestamp;
  68. };
  69. /**
  70. * struct cpufreq_cooling_device - data for cooling device with cpufreq
  71. * @id: unique integer value corresponding to each cpufreq_cooling_device
  72. * registered.
  73. * @last_load: load measured by the latest call to cpufreq_get_requested_power()
  74. * @cpufreq_state: integer value representing the current state of cpufreq
  75. * cooling devices.
  76. * @clipped_freq: integer value representing the absolute value of the clipped
  77. * frequency.
  78. * @max_level: maximum cooling level. One less than total number of valid
  79. * cpufreq frequencies.
  80. * @freq_table: Freq table in descending order of frequencies
  81. * @cdev: thermal_cooling_device pointer to keep track of the
  82. * registered cooling device.
  83. * @policy: cpufreq policy.
  84. * @node: list_head to link all cpufreq_cooling_device together.
  85. * @idle_time: idle time stats
  86. * @plat_get_static_power: callback to calculate the static power
  87. *
  88. * This structure is required for keeping information of each registered
  89. * cpufreq_cooling_device.
  90. */
  91. struct cpufreq_cooling_device {
  92. int id;
  93. u32 last_load;
  94. unsigned int cpufreq_state;
  95. unsigned int clipped_freq;
  96. unsigned int max_level;
  97. struct freq_table *freq_table; /* In descending order */
  98. struct thermal_cooling_device *cdev;
  99. struct cpufreq_policy *policy;
  100. struct list_head node;
  101. struct time_in_idle *idle_time;
  102. get_static_t plat_get_static_power;
  103. };
  104. static DEFINE_IDA(cpufreq_ida);
  105. static DEFINE_MUTEX(cooling_list_lock);
  106. static LIST_HEAD(cpufreq_cdev_list);
  107. /* Below code defines functions to be used for cpufreq as cooling device */
  108. /**
  109. * get_level: Find the level for a particular frequency
  110. * @cpufreq_cdev: cpufreq_cdev for which the property is required
  111. * @freq: Frequency
  112. *
  113. * Return: level corresponding to the frequency.
  114. */
  115. static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
  116. unsigned int freq)
  117. {
  118. struct freq_table *freq_table = cpufreq_cdev->freq_table;
  119. unsigned long level;
  120. for (level = 1; level <= cpufreq_cdev->max_level; level++)
  121. if (freq > freq_table[level].frequency)
  122. break;
  123. return level - 1;
  124. }
  125. /**
  126. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  127. * @nb: struct notifier_block * with callback info.
  128. * @event: value showing cpufreq event for which this function invoked.
  129. * @data: callback-specific data
  130. *
  131. * Callback to hijack the notification on cpufreq policy transition.
  132. * Every time there is a change in policy, we will intercept and
  133. * update the cpufreq policy with thermal constraints.
  134. *
  135. * Return: 0 (success)
  136. */
  137. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  138. unsigned long event, void *data)
  139. {
  140. struct cpufreq_policy *policy = data;
  141. unsigned long clipped_freq;
  142. struct cpufreq_cooling_device *cpufreq_cdev;
  143. if (event != CPUFREQ_ADJUST)
  144. return NOTIFY_DONE;
  145. mutex_lock(&cooling_list_lock);
  146. list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
  147. /*
  148. * A new copy of the policy is sent to the notifier and can't
  149. * compare that directly.
  150. */
  151. if (policy->cpu != cpufreq_cdev->policy->cpu)
  152. continue;
  153. /*
  154. * policy->max is the maximum allowed frequency defined by user
  155. * and clipped_freq is the maximum that thermal constraints
  156. * allow.
  157. *
  158. * If clipped_freq is lower than policy->max, then we need to
  159. * readjust policy->max.
  160. *
  161. * But, if clipped_freq is greater than policy->max, we don't
  162. * need to do anything.
  163. */
  164. clipped_freq = cpufreq_cdev->clipped_freq;
  165. if (policy->max > clipped_freq)
  166. cpufreq_verify_within_limits(policy, 0, clipped_freq);
  167. break;
  168. }
  169. mutex_unlock(&cooling_list_lock);
  170. return NOTIFY_OK;
  171. }
  172. /**
  173. * update_freq_table() - Update the freq table with power numbers
  174. * @cpufreq_cdev: the cpufreq cooling device in which to update the table
  175. * @capacitance: dynamic power coefficient for these cpus
  176. *
  177. * Update the freq table with power numbers. This table will be used in
  178. * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
  179. * frequency efficiently. Power is stored in mW, frequency in KHz. The
  180. * resulting table is in descending order.
  181. *
  182. * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
  183. * or -ENOMEM if we run out of memory.
  184. */
  185. static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
  186. u32 capacitance)
  187. {
  188. struct freq_table *freq_table = cpufreq_cdev->freq_table;
  189. struct dev_pm_opp *opp;
  190. struct device *dev = NULL;
  191. int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
  192. dev = get_cpu_device(cpu);
  193. if (unlikely(!dev)) {
  194. dev_warn(&cpufreq_cdev->cdev->device,
  195. "No cpu device for cpu %d\n", cpu);
  196. return -ENODEV;
  197. }
  198. num_opps = dev_pm_opp_get_opp_count(dev);
  199. if (num_opps < 0)
  200. return num_opps;
  201. /*
  202. * The cpufreq table is also built from the OPP table and so the count
  203. * should match.
  204. */
  205. if (num_opps != cpufreq_cdev->max_level + 1) {
  206. dev_warn(dev, "Number of OPPs not matching with max_levels\n");
  207. return -EINVAL;
  208. }
  209. for (i = 0; i <= cpufreq_cdev->max_level; i++) {
  210. unsigned long freq = freq_table[i].frequency * 1000;
  211. u32 freq_mhz = freq_table[i].frequency / 1000;
  212. u64 power;
  213. u32 voltage_mv;
  214. /*
  215. * Find ceil frequency as 'freq' may be slightly lower than OPP
  216. * freq due to truncation while converting to kHz.
  217. */
  218. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  219. if (IS_ERR(opp)) {
  220. dev_err(dev, "failed to get opp for %lu frequency\n",
  221. freq);
  222. return -EINVAL;
  223. }
  224. voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
  225. dev_pm_opp_put(opp);
  226. /*
  227. * Do the multiplication with MHz and millivolt so as
  228. * to not overflow.
  229. */
  230. power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
  231. do_div(power, 1000000000);
  232. /* power is stored in mW */
  233. freq_table[i].power = power;
  234. }
  235. return 0;
  236. }
  237. static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
  238. u32 freq)
  239. {
  240. int i;
  241. struct freq_table *freq_table = cpufreq_cdev->freq_table;
  242. for (i = 1; i <= cpufreq_cdev->max_level; i++)
  243. if (freq > freq_table[i].frequency)
  244. break;
  245. return freq_table[i - 1].power;
  246. }
  247. static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
  248. u32 power)
  249. {
  250. int i;
  251. struct freq_table *freq_table = cpufreq_cdev->freq_table;
  252. for (i = 1; i <= cpufreq_cdev->max_level; i++)
  253. if (power > freq_table[i].power)
  254. break;
  255. return freq_table[i - 1].frequency;
  256. }
  257. /**
  258. * get_load() - get load for a cpu since last updated
  259. * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
  260. * @cpu: cpu number
  261. * @cpu_idx: index of the cpu in time_in_idle*
  262. *
  263. * Return: The average load of cpu @cpu in percentage since this
  264. * function was last called.
  265. */
  266. static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
  267. int cpu_idx)
  268. {
  269. u32 load;
  270. u64 now, now_idle, delta_time, delta_idle;
  271. struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
  272. now_idle = get_cpu_idle_time(cpu, &now, 0);
  273. delta_idle = now_idle - idle_time->time;
  274. delta_time = now - idle_time->timestamp;
  275. if (delta_time <= delta_idle)
  276. load = 0;
  277. else
  278. load = div64_u64(100 * (delta_time - delta_idle), delta_time);
  279. idle_time->time = now_idle;
  280. idle_time->timestamp = now;
  281. return load;
  282. }
  283. /**
  284. * get_static_power() - calculate the static power consumed by the cpus
  285. * @cpufreq_cdev: struct &cpufreq_cooling_device for this cpu cdev
  286. * @tz: thermal zone device in which we're operating
  287. * @freq: frequency in KHz
  288. * @power: pointer in which to store the calculated static power
  289. *
  290. * Calculate the static power consumed by the cpus described by
  291. * @cpu_actor running at frequency @freq. This function relies on a
  292. * platform specific function that should have been provided when the
  293. * actor was registered. If it wasn't, the static power is assumed to
  294. * be negligible. The calculated static power is stored in @power.
  295. *
  296. * Return: 0 on success, -E* on failure.
  297. */
  298. static int get_static_power(struct cpufreq_cooling_device *cpufreq_cdev,
  299. struct thermal_zone_device *tz, unsigned long freq,
  300. u32 *power)
  301. {
  302. struct dev_pm_opp *opp;
  303. unsigned long voltage;
  304. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  305. struct cpumask *cpumask = policy->related_cpus;
  306. unsigned long freq_hz = freq * 1000;
  307. struct device *dev;
  308. if (!cpufreq_cdev->plat_get_static_power) {
  309. *power = 0;
  310. return 0;
  311. }
  312. dev = get_cpu_device(policy->cpu);
  313. WARN_ON(!dev);
  314. opp = dev_pm_opp_find_freq_exact(dev, freq_hz, true);
  315. if (IS_ERR(opp)) {
  316. dev_warn_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
  317. freq_hz, PTR_ERR(opp));
  318. return -EINVAL;
  319. }
  320. voltage = dev_pm_opp_get_voltage(opp);
  321. dev_pm_opp_put(opp);
  322. if (voltage == 0) {
  323. dev_err_ratelimited(dev, "Failed to get voltage for frequency %lu\n",
  324. freq_hz);
  325. return -EINVAL;
  326. }
  327. return cpufreq_cdev->plat_get_static_power(cpumask, tz->passive_delay,
  328. voltage, power);
  329. }
  330. /**
  331. * get_dynamic_power() - calculate the dynamic power
  332. * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
  333. * @freq: current frequency
  334. *
  335. * Return: the dynamic power consumed by the cpus described by
  336. * @cpufreq_cdev.
  337. */
  338. static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
  339. unsigned long freq)
  340. {
  341. u32 raw_cpu_power;
  342. raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
  343. return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
  344. }
  345. /* cpufreq cooling device callback functions are defined below */
  346. /**
  347. * cpufreq_get_max_state - callback function to get the max cooling state.
  348. * @cdev: thermal cooling device pointer.
  349. * @state: fill this variable with the max cooling state.
  350. *
  351. * Callback for the thermal cooling device to return the cpufreq
  352. * max cooling state.
  353. *
  354. * Return: 0 on success, an error code otherwise.
  355. */
  356. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  357. unsigned long *state)
  358. {
  359. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  360. *state = cpufreq_cdev->max_level;
  361. return 0;
  362. }
  363. /**
  364. * cpufreq_get_cur_state - callback function to get the current cooling state.
  365. * @cdev: thermal cooling device pointer.
  366. * @state: fill this variable with the current cooling state.
  367. *
  368. * Callback for the thermal cooling device to return the cpufreq
  369. * current cooling state.
  370. *
  371. * Return: 0 on success, an error code otherwise.
  372. */
  373. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  374. unsigned long *state)
  375. {
  376. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  377. *state = cpufreq_cdev->cpufreq_state;
  378. return 0;
  379. }
  380. /**
  381. * cpufreq_set_cur_state - callback function to set the current cooling state.
  382. * @cdev: thermal cooling device pointer.
  383. * @state: set this variable to the current cooling state.
  384. *
  385. * Callback for the thermal cooling device to change the cpufreq
  386. * current cooling state.
  387. *
  388. * Return: 0 on success, an error code otherwise.
  389. */
  390. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  391. unsigned long state)
  392. {
  393. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  394. unsigned int clip_freq;
  395. /* Request state should be less than max_level */
  396. if (WARN_ON(state > cpufreq_cdev->max_level))
  397. return -EINVAL;
  398. /* Check if the old cooling action is same as new cooling action */
  399. if (cpufreq_cdev->cpufreq_state == state)
  400. return 0;
  401. clip_freq = cpufreq_cdev->freq_table[state].frequency;
  402. cpufreq_cdev->cpufreq_state = state;
  403. cpufreq_cdev->clipped_freq = clip_freq;
  404. cpufreq_update_policy(cpufreq_cdev->policy->cpu);
  405. return 0;
  406. }
  407. /**
  408. * cpufreq_get_requested_power() - get the current power
  409. * @cdev: &thermal_cooling_device pointer
  410. * @tz: a valid thermal zone device pointer
  411. * @power: pointer in which to store the resulting power
  412. *
  413. * Calculate the current power consumption of the cpus in milliwatts
  414. * and store it in @power. This function should actually calculate
  415. * the requested power, but it's hard to get the frequency that
  416. * cpufreq would have assigned if there were no thermal limits.
  417. * Instead, we calculate the current power on the assumption that the
  418. * immediate future will look like the immediate past.
  419. *
  420. * We use the current frequency and the average load since this
  421. * function was last called. In reality, there could have been
  422. * multiple opps since this function was last called and that affects
  423. * the load calculation. While it's not perfectly accurate, this
  424. * simplification is good enough and works. REVISIT this, as more
  425. * complex code may be needed if experiments show that it's not
  426. * accurate enough.
  427. *
  428. * Return: 0 on success, -E* if getting the static power failed.
  429. */
  430. static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
  431. struct thermal_zone_device *tz,
  432. u32 *power)
  433. {
  434. unsigned long freq;
  435. int i = 0, cpu, ret;
  436. u32 static_power, dynamic_power, total_load = 0;
  437. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  438. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  439. u32 *load_cpu = NULL;
  440. freq = cpufreq_quick_get(policy->cpu);
  441. if (trace_thermal_power_cpu_get_power_enabled()) {
  442. u32 ncpus = cpumask_weight(policy->related_cpus);
  443. load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
  444. }
  445. for_each_cpu(cpu, policy->related_cpus) {
  446. u32 load;
  447. if (cpu_online(cpu))
  448. load = get_load(cpufreq_cdev, cpu, i);
  449. else
  450. load = 0;
  451. total_load += load;
  452. if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
  453. load_cpu[i] = load;
  454. i++;
  455. }
  456. cpufreq_cdev->last_load = total_load;
  457. dynamic_power = get_dynamic_power(cpufreq_cdev, freq);
  458. ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
  459. if (ret) {
  460. kfree(load_cpu);
  461. return ret;
  462. }
  463. if (load_cpu) {
  464. trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
  465. load_cpu, i, dynamic_power,
  466. static_power);
  467. kfree(load_cpu);
  468. }
  469. *power = static_power + dynamic_power;
  470. return 0;
  471. }
  472. /**
  473. * cpufreq_state2power() - convert a cpu cdev state to power consumed
  474. * @cdev: &thermal_cooling_device pointer
  475. * @tz: a valid thermal zone device pointer
  476. * @state: cooling device state to be converted
  477. * @power: pointer in which to store the resulting power
  478. *
  479. * Convert cooling device state @state into power consumption in
  480. * milliwatts assuming 100% load. Store the calculated power in
  481. * @power.
  482. *
  483. * Return: 0 on success, -EINVAL if the cooling device state could not
  484. * be converted into a frequency or other -E* if there was an error
  485. * when calculating the static power.
  486. */
  487. static int cpufreq_state2power(struct thermal_cooling_device *cdev,
  488. struct thermal_zone_device *tz,
  489. unsigned long state, u32 *power)
  490. {
  491. unsigned int freq, num_cpus;
  492. u32 static_power, dynamic_power;
  493. int ret;
  494. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  495. /* Request state should be less than max_level */
  496. if (WARN_ON(state > cpufreq_cdev->max_level))
  497. return -EINVAL;
  498. num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
  499. freq = cpufreq_cdev->freq_table[state].frequency;
  500. dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
  501. ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
  502. if (ret)
  503. return ret;
  504. *power = static_power + dynamic_power;
  505. return ret;
  506. }
  507. /**
  508. * cpufreq_power2state() - convert power to a cooling device state
  509. * @cdev: &thermal_cooling_device pointer
  510. * @tz: a valid thermal zone device pointer
  511. * @power: power in milliwatts to be converted
  512. * @state: pointer in which to store the resulting state
  513. *
  514. * Calculate a cooling device state for the cpus described by @cdev
  515. * that would allow them to consume at most @power mW and store it in
  516. * @state. Note that this calculation depends on external factors
  517. * such as the cpu load or the current static power. Calling this
  518. * function with the same power as input can yield different cooling
  519. * device states depending on those external factors.
  520. *
  521. * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
  522. * the calculated frequency could not be converted to a valid state.
  523. * The latter should not happen unless the frequencies available to
  524. * cpufreq have changed since the initialization of the cpu cooling
  525. * device.
  526. */
  527. static int cpufreq_power2state(struct thermal_cooling_device *cdev,
  528. struct thermal_zone_device *tz, u32 power,
  529. unsigned long *state)
  530. {
  531. unsigned int cur_freq, target_freq;
  532. int ret;
  533. s32 dyn_power;
  534. u32 last_load, normalised_power, static_power;
  535. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  536. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  537. cur_freq = cpufreq_quick_get(policy->cpu);
  538. ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power);
  539. if (ret)
  540. return ret;
  541. dyn_power = power - static_power;
  542. dyn_power = dyn_power > 0 ? dyn_power : 0;
  543. last_load = cpufreq_cdev->last_load ?: 1;
  544. normalised_power = (dyn_power * 100) / last_load;
  545. target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
  546. *state = get_level(cpufreq_cdev, target_freq);
  547. trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
  548. power);
  549. return 0;
  550. }
  551. /* Bind cpufreq callbacks to thermal cooling device ops */
  552. static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
  553. .get_max_state = cpufreq_get_max_state,
  554. .get_cur_state = cpufreq_get_cur_state,
  555. .set_cur_state = cpufreq_set_cur_state,
  556. };
  557. static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
  558. .get_max_state = cpufreq_get_max_state,
  559. .get_cur_state = cpufreq_get_cur_state,
  560. .set_cur_state = cpufreq_set_cur_state,
  561. .get_requested_power = cpufreq_get_requested_power,
  562. .state2power = cpufreq_state2power,
  563. .power2state = cpufreq_power2state,
  564. };
  565. /* Notifier for cpufreq policy change */
  566. static struct notifier_block thermal_cpufreq_notifier_block = {
  567. .notifier_call = cpufreq_thermal_notifier,
  568. };
  569. static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  570. unsigned int prev_max)
  571. {
  572. struct cpufreq_frequency_table *pos;
  573. unsigned int max = 0;
  574. cpufreq_for_each_valid_entry(pos, table) {
  575. if (pos->frequency > max && pos->frequency < prev_max)
  576. max = pos->frequency;
  577. }
  578. return max;
  579. }
  580. /**
  581. * __cpufreq_cooling_register - helper function to create cpufreq cooling device
  582. * @np: a valid struct device_node to the cooling device device tree node
  583. * @policy: cpufreq policy
  584. * Normally this should be same as cpufreq policy->related_cpus.
  585. * @capacitance: dynamic power coefficient for these cpus
  586. * @plat_static_func: function to calculate the static power consumed by these
  587. * cpus (optional)
  588. *
  589. * This interface function registers the cpufreq cooling device with the name
  590. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  591. * cooling devices. It also gives the opportunity to link the cooling device
  592. * with a device tree node, in order to bind it via the thermal DT code.
  593. *
  594. * Return: a valid struct thermal_cooling_device pointer on success,
  595. * on failure, it returns a corresponding ERR_PTR().
  596. */
  597. static struct thermal_cooling_device *
  598. __cpufreq_cooling_register(struct device_node *np,
  599. struct cpufreq_policy *policy, u32 capacitance,
  600. get_static_t plat_static_func)
  601. {
  602. struct thermal_cooling_device *cdev;
  603. struct cpufreq_cooling_device *cpufreq_cdev;
  604. char dev_name[THERMAL_NAME_LENGTH];
  605. unsigned int freq, i, num_cpus;
  606. int ret;
  607. struct thermal_cooling_device_ops *cooling_ops;
  608. bool first;
  609. if (IS_ERR_OR_NULL(policy)) {
  610. pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy);
  611. return ERR_PTR(-EINVAL);
  612. }
  613. i = cpufreq_table_count_valid_entries(policy);
  614. if (!i) {
  615. pr_debug("%s: CPUFreq table not found or has no valid entries\n",
  616. __func__);
  617. return ERR_PTR(-ENODEV);
  618. }
  619. cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
  620. if (!cpufreq_cdev)
  621. return ERR_PTR(-ENOMEM);
  622. cpufreq_cdev->policy = policy;
  623. num_cpus = cpumask_weight(policy->related_cpus);
  624. cpufreq_cdev->idle_time = kcalloc(num_cpus,
  625. sizeof(*cpufreq_cdev->idle_time),
  626. GFP_KERNEL);
  627. if (!cpufreq_cdev->idle_time) {
  628. cdev = ERR_PTR(-ENOMEM);
  629. goto free_cdev;
  630. }
  631. /* max_level is an index, not a counter */
  632. cpufreq_cdev->max_level = i - 1;
  633. cpufreq_cdev->freq_table = kmalloc_array(i,
  634. sizeof(*cpufreq_cdev->freq_table),
  635. GFP_KERNEL);
  636. if (!cpufreq_cdev->freq_table) {
  637. cdev = ERR_PTR(-ENOMEM);
  638. goto free_idle_time;
  639. }
  640. ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
  641. if (ret < 0) {
  642. cdev = ERR_PTR(ret);
  643. goto free_table;
  644. }
  645. cpufreq_cdev->id = ret;
  646. snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
  647. cpufreq_cdev->id);
  648. /* Fill freq-table in descending order of frequencies */
  649. for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
  650. freq = find_next_max(policy->freq_table, freq);
  651. cpufreq_cdev->freq_table[i].frequency = freq;
  652. /* Warn for duplicate entries */
  653. if (!freq)
  654. pr_warn("%s: table has duplicate entries\n", __func__);
  655. else
  656. pr_debug("%s: freq:%u KHz\n", __func__, freq);
  657. }
  658. if (capacitance) {
  659. cpufreq_cdev->plat_get_static_power = plat_static_func;
  660. ret = update_freq_table(cpufreq_cdev, capacitance);
  661. if (ret) {
  662. cdev = ERR_PTR(ret);
  663. goto remove_ida;
  664. }
  665. cooling_ops = &cpufreq_power_cooling_ops;
  666. } else {
  667. cooling_ops = &cpufreq_cooling_ops;
  668. }
  669. cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
  670. cooling_ops);
  671. if (IS_ERR(cdev))
  672. goto remove_ida;
  673. cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
  674. cpufreq_cdev->cdev = cdev;
  675. mutex_lock(&cooling_list_lock);
  676. /* Register the notifier for first cpufreq cooling device */
  677. first = list_empty(&cpufreq_cdev_list);
  678. list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
  679. mutex_unlock(&cooling_list_lock);
  680. if (first)
  681. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  682. CPUFREQ_POLICY_NOTIFIER);
  683. return cdev;
  684. remove_ida:
  685. ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
  686. free_table:
  687. kfree(cpufreq_cdev->freq_table);
  688. free_idle_time:
  689. kfree(cpufreq_cdev->idle_time);
  690. free_cdev:
  691. kfree(cpufreq_cdev);
  692. return cdev;
  693. }
  694. /**
  695. * cpufreq_cooling_register - function to create cpufreq cooling device.
  696. * @policy: cpufreq policy
  697. *
  698. * This interface function registers the cpufreq cooling device with the name
  699. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  700. * cooling devices.
  701. *
  702. * Return: a valid struct thermal_cooling_device pointer on success,
  703. * on failure, it returns a corresponding ERR_PTR().
  704. */
  705. struct thermal_cooling_device *
  706. cpufreq_cooling_register(struct cpufreq_policy *policy)
  707. {
  708. return __cpufreq_cooling_register(NULL, policy, 0, NULL);
  709. }
  710. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  711. /**
  712. * of_cpufreq_cooling_register - function to create cpufreq cooling device.
  713. * @np: a valid struct device_node to the cooling device device tree node
  714. * @policy: cpufreq policy
  715. *
  716. * This interface function registers the cpufreq cooling device with the name
  717. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  718. * cooling devices. Using this API, the cpufreq cooling device will be
  719. * linked to the device tree node provided.
  720. *
  721. * Return: a valid struct thermal_cooling_device pointer on success,
  722. * on failure, it returns a corresponding ERR_PTR().
  723. */
  724. struct thermal_cooling_device *
  725. of_cpufreq_cooling_register(struct device_node *np,
  726. struct cpufreq_policy *policy)
  727. {
  728. if (!np)
  729. return ERR_PTR(-EINVAL);
  730. return __cpufreq_cooling_register(np, policy, 0, NULL);
  731. }
  732. EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
  733. /**
  734. * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  735. * @policy: cpufreq policy
  736. * @capacitance: dynamic power coefficient for these cpus
  737. * @plat_static_func: function to calculate the static power consumed by these
  738. * cpus (optional)
  739. *
  740. * This interface function registers the cpufreq cooling device with
  741. * the name "thermal-cpufreq-%x". This api can support multiple
  742. * instances of cpufreq cooling devices. Using this function, the
  743. * cooling device will implement the power extensions by using a
  744. * simple cpu power model. The cpus must have registered their OPPs
  745. * using the OPP library.
  746. *
  747. * An optional @plat_static_func may be provided to calculate the
  748. * static power consumed by these cpus. If the platform's static
  749. * power consumption is unknown or negligible, make it NULL.
  750. *
  751. * Return: a valid struct thermal_cooling_device pointer on success,
  752. * on failure, it returns a corresponding ERR_PTR().
  753. */
  754. struct thermal_cooling_device *
  755. cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance,
  756. get_static_t plat_static_func)
  757. {
  758. return __cpufreq_cooling_register(NULL, policy, capacitance,
  759. plat_static_func);
  760. }
  761. EXPORT_SYMBOL(cpufreq_power_cooling_register);
  762. /**
  763. * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  764. * @np: a valid struct device_node to the cooling device device tree node
  765. * @policy: cpufreq policy
  766. * @capacitance: dynamic power coefficient for these cpus
  767. * @plat_static_func: function to calculate the static power consumed by these
  768. * cpus (optional)
  769. *
  770. * This interface function registers the cpufreq cooling device with
  771. * the name "thermal-cpufreq-%x". This api can support multiple
  772. * instances of cpufreq cooling devices. Using this API, the cpufreq
  773. * cooling device will be linked to the device tree node provided.
  774. * Using this function, the cooling device will implement the power
  775. * extensions by using a simple cpu power model. The cpus must have
  776. * registered their OPPs using the OPP library.
  777. *
  778. * An optional @plat_static_func may be provided to calculate the
  779. * static power consumed by these cpus. If the platform's static
  780. * power consumption is unknown or negligible, make it NULL.
  781. *
  782. * Return: a valid struct thermal_cooling_device pointer on success,
  783. * on failure, it returns a corresponding ERR_PTR().
  784. */
  785. struct thermal_cooling_device *
  786. of_cpufreq_power_cooling_register(struct device_node *np,
  787. struct cpufreq_policy *policy,
  788. u32 capacitance,
  789. get_static_t plat_static_func)
  790. {
  791. if (!np)
  792. return ERR_PTR(-EINVAL);
  793. return __cpufreq_cooling_register(np, policy, capacitance,
  794. plat_static_func);
  795. }
  796. EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
  797. /**
  798. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  799. * @cdev: thermal cooling device pointer.
  800. *
  801. * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  802. */
  803. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  804. {
  805. struct cpufreq_cooling_device *cpufreq_cdev;
  806. bool last;
  807. if (!cdev)
  808. return;
  809. cpufreq_cdev = cdev->devdata;
  810. mutex_lock(&cooling_list_lock);
  811. list_del(&cpufreq_cdev->node);
  812. /* Unregister the notifier for the last cpufreq cooling device */
  813. last = list_empty(&cpufreq_cdev_list);
  814. mutex_unlock(&cooling_list_lock);
  815. if (last)
  816. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  817. CPUFREQ_POLICY_NOTIFIER);
  818. thermal_cooling_device_unregister(cpufreq_cdev->cdev);
  819. ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
  820. kfree(cpufreq_cdev->idle_time);
  821. kfree(cpufreq_cdev->freq_table);
  822. kfree(cpufreq_cdev);
  823. }
  824. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);