cpu_cooling.c 32 KB

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