cpu_cooling.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 <linux/of_device.h>
  35. #include <trace/events/thermal.h>
  36. /*
  37. * Cooling state <-> CPUFreq frequency
  38. *
  39. * Cooling states are translated to frequencies throughout this driver and this
  40. * is the relation between them.
  41. *
  42. * Highest cooling state corresponds to lowest possible frequency.
  43. *
  44. * i.e.
  45. * level 0 --> 1st Max Freq
  46. * level 1 --> 2nd Max Freq
  47. * ...
  48. */
  49. /**
  50. * struct freq_table - frequency table along with power entries
  51. * @frequency: frequency in KHz
  52. * @power: power in mW
  53. *
  54. * This structure is built when the cooling device registers and helps
  55. * in translating frequency to power and vice versa.
  56. */
  57. struct freq_table {
  58. u32 frequency;
  59. u32 power;
  60. };
  61. /**
  62. * struct time_in_idle - Idle time stats
  63. * @time: previous reading of the absolute time that this cpu was idle
  64. * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
  65. */
  66. struct time_in_idle {
  67. u64 time;
  68. u64 timestamp;
  69. };
  70. /**
  71. * struct cpufreq_cooling_device - data for cooling device with cpufreq
  72. * @id: unique integer value corresponding to each cpufreq_cooling_device
  73. * registered.
  74. * @last_load: load measured by the latest call to cpufreq_get_requested_power()
  75. * @cpufreq_state: integer value representing the current state of cpufreq
  76. * cooling devices.
  77. * @clipped_freq: integer value representing the absolute value of the clipped
  78. * frequency.
  79. * @max_level: maximum cooling level. One less than total number of valid
  80. * cpufreq frequencies.
  81. * @freq_table: Freq table in descending order of frequencies
  82. * @cdev: thermal_cooling_device pointer to keep track of the
  83. * registered cooling device.
  84. * @policy: cpufreq policy.
  85. * @node: list_head to link all cpufreq_cooling_device together.
  86. * @idle_time: idle time stats
  87. * @plat_get_static_power: callback to calculate the static power
  88. *
  89. * This structure is required for keeping information of each registered
  90. * cpufreq_cooling_device.
  91. */
  92. struct cpufreq_cooling_device {
  93. int id;
  94. u32 last_load;
  95. unsigned int cpufreq_state;
  96. unsigned int clipped_freq;
  97. unsigned int max_level;
  98. struct freq_table *freq_table; /* In descending order */
  99. struct thermal_cooling_device *cdev;
  100. struct cpufreq_policy *policy;
  101. struct list_head node;
  102. struct time_in_idle *idle_time;
  103. get_static_t plat_get_static_power;
  104. };
  105. static DEFINE_IDA(cpufreq_ida);
  106. static DEFINE_MUTEX(cooling_list_lock);
  107. static LIST_HEAD(cpufreq_cdev_list);
  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_cdev: cpufreq_cdev for which the property is required
  112. * @freq: Frequency
  113. *
  114. * Return: level corresponding to the frequency.
  115. */
  116. static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
  117. unsigned int freq)
  118. {
  119. struct freq_table *freq_table = cpufreq_cdev->freq_table;
  120. unsigned long level;
  121. for (level = 1; level <= cpufreq_cdev->max_level; level++)
  122. if (freq > freq_table[level].frequency)
  123. break;
  124. return level - 1;
  125. }
  126. /**
  127. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  128. * @nb: struct notifier_block * with callback info.
  129. * @event: value showing cpufreq event for which this function invoked.
  130. * @data: callback-specific data
  131. *
  132. * Callback to hijack the notification on cpufreq policy transition.
  133. * Every time there is a change in policy, we will intercept and
  134. * update the cpufreq policy with thermal constraints.
  135. *
  136. * Return: 0 (success)
  137. */
  138. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  139. unsigned long event, void *data)
  140. {
  141. struct cpufreq_policy *policy = data;
  142. unsigned long clipped_freq = ULONG_MAX;
  143. struct cpufreq_cooling_device *cpufreq_cdev;
  144. if (event != CPUFREQ_THERMAL)
  145. return NOTIFY_DONE;
  146. mutex_lock(&cooling_list_lock);
  147. list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
  148. /*
  149. * A new copy of the policy is sent to the notifier and can't
  150. * compare that directly.
  151. */
  152. if (policy->cpu != cpufreq_cdev->policy->cpu)
  153. continue;
  154. /*
  155. * policy->max is the maximum allowed frequency defined by user
  156. * and clipped_freq is the maximum that thermal constraints
  157. * allow.
  158. *
  159. * If clipped_freq is lower than policy->max, then we need to
  160. * readjust policy->max.
  161. *
  162. * But, if clipped_freq is greater than policy->max, we don't
  163. * need to do anything.
  164. */
  165. if (clipped_freq > cpufreq_cdev->clipped_freq)
  166. clipped_freq = cpufreq_cdev->clipped_freq;
  167. }
  168. cpufreq_verify_within_limits(policy, 0, clipped_freq);
  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 = 0; i < cpufreq_cdev->max_level; i++)
  253. if (power >= freq_table[i].power)
  254. break;
  255. return freq_table[i].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 cpufreq_cdev->max_level;
  401. clip_freq = cpufreq_cdev->freq_table[state].frequency;
  402. cpufreq_cdev->cpufreq_state = state;
  403. cpufreq_cdev->clipped_freq = clip_freq;
  404. get_online_cpus();
  405. cpufreq_update_policy(cpufreq_cdev->policy->cpu);
  406. put_online_cpus();
  407. return 0;
  408. }
  409. void cpu_limits_set_level(unsigned int cpu, unsigned int max_freq)
  410. {
  411. struct cpufreq_cooling_device *cpufreq_cdev;
  412. struct thermal_cooling_device *cdev;
  413. unsigned int cdev_cpu;
  414. unsigned int level;
  415. list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
  416. sscanf(cpufreq_cdev->cdev->type, "thermal-cpufreq-%d", &cdev_cpu);
  417. if (cdev_cpu == cpu) {
  418. for (level = 0; level <= cpufreq_cdev->max_level; level++) {
  419. int target_freq = cpufreq_cdev->freq_table[level].frequency;
  420. pr_err("%s: %d not part of any cooling device\n", __func__, target_freq);
  421. if (max_freq >= target_freq) {
  422. cdev = cpufreq_cdev->cdev;
  423. if (cdev)
  424. cdev->ops->set_cur_state(cdev, level);
  425. break;
  426. }
  427. }
  428. break;
  429. }
  430. }
  431. }
  432. /**
  433. * cpufreq_get_requested_power() - get the current power
  434. * @cdev: &thermal_cooling_device pointer
  435. * @tz: a valid thermal zone device pointer
  436. * @power: pointer in which to store the resulting power
  437. *
  438. * Calculate the current power consumption of the cpus in milliwatts
  439. * and store it in @power. This function should actually calculate
  440. * the requested power, but it's hard to get the frequency that
  441. * cpufreq would have assigned if there were no thermal limits.
  442. * Instead, we calculate the current power on the assumption that the
  443. * immediate future will look like the immediate past.
  444. *
  445. * We use the current frequency and the average load since this
  446. * function was last called. In reality, there could have been
  447. * multiple opps since this function was last called and that affects
  448. * the load calculation. While it's not perfectly accurate, this
  449. * simplification is good enough and works. REVISIT this, as more
  450. * complex code may be needed if experiments show that it's not
  451. * accurate enough.
  452. *
  453. * Return: 0 on success, -E* if getting the static power failed.
  454. */
  455. static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
  456. struct thermal_zone_device *tz,
  457. u32 *power)
  458. {
  459. unsigned long freq;
  460. int i = 0, cpu, ret;
  461. u32 static_power, dynamic_power, total_load = 0;
  462. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  463. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  464. u32 *load_cpu = NULL;
  465. freq = cpufreq_quick_get(policy->cpu);
  466. if (trace_thermal_power_cpu_get_power_enabled()) {
  467. u32 ncpus = cpumask_weight(policy->related_cpus);
  468. load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
  469. }
  470. for_each_cpu(cpu, policy->related_cpus) {
  471. u32 load;
  472. if (cpu_online(cpu))
  473. load = get_load(cpufreq_cdev, cpu, i);
  474. else
  475. load = 0;
  476. total_load += load;
  477. if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
  478. load_cpu[i] = load;
  479. i++;
  480. }
  481. cpufreq_cdev->last_load = total_load;
  482. dynamic_power = get_dynamic_power(cpufreq_cdev, freq);
  483. ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
  484. if (ret) {
  485. kfree(load_cpu);
  486. return ret;
  487. }
  488. if (load_cpu) {
  489. trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
  490. load_cpu, i, dynamic_power,
  491. static_power);
  492. kfree(load_cpu);
  493. }
  494. *power = static_power + dynamic_power;
  495. return 0;
  496. }
  497. /**
  498. * cpufreq_state2power() - convert a cpu cdev state to power consumed
  499. * @cdev: &thermal_cooling_device pointer
  500. * @tz: a valid thermal zone device pointer
  501. * @state: cooling device state to be converted
  502. * @power: pointer in which to store the resulting power
  503. *
  504. * Convert cooling device state @state into power consumption in
  505. * milliwatts assuming 100% load. Store the calculated power in
  506. * @power.
  507. *
  508. * Return: 0 on success, -EINVAL if the cooling device state could not
  509. * be converted into a frequency or other -E* if there was an error
  510. * when calculating the static power.
  511. */
  512. static int cpufreq_state2power(struct thermal_cooling_device *cdev,
  513. struct thermal_zone_device *tz,
  514. unsigned long state, u32 *power)
  515. {
  516. unsigned int freq, num_cpus;
  517. u32 static_power, dynamic_power;
  518. int ret;
  519. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  520. /* Request state should be less than max_level */
  521. if (WARN_ON(state > cpufreq_cdev->max_level))
  522. return -EINVAL;
  523. num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
  524. freq = cpufreq_cdev->freq_table[state].frequency;
  525. dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
  526. ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
  527. if (ret)
  528. return ret;
  529. *power = static_power + dynamic_power;
  530. return ret;
  531. }
  532. /**
  533. * cpufreq_power2state() - convert power to a cooling device state
  534. * @cdev: &thermal_cooling_device pointer
  535. * @tz: a valid thermal zone device pointer
  536. * @power: power in milliwatts to be converted
  537. * @state: pointer in which to store the resulting state
  538. *
  539. * Calculate a cooling device state for the cpus described by @cdev
  540. * that would allow them to consume at most @power mW and store it in
  541. * @state. Note that this calculation depends on external factors
  542. * such as the cpu load or the current static power. Calling this
  543. * function with the same power as input can yield different cooling
  544. * device states depending on those external factors.
  545. *
  546. * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
  547. * the calculated frequency could not be converted to a valid state.
  548. * The latter should not happen unless the frequencies available to
  549. * cpufreq have changed since the initialization of the cpu cooling
  550. * device.
  551. */
  552. static int cpufreq_power2state(struct thermal_cooling_device *cdev,
  553. struct thermal_zone_device *tz, u32 power,
  554. unsigned long *state)
  555. {
  556. unsigned int cur_freq, target_freq;
  557. int ret;
  558. s32 dyn_power;
  559. u32 last_load, normalised_power, static_power;
  560. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  561. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  562. cur_freq = cpufreq_quick_get(policy->cpu);
  563. ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power);
  564. if (ret)
  565. return ret;
  566. dyn_power = power - static_power;
  567. dyn_power = dyn_power > 0 ? dyn_power : 0;
  568. last_load = cpufreq_cdev->last_load ?: 1;
  569. normalised_power = (dyn_power * 100) / last_load;
  570. target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
  571. *state = get_level(cpufreq_cdev, target_freq);
  572. trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
  573. power);
  574. return 0;
  575. }
  576. /* Bind cpufreq callbacks to thermal cooling device ops */
  577. static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
  578. .get_max_state = cpufreq_get_max_state,
  579. .get_cur_state = cpufreq_get_cur_state,
  580. .set_cur_state = cpufreq_set_cur_state,
  581. };
  582. static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
  583. .get_max_state = cpufreq_get_max_state,
  584. .get_cur_state = cpufreq_get_cur_state,
  585. .set_cur_state = cpufreq_set_cur_state,
  586. .get_requested_power = cpufreq_get_requested_power,
  587. .state2power = cpufreq_state2power,
  588. .power2state = cpufreq_power2state,
  589. };
  590. /* Notifier for cpufreq policy change */
  591. static struct notifier_block thermal_cpufreq_notifier_block = {
  592. .notifier_call = cpufreq_thermal_notifier,
  593. };
  594. static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  595. unsigned int prev_max)
  596. {
  597. struct cpufreq_frequency_table *pos;
  598. unsigned int max = 0;
  599. cpufreq_for_each_valid_entry(pos, table) {
  600. if (pos->frequency > max && pos->frequency < prev_max)
  601. max = pos->frequency;
  602. }
  603. return max;
  604. }
  605. /**
  606. * __cpufreq_cooling_register - helper function to create cpufreq cooling device
  607. * @np: a valid struct device_node to the cooling device device tree node
  608. * @policy: cpufreq policy
  609. * Normally this should be same as cpufreq policy->related_cpus.
  610. * @capacitance: dynamic power coefficient for these cpus
  611. * @plat_static_func: function to calculate the static power consumed by these
  612. * cpus (optional)
  613. *
  614. * This interface function registers the cpufreq cooling device with the name
  615. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  616. * cooling devices. It also gives the opportunity to link the cooling device
  617. * with a device tree node, in order to bind it via the thermal DT code.
  618. *
  619. * Return: a valid struct thermal_cooling_device pointer on success,
  620. * on failure, it returns a corresponding ERR_PTR().
  621. */
  622. static struct thermal_cooling_device *
  623. __cpufreq_cooling_register(struct device_node *np,
  624. struct cpufreq_policy *policy, u32 capacitance,
  625. get_static_t plat_static_func)
  626. {
  627. struct thermal_cooling_device *cdev;
  628. struct cpufreq_cooling_device *cpufreq_cdev;
  629. char dev_name[THERMAL_NAME_LENGTH];
  630. unsigned int freq, i, num_cpus;
  631. int ret;
  632. struct thermal_cooling_device_ops *cooling_ops;
  633. bool first;
  634. if (IS_ERR_OR_NULL(policy)) {
  635. pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy);
  636. return ERR_PTR(-EINVAL);
  637. }
  638. i = cpufreq_table_count_valid_entries(policy);
  639. if (!i) {
  640. pr_debug("%s: CPUFreq table not found or has no valid entries\n",
  641. __func__);
  642. return ERR_PTR(-ENODEV);
  643. }
  644. cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
  645. if (!cpufreq_cdev)
  646. return ERR_PTR(-ENOMEM);
  647. cpufreq_cdev->policy = policy;
  648. num_cpus = cpumask_weight(policy->related_cpus);
  649. cpufreq_cdev->idle_time = kcalloc(num_cpus,
  650. sizeof(*cpufreq_cdev->idle_time),
  651. GFP_KERNEL);
  652. if (!cpufreq_cdev->idle_time) {
  653. cdev = ERR_PTR(-ENOMEM);
  654. goto free_cdev;
  655. }
  656. /* max_level is an index, not a counter */
  657. cpufreq_cdev->max_level = i - 1;
  658. cpufreq_cdev->freq_table = kmalloc_array(i,
  659. sizeof(*cpufreq_cdev->freq_table),
  660. GFP_KERNEL);
  661. if (!cpufreq_cdev->freq_table) {
  662. cdev = ERR_PTR(-ENOMEM);
  663. goto free_idle_time;
  664. }
  665. ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
  666. if (ret < 0) {
  667. cdev = ERR_PTR(ret);
  668. goto free_table;
  669. }
  670. cpufreq_cdev->id = ret;
  671. snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
  672. cpufreq_cdev->id);
  673. /* Fill freq-table in descending order of frequencies */
  674. for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
  675. freq = find_next_max(policy->freq_table, freq);
  676. cpufreq_cdev->freq_table[i].frequency = freq;
  677. /* Warn for duplicate entries */
  678. if (!freq)
  679. pr_warn("%s: table has duplicate entries\n", __func__);
  680. else
  681. pr_debug("%s: freq:%u KHz\n", __func__, freq);
  682. }
  683. if (capacitance) {
  684. cpufreq_cdev->plat_get_static_power = plat_static_func;
  685. ret = update_freq_table(cpufreq_cdev, capacitance);
  686. if (ret) {
  687. cdev = ERR_PTR(ret);
  688. goto remove_ida;
  689. }
  690. cooling_ops = &cpufreq_power_cooling_ops;
  691. } else {
  692. cooling_ops = &cpufreq_cooling_ops;
  693. }
  694. cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
  695. cooling_ops);
  696. if (IS_ERR(cdev))
  697. goto remove_ida;
  698. cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
  699. cpufreq_cdev->cdev = cdev;
  700. mutex_lock(&cooling_list_lock);
  701. /* Register the notifier for first cpufreq cooling device */
  702. first = list_empty(&cpufreq_cdev_list);
  703. list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
  704. mutex_unlock(&cooling_list_lock);
  705. if (first)
  706. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  707. CPUFREQ_POLICY_NOTIFIER);
  708. return cdev;
  709. remove_ida:
  710. ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
  711. free_table:
  712. kfree(cpufreq_cdev->freq_table);
  713. free_idle_time:
  714. kfree(cpufreq_cdev->idle_time);
  715. free_cdev:
  716. kfree(cpufreq_cdev);
  717. return cdev;
  718. }
  719. /**
  720. * cpufreq_cooling_register - function to create cpufreq cooling device.
  721. * @policy: cpufreq policy
  722. *
  723. * This interface function registers the cpufreq cooling device with the name
  724. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  725. * cooling devices.
  726. *
  727. * Return: a valid struct thermal_cooling_device pointer on success,
  728. * on failure, it returns a corresponding ERR_PTR().
  729. */
  730. struct thermal_cooling_device *
  731. cpufreq_cooling_register(struct cpufreq_policy *policy)
  732. {
  733. return __cpufreq_cooling_register(NULL, policy, 0, NULL);
  734. }
  735. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  736. /**
  737. * of_cpufreq_cooling_register - function to create cpufreq cooling device.
  738. * @np: a valid struct device_node to the cooling device device tree node
  739. * @policy: cpufreq policy
  740. *
  741. * This interface function registers the cpufreq cooling device with the name
  742. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  743. * cooling devices. Using this API, the cpufreq cooling device will be
  744. * linked to the device tree node provided.
  745. *
  746. * Return: a valid struct thermal_cooling_device pointer on success,
  747. * on failure, it returns a corresponding ERR_PTR().
  748. */
  749. struct thermal_cooling_device *
  750. of_cpufreq_cooling_register(struct device_node *np,
  751. struct cpufreq_policy *policy)
  752. {
  753. if (!np)
  754. return ERR_PTR(-EINVAL);
  755. return __cpufreq_cooling_register(np, policy, 0, NULL);
  756. }
  757. EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
  758. /**
  759. * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  760. * @policy: cpufreq policy
  761. * @capacitance: dynamic power coefficient for these cpus
  762. * @plat_static_func: function to calculate the static power consumed by these
  763. * cpus (optional)
  764. *
  765. * This interface function registers the cpufreq cooling device with
  766. * the name "thermal-cpufreq-%x". This api can support multiple
  767. * instances of cpufreq cooling devices. Using this function, the
  768. * cooling device will implement the power extensions by using a
  769. * simple cpu power model. The cpus must have registered their OPPs
  770. * using the OPP library.
  771. *
  772. * An optional @plat_static_func may be provided to calculate the
  773. * static power consumed by these cpus. If the platform's static
  774. * power consumption is unknown or negligible, make it NULL.
  775. *
  776. * Return: a valid struct thermal_cooling_device pointer on success,
  777. * on failure, it returns a corresponding ERR_PTR().
  778. */
  779. struct thermal_cooling_device *
  780. cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance,
  781. get_static_t plat_static_func)
  782. {
  783. return __cpufreq_cooling_register(NULL, policy, capacitance,
  784. plat_static_func);
  785. }
  786. EXPORT_SYMBOL(cpufreq_power_cooling_register);
  787. int cpufreq_platform_cooling_register(void)
  788. {
  789. struct cpufreq_policy *policy;
  790. struct device_node *cpu_node;
  791. int cpu;
  792. for_each_cpu(cpu, cpu_online_mask) {
  793. policy = cpufreq_cpu_get(cpu);
  794. if (!policy) {
  795. pr_err("no policy for cpu%d\n", cpu);
  796. continue;
  797. }
  798. cpu_node = of_cpu_device_node_get(cpumask_first(policy->cpus));
  799. if (!cpu_node) {
  800. pr_err("no cpu node\n");
  801. continue;
  802. }
  803. __cpufreq_cooling_register(cpu_node, policy, 0, NULL);
  804. }
  805. return 0;
  806. }
  807. /**
  808. * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  809. * @np: a valid struct device_node to the cooling device device tree node
  810. * @policy: cpufreq policy
  811. * @capacitance: dynamic power coefficient for these cpus
  812. * @plat_static_func: function to calculate the static power consumed by these
  813. * cpus (optional)
  814. *
  815. * This interface function registers the cpufreq cooling device with
  816. * the name "thermal-cpufreq-%x". This api can support multiple
  817. * instances of cpufreq cooling devices. Using this API, the cpufreq
  818. * cooling device will be linked to the device tree node provided.
  819. * Using this function, the cooling device will implement the power
  820. * extensions by using a simple cpu power model. The cpus must have
  821. * registered their OPPs using the OPP library.
  822. *
  823. * An optional @plat_static_func may be provided to calculate the
  824. * static power consumed by these cpus. If the platform's static
  825. * power consumption is unknown or negligible, make it NULL.
  826. *
  827. * Return: a valid struct thermal_cooling_device pointer on success,
  828. * on failure, it returns a corresponding ERR_PTR().
  829. */
  830. struct thermal_cooling_device *
  831. of_cpufreq_power_cooling_register(struct device_node *np,
  832. struct cpufreq_policy *policy,
  833. u32 capacitance,
  834. get_static_t plat_static_func)
  835. {
  836. if (!np)
  837. return ERR_PTR(-EINVAL);
  838. return __cpufreq_cooling_register(np, policy, capacitance,
  839. plat_static_func);
  840. }
  841. EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
  842. /**
  843. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  844. * @cdev: thermal cooling device pointer.
  845. *
  846. * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  847. */
  848. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  849. {
  850. struct cpufreq_cooling_device *cpufreq_cdev;
  851. bool last;
  852. if (!cdev)
  853. return;
  854. cpufreq_cdev = cdev->devdata;
  855. mutex_lock(&cooling_list_lock);
  856. list_del(&cpufreq_cdev->node);
  857. /* Unregister the notifier for the last cpufreq cooling device */
  858. last = list_empty(&cpufreq_cdev_list);
  859. mutex_unlock(&cooling_list_lock);
  860. if (last)
  861. cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
  862. CPUFREQ_POLICY_NOTIFIER);
  863. thermal_cooling_device_unregister(cpufreq_cdev->cdev);
  864. ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
  865. kfree(cpufreq_cdev->idle_time);
  866. kfree(cpufreq_cdev->freq_table);
  867. kfree(cpufreq_cdev);
  868. }
  869. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
  870. late_initcall(cpufreq_platform_cooling_register)