cpu_cooling.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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 <[email protected]>
  6. *
  7. * Copyright (C) 2014 Viresh Kumar <[email protected]>
  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 <linux/sched.h>
  34. #include <linux/of_device.h>
  35. #include <linux/suspend.h>
  36. #include <trace/events/thermal.h>
  37. /*
  38. * Cooling state <-> CPUFreq frequency
  39. *
  40. * Cooling states are translated to frequencies throughout this driver and this
  41. * is the relation between them.
  42. *
  43. * Highest cooling state corresponds to lowest possible frequency.
  44. *
  45. * i.e.
  46. * level 0 --> 1st Max Freq
  47. * level 1 --> 2nd Max Freq
  48. * ...
  49. * leven n --> core isolated
  50. */
  51. /**
  52. * struct power_table - frequency to power conversion
  53. * @frequency: frequency in KHz
  54. * @power: power in mW
  55. *
  56. * This structure is built when the cooling device registers and helps
  57. * in translating frequency to power and viceversa.
  58. */
  59. struct power_table {
  60. u32 frequency;
  61. u32 power;
  62. };
  63. /**
  64. * struct cpufreq_cooling_device - data for cooling device with cpufreq
  65. * @id: unique integer value corresponding to each cpufreq_cooling_device
  66. * registered.
  67. * @cool_dev: thermal_cooling_device pointer to keep track of the
  68. * registered cooling device.
  69. * @cpufreq_state: integer value representing the current state of cpufreq
  70. * cooling devices.
  71. * @clipped_freq: integer value representing the absolute value of the clipped
  72. * frequency.
  73. * @cpufreq_floor_state: integer value representing the frequency floor state
  74. * of cpufreq cooling devices.
  75. * @floor_freq: integer value representing the absolute value of the floor
  76. * frequency.
  77. * @max_level: maximum cooling level. [0..max_level-1: <freq>
  78. * max_level: Core unavailable]
  79. * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
  80. * @node: list_head to link all cpufreq_cooling_device together.
  81. * @last_load: load measured by the latest call to cpufreq_get_requested_power()
  82. * @time_in_idle: previous reading of the absolute time that this cpu was idle
  83. * @time_in_idle_timestamp: wall time of the last invocation of
  84. * get_cpu_idle_time_us()
  85. * @dyn_power_table: array of struct power_table for frequency to power
  86. * conversion, sorted in ascending order.
  87. * @dyn_power_table_entries: number of entries in the @dyn_power_table array
  88. * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
  89. * @plat_get_static_power: callback to calculate the static power
  90. *
  91. * This structure is required for keeping information of each registered
  92. * cpufreq_cooling_device.
  93. */
  94. struct cpufreq_cooling_device {
  95. int id;
  96. struct thermal_cooling_device *cool_dev;
  97. unsigned int cpufreq_state;
  98. unsigned int clipped_freq;
  99. unsigned int cpufreq_floor_state;
  100. unsigned int floor_freq;
  101. unsigned int max_level;
  102. unsigned int *freq_table; /* In descending order */
  103. struct cpumask allowed_cpus;
  104. struct list_head node;
  105. u32 last_load;
  106. u64 *time_in_idle;
  107. u64 *time_in_idle_timestamp;
  108. struct power_table *dyn_power_table;
  109. int dyn_power_table_entries;
  110. struct device *cpu_dev;
  111. get_static_t plat_get_static_power;
  112. struct cpu_cooling_ops *plat_ops;
  113. };
  114. static DEFINE_IDR(cpufreq_idr);
  115. static DEFINE_MUTEX(cooling_cpufreq_lock);
  116. static atomic_t in_suspend;
  117. static unsigned int cpufreq_dev_count;
  118. static int8_t cpuhp_registered;
  119. static struct work_struct cpuhp_register_work;
  120. static struct cpumask cpus_pending_online;
  121. static struct cpumask cpus_isolated_by_thermal;
  122. static DEFINE_MUTEX(core_isolate_lock);
  123. static DEFINE_MUTEX(cooling_list_lock);
  124. static LIST_HEAD(cpufreq_dev_list);
  125. /**
  126. * get_idr - function to get a unique id.
  127. * @idr: struct idr * handle used to create a id.
  128. * @id: int * value generated by this function.
  129. *
  130. * This function will populate @id with an unique
  131. * id, using the idr API.
  132. *
  133. * Return: 0 on success, an error code on failure.
  134. */
  135. static int get_idr(struct idr *idr, int *id)
  136. {
  137. int ret;
  138. mutex_lock(&cooling_cpufreq_lock);
  139. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  140. mutex_unlock(&cooling_cpufreq_lock);
  141. if (unlikely(ret < 0))
  142. return ret;
  143. *id = ret;
  144. return 0;
  145. }
  146. /**
  147. * release_idr - function to free the unique id.
  148. * @idr: struct idr * handle used for creating the id.
  149. * @id: int value representing the unique id.
  150. */
  151. static void release_idr(struct idr *idr, int id)
  152. {
  153. mutex_lock(&cooling_cpufreq_lock);
  154. idr_remove(idr, id);
  155. mutex_unlock(&cooling_cpufreq_lock);
  156. }
  157. /* Below code defines functions to be used for cpufreq as cooling device */
  158. /**
  159. * get_level: Find the level for a particular frequency
  160. * @cpufreq_dev: cpufreq_dev for which the property is required
  161. * @freq: Frequency
  162. *
  163. * Return: level on success, THERMAL_CSTATE_INVALID on error.
  164. */
  165. static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
  166. unsigned int freq)
  167. {
  168. unsigned long level;
  169. for (level = 0; level < cpufreq_dev->max_level; level++) {
  170. if (freq == cpufreq_dev->freq_table[level])
  171. return level;
  172. if (freq > cpufreq_dev->freq_table[level])
  173. break;
  174. }
  175. return THERMAL_CSTATE_INVALID;
  176. }
  177. /**
  178. * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
  179. * @cpu: cpu for which the level is required
  180. * @freq: the frequency of interest
  181. *
  182. * This function will match the cooling level corresponding to the
  183. * requested @freq and return it.
  184. *
  185. * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
  186. * otherwise.
  187. */
  188. unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
  189. {
  190. struct cpufreq_cooling_device *cpufreq_dev;
  191. mutex_lock(&cooling_list_lock);
  192. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  193. if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
  194. unsigned long level = get_level(cpufreq_dev, freq);
  195. mutex_unlock(&cooling_list_lock);
  196. return level;
  197. }
  198. }
  199. mutex_unlock(&cooling_list_lock);
  200. pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
  201. return THERMAL_CSTATE_INVALID;
  202. }
  203. EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
  204. static int cpufreq_cooling_pm_notify(struct notifier_block *nb,
  205. unsigned long mode, void *_unused)
  206. {
  207. struct cpufreq_cooling_device *cpufreq_dev;
  208. unsigned int cpu;
  209. switch (mode) {
  210. case PM_HIBERNATION_PREPARE:
  211. case PM_RESTORE_PREPARE:
  212. case PM_SUSPEND_PREPARE:
  213. atomic_set(&in_suspend, 1);
  214. break;
  215. case PM_POST_HIBERNATION:
  216. case PM_POST_RESTORE:
  217. case PM_POST_SUSPEND:
  218. mutex_lock(&cooling_list_lock);
  219. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  220. mutex_lock(&core_isolate_lock);
  221. if (cpufreq_dev->cpufreq_state ==
  222. cpufreq_dev->max_level) {
  223. cpu = cpumask_any(&cpufreq_dev->allowed_cpus);
  224. /*
  225. * Unlock this lock before calling
  226. * schedule_isolate. as this could lead to
  227. * deadlock with hotplug path.
  228. */
  229. mutex_unlock(&core_isolate_lock);
  230. if (cpu_online(cpu) &&
  231. !cpumask_test_and_set_cpu(cpu,
  232. &cpus_isolated_by_thermal)) {
  233. if (sched_isolate_cpu(cpu))
  234. cpumask_clear_cpu(cpu,
  235. &cpus_isolated_by_thermal);
  236. }
  237. continue;
  238. }
  239. mutex_unlock(&core_isolate_lock);
  240. }
  241. mutex_unlock(&cooling_list_lock);
  242. atomic_set(&in_suspend, 0);
  243. break;
  244. default:
  245. break;
  246. }
  247. return 0;
  248. }
  249. static struct notifier_block cpufreq_cooling_pm_nb = {
  250. .notifier_call = cpufreq_cooling_pm_notify,
  251. };
  252. static int cpufreq_hp_offline(unsigned int offline_cpu)
  253. {
  254. struct cpufreq_cooling_device *cpufreq_dev;
  255. mutex_lock(&cooling_list_lock);
  256. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  257. if (!cpumask_test_cpu(offline_cpu, &cpufreq_dev->allowed_cpus))
  258. continue;
  259. mutex_lock(&core_isolate_lock);
  260. if ((cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) &&
  261. (cpumask_test_and_clear_cpu(offline_cpu,
  262. &cpus_isolated_by_thermal)))
  263. sched_unisolate_cpu_unlocked(offline_cpu);
  264. mutex_unlock(&core_isolate_lock);
  265. break;
  266. }
  267. mutex_unlock(&cooling_list_lock);
  268. return 0;
  269. }
  270. static int cpufreq_hp_online(unsigned int online_cpu)
  271. {
  272. struct cpufreq_cooling_device *cpufreq_dev;
  273. int ret = 0;
  274. if (atomic_read(&in_suspend))
  275. return 0;
  276. mutex_lock(&cooling_list_lock);
  277. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  278. if (!cpumask_test_cpu(online_cpu, &cpufreq_dev->allowed_cpus))
  279. continue;
  280. mutex_lock(&core_isolate_lock);
  281. if (cpufreq_dev->cpufreq_state == cpufreq_dev->max_level) {
  282. cpumask_set_cpu(online_cpu, &cpus_pending_online);
  283. ret = NOTIFY_BAD;
  284. }
  285. mutex_unlock(&core_isolate_lock);
  286. break;
  287. }
  288. mutex_unlock(&cooling_list_lock);
  289. return ret;
  290. }
  291. /**
  292. * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
  293. * @nb: struct notifier_block * with callback info.
  294. * @event: value showing cpufreq event for which this function invoked.
  295. * @data: callback-specific data
  296. *
  297. * Callback to hijack the notification on cpufreq policy transition.
  298. * Every time there is a change in policy, we will intercept and
  299. * update the cpufreq policy with thermal constraints.
  300. *
  301. * Return: 0 (success)
  302. */
  303. static int cpufreq_thermal_notifier(struct notifier_block *nb,
  304. unsigned long event, void *data)
  305. {
  306. struct cpufreq_policy *policy = data;
  307. unsigned long clipped_freq = ULONG_MAX, floor_freq = 0;
  308. struct cpufreq_cooling_device *cpufreq_dev;
  309. if (event != CPUFREQ_ADJUST)
  310. return NOTIFY_DONE;
  311. mutex_lock(&cooling_list_lock);
  312. list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
  313. if (!cpumask_intersects(&cpufreq_dev->allowed_cpus,
  314. policy->related_cpus))
  315. continue;
  316. if (cpufreq_dev->clipped_freq < clipped_freq)
  317. clipped_freq = cpufreq_dev->clipped_freq;
  318. if (cpufreq_dev->floor_freq > floor_freq)
  319. floor_freq = cpufreq_dev->floor_freq;
  320. }
  321. /*
  322. * policy->max is the maximum allowed frequency defined by user
  323. * and clipped_freq is the maximum that thermal constraints
  324. * allow.
  325. *
  326. * If clipped_freq is lower than policy->max, then we need to
  327. * readjust policy->max.
  328. *
  329. * But, if clipped_freq is greater than policy->max, we don't
  330. * need to do anything.
  331. *
  332. * Similarly, if policy minimum set by the user is less than
  333. * the floor_frequency, then adjust the policy->min.
  334. */
  335. if (policy->max > clipped_freq || policy->min < floor_freq)
  336. cpufreq_verify_within_limits(policy, floor_freq, clipped_freq);
  337. mutex_unlock(&cooling_list_lock);
  338. return NOTIFY_OK;
  339. }
  340. /**
  341. * build_dyn_power_table() - create a dynamic power to frequency table
  342. * @cpufreq_device: the cpufreq cooling device in which to store the table
  343. * @capacitance: dynamic power coefficient for these cpus
  344. *
  345. * Build a dynamic power to frequency table for this cpu and store it
  346. * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
  347. * cpu_freq_to_power() to convert between power and frequency
  348. * efficiently. Power is stored in mW, frequency in KHz. The
  349. * resulting table is in ascending order.
  350. *
  351. * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
  352. * -ENOMEM if we run out of memory or -EAGAIN if an OPP was
  353. * added/enabled while the function was executing.
  354. */
  355. static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
  356. u32 capacitance)
  357. {
  358. struct power_table *power_table;
  359. struct dev_pm_opp *opp;
  360. struct device *dev = NULL;
  361. int num_opps = 0, cpu, i, ret = 0;
  362. unsigned long freq;
  363. for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
  364. dev = get_cpu_device(cpu);
  365. if (!dev) {
  366. dev_warn(&cpufreq_device->cool_dev->device,
  367. "No cpu device for cpu %d\n", cpu);
  368. continue;
  369. }
  370. num_opps = dev_pm_opp_get_opp_count(dev);
  371. if (num_opps > 0)
  372. break;
  373. else if (num_opps < 0)
  374. return num_opps;
  375. }
  376. if (num_opps == 0)
  377. return -EINVAL;
  378. power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
  379. if (!power_table)
  380. return -ENOMEM;
  381. rcu_read_lock();
  382. for (freq = 0, i = 0;
  383. opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
  384. freq++, i++) {
  385. u32 freq_mhz, voltage_mv;
  386. u64 power;
  387. if (i >= num_opps) {
  388. rcu_read_unlock();
  389. ret = -EAGAIN;
  390. goto free_power_table;
  391. }
  392. freq_mhz = freq / 1000000;
  393. voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
  394. /*
  395. * Do the multiplication with MHz and millivolt so as
  396. * to not overflow.
  397. */
  398. power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
  399. do_div(power, 1000000000);
  400. /* frequency is stored in power_table in KHz */
  401. power_table[i].frequency = freq / 1000;
  402. /* power is stored in mW */
  403. power_table[i].power = power;
  404. }
  405. rcu_read_unlock();
  406. if (i != num_opps) {
  407. ret = PTR_ERR(opp);
  408. goto free_power_table;
  409. }
  410. cpufreq_device->cpu_dev = dev;
  411. cpufreq_device->dyn_power_table = power_table;
  412. cpufreq_device->dyn_power_table_entries = i;
  413. return 0;
  414. free_power_table:
  415. kfree(power_table);
  416. return ret;
  417. }
  418. static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
  419. u32 freq)
  420. {
  421. int i;
  422. struct power_table *pt = cpufreq_device->dyn_power_table;
  423. for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
  424. if (freq < pt[i].frequency)
  425. break;
  426. return pt[i - 1].power;
  427. }
  428. static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
  429. u32 power)
  430. {
  431. int i;
  432. struct power_table *pt = cpufreq_device->dyn_power_table;
  433. for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
  434. if (power < pt[i].power)
  435. break;
  436. return pt[i - 1].frequency;
  437. }
  438. /**
  439. * get_load() - get load for a cpu since last updated
  440. * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
  441. * @cpu: cpu number
  442. * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus
  443. *
  444. * Return: The average load of cpu @cpu in percentage since this
  445. * function was last called.
  446. */
  447. static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu,
  448. int cpu_idx)
  449. {
  450. u32 load;
  451. u64 now, now_idle, delta_time, delta_idle;
  452. now_idle = get_cpu_idle_time(cpu, &now, 0);
  453. delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx];
  454. delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx];
  455. if (delta_time <= delta_idle)
  456. load = 0;
  457. else
  458. load = div64_u64(100 * (delta_time - delta_idle), delta_time);
  459. cpufreq_device->time_in_idle[cpu_idx] = now_idle;
  460. cpufreq_device->time_in_idle_timestamp[cpu_idx] = now;
  461. return load;
  462. }
  463. /**
  464. * get_static_power() - calculate the static power consumed by the cpus
  465. * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
  466. * @tz: thermal zone device in which we're operating
  467. * @freq: frequency in KHz
  468. * @power: pointer in which to store the calculated static power
  469. *
  470. * Calculate the static power consumed by the cpus described by
  471. * @cpu_actor running at frequency @freq. This function relies on a
  472. * platform specific function that should have been provided when the
  473. * actor was registered. If it wasn't, the static power is assumed to
  474. * be negligible. The calculated static power is stored in @power.
  475. *
  476. * Return: 0 on success, -E* on failure.
  477. */
  478. static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
  479. struct thermal_zone_device *tz, unsigned long freq,
  480. u32 *power)
  481. {
  482. struct dev_pm_opp *opp;
  483. unsigned long voltage;
  484. struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
  485. unsigned long freq_hz = freq * 1000;
  486. if (!cpufreq_device->plat_get_static_power ||
  487. !cpufreq_device->cpu_dev) {
  488. *power = 0;
  489. return 0;
  490. }
  491. rcu_read_lock();
  492. opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
  493. true);
  494. voltage = dev_pm_opp_get_voltage(opp);
  495. rcu_read_unlock();
  496. if (voltage == 0) {
  497. dev_warn_ratelimited(cpufreq_device->cpu_dev,
  498. "Failed to get voltage for frequency %lu: %ld\n",
  499. freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
  500. return -EINVAL;
  501. }
  502. return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
  503. voltage, power);
  504. }
  505. /**
  506. * get_dynamic_power() - calculate the dynamic power
  507. * @cpufreq_device: &cpufreq_cooling_device for this cdev
  508. * @freq: current frequency
  509. *
  510. * Return: the dynamic power consumed by the cpus described by
  511. * @cpufreq_device.
  512. */
  513. static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
  514. unsigned long freq)
  515. {
  516. u32 raw_cpu_power;
  517. raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
  518. return (raw_cpu_power * cpufreq_device->last_load) / 100;
  519. }
  520. /* cpufreq cooling device callback functions are defined below */
  521. /**
  522. * cpufreq_get_max_state - callback function to get the max cooling state.
  523. * @cdev: thermal cooling device pointer.
  524. * @state: fill this variable with the max cooling state.
  525. *
  526. * Callback for the thermal cooling device to return the cpufreq
  527. * max cooling state.
  528. *
  529. * Return: 0 on success, an error code otherwise.
  530. */
  531. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  532. unsigned long *state)
  533. {
  534. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  535. *state = cpufreq_device->max_level;
  536. return 0;
  537. }
  538. /**
  539. * cpufreq_get_min_state - callback function to get the device floor state.
  540. * @cdev: thermal cooling device pointer.
  541. * @state: fill this variable with the cooling device floor.
  542. *
  543. * Callback for the thermal cooling device to return the cpufreq
  544. * floor state.
  545. *
  546. * Return: 0 on success, an error code otherwise.
  547. */
  548. static int cpufreq_get_min_state(struct thermal_cooling_device *cdev,
  549. unsigned long *state)
  550. {
  551. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  552. *state = cpufreq_device->cpufreq_floor_state;
  553. return 0;
  554. }
  555. /**
  556. * cpufreq_set_min_state - callback function to set the device floor state.
  557. * @cdev: thermal cooling device pointer.
  558. * @state: set this variable to the current cooling state.
  559. *
  560. * Callback for the thermal cooling device to change the cpufreq
  561. * floor state.
  562. *
  563. * Return: 0 on success, an error code otherwise.
  564. */
  565. static int cpufreq_set_min_state(struct thermal_cooling_device *cdev,
  566. unsigned long state)
  567. {
  568. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  569. unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
  570. unsigned int floor_freq;
  571. if (state > cpufreq_device->max_level)
  572. state = cpufreq_device->max_level;
  573. if (cpufreq_device->cpufreq_floor_state == state)
  574. return 0;
  575. cpufreq_device->cpufreq_floor_state = state;
  576. /*
  577. * Check if the device has a platform mitigation function that
  578. * can handle the CPU freq mitigation, if not, notify cpufreq
  579. * framework.
  580. */
  581. if (cpufreq_device->plat_ops &&
  582. cpufreq_device->plat_ops->floor_limit) {
  583. /*
  584. * Last level is core isolation so use the frequency
  585. * of previous state.
  586. */
  587. if (state == cpufreq_device->max_level)
  588. state--;
  589. floor_freq = cpufreq_device->freq_table[state];
  590. cpufreq_device->floor_freq = floor_freq;
  591. cpufreq_device->plat_ops->floor_limit(cpu, floor_freq);
  592. } else {
  593. floor_freq = cpufreq_device->freq_table[state];
  594. cpufreq_device->floor_freq = floor_freq;
  595. cpufreq_update_policy(cpu);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * cpufreq_get_cur_state - callback function to get the current cooling state.
  601. * @cdev: thermal cooling device pointer.
  602. * @state: fill this variable with the current cooling state.
  603. *
  604. * Callback for the thermal cooling device to return the cpufreq
  605. * current cooling state.
  606. *
  607. * Return: 0 on success, an error code otherwise.
  608. */
  609. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  610. unsigned long *state)
  611. {
  612. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  613. *state = cpufreq_device->cpufreq_state;
  614. return 0;
  615. }
  616. /**
  617. * cpufreq_set_cur_state - callback function to set the current cooling state.
  618. * @cdev: thermal cooling device pointer.
  619. * @state: set this variable to the current cooling state.
  620. *
  621. * Callback for the thermal cooling device to change the cpufreq
  622. * current cooling state.
  623. *
  624. * Return: 0 on success, an error code otherwise.
  625. */
  626. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  627. unsigned long state)
  628. {
  629. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  630. unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
  631. unsigned int clip_freq;
  632. unsigned long prev_state;
  633. struct device *cpu_dev;
  634. int ret = 0;
  635. /* Request state should be less than max_level */
  636. if (WARN_ON(state > cpufreq_device->max_level))
  637. return -EINVAL;
  638. /* Check if the old cooling action is same as new cooling action */
  639. if (cpufreq_device->cpufreq_state == state)
  640. return 0;
  641. mutex_lock(&core_isolate_lock);
  642. prev_state = cpufreq_device->cpufreq_state;
  643. cpufreq_device->cpufreq_state = state;
  644. mutex_unlock(&core_isolate_lock);
  645. /* If state is the last, isolate the CPU */
  646. if (state == cpufreq_device->max_level) {
  647. if (cpu_online(cpu) &&
  648. (!cpumask_test_and_set_cpu(cpu,
  649. &cpus_isolated_by_thermal))) {
  650. if (sched_isolate_cpu(cpu))
  651. cpumask_clear_cpu(cpu,
  652. &cpus_isolated_by_thermal);
  653. }
  654. return ret;
  655. } else if ((prev_state == cpufreq_device->max_level)
  656. && (state < cpufreq_device->max_level)) {
  657. if (cpumask_test_and_clear_cpu(cpu, &cpus_pending_online)) {
  658. cpu_dev = get_cpu_device(cpu);
  659. ret = device_online(cpu_dev);
  660. if (ret)
  661. pr_err("CPU:%d online error:%d\n", cpu, ret);
  662. goto update_frequency;
  663. } else if (cpumask_test_and_clear_cpu(cpu,
  664. &cpus_isolated_by_thermal)) {
  665. sched_unisolate_cpu(cpu);
  666. }
  667. }
  668. update_frequency:
  669. clip_freq = cpufreq_device->freq_table[state];
  670. cpufreq_device->clipped_freq = clip_freq;
  671. /* Check if the device has a platform mitigation function that
  672. * can handle the CPU freq mitigation, if not, notify cpufreq
  673. * framework.
  674. */
  675. if (cpufreq_device->plat_ops) {
  676. if (cpufreq_device->plat_ops->ceil_limit)
  677. cpufreq_device->plat_ops->ceil_limit(cpu,
  678. clip_freq);
  679. } else {
  680. cpufreq_update_policy(cpu);
  681. }
  682. return 0;
  683. }
  684. /**
  685. * cpufreq_get_requested_power() - get the current power
  686. * @cdev: &thermal_cooling_device pointer
  687. * @tz: a valid thermal zone device pointer
  688. * @power: pointer in which to store the resulting power
  689. *
  690. * Calculate the current power consumption of the cpus in milliwatts
  691. * and store it in @power. This function should actually calculate
  692. * the requested power, but it's hard to get the frequency that
  693. * cpufreq would have assigned if there were no thermal limits.
  694. * Instead, we calculate the current power on the assumption that the
  695. * immediate future will look like the immediate past.
  696. *
  697. * We use the current frequency and the average load since this
  698. * function was last called. In reality, there could have been
  699. * multiple opps since this function was last called and that affects
  700. * the load calculation. While it's not perfectly accurate, this
  701. * simplification is good enough and works. REVISIT this, as more
  702. * complex code may be needed if experiments show that it's not
  703. * accurate enough.
  704. *
  705. * Return: 0 on success, -E* if getting the static power failed.
  706. */
  707. static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
  708. struct thermal_zone_device *tz,
  709. u32 *power)
  710. {
  711. unsigned long freq;
  712. int i = 0, cpu, ret;
  713. u32 static_power, dynamic_power, total_load = 0;
  714. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  715. u32 *load_cpu = NULL;
  716. cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
  717. /*
  718. * All the CPUs are offline, thus the requested power by
  719. * the cdev is 0
  720. */
  721. if (cpu >= nr_cpu_ids) {
  722. *power = 0;
  723. return 0;
  724. }
  725. freq = cpufreq_quick_get(cpu);
  726. if (trace_thermal_power_cpu_get_power_enabled()) {
  727. u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
  728. load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
  729. }
  730. for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
  731. u32 load;
  732. if (cpu_online(cpu))
  733. load = get_load(cpufreq_device, cpu, i);
  734. else
  735. load = 0;
  736. total_load += load;
  737. if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
  738. load_cpu[i] = load;
  739. i++;
  740. }
  741. cpufreq_device->last_load = total_load;
  742. dynamic_power = get_dynamic_power(cpufreq_device, freq);
  743. ret = get_static_power(cpufreq_device, tz, freq, &static_power);
  744. if (ret) {
  745. kfree(load_cpu);
  746. return ret;
  747. }
  748. if (load_cpu) {
  749. trace_thermal_power_cpu_get_power(
  750. &cpufreq_device->allowed_cpus,
  751. freq, load_cpu, i, dynamic_power, static_power);
  752. kfree(load_cpu);
  753. }
  754. *power = static_power + dynamic_power;
  755. return 0;
  756. }
  757. /**
  758. * cpufreq_state2power() - convert a cpu cdev state to power consumed
  759. * @cdev: &thermal_cooling_device pointer
  760. * @tz: a valid thermal zone device pointer
  761. * @state: cooling device state to be converted
  762. * @power: pointer in which to store the resulting power
  763. *
  764. * Convert cooling device state @state into power consumption in
  765. * milliwatts assuming 100% load. Store the calculated power in
  766. * @power.
  767. *
  768. * Return: 0 on success, -EINVAL if the cooling device state could not
  769. * be converted into a frequency or other -E* if there was an error
  770. * when calculating the static power.
  771. */
  772. static int cpufreq_state2power(struct thermal_cooling_device *cdev,
  773. struct thermal_zone_device *tz,
  774. unsigned long state, u32 *power)
  775. {
  776. unsigned int freq, num_cpus;
  777. cpumask_t cpumask;
  778. u32 static_power, dynamic_power;
  779. int ret;
  780. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  781. cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
  782. num_cpus = cpumask_weight(&cpumask);
  783. /* None of our cpus are online, so no power */
  784. if (num_cpus == 0) {
  785. *power = 0;
  786. return 0;
  787. }
  788. freq = cpufreq_device->freq_table[state];
  789. if (!freq)
  790. return -EINVAL;
  791. dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
  792. ret = get_static_power(cpufreq_device, tz, freq, &static_power);
  793. if (ret)
  794. return ret;
  795. *power = static_power + dynamic_power;
  796. return 0;
  797. }
  798. /**
  799. * cpufreq_power2state() - convert power to a cooling device state
  800. * @cdev: &thermal_cooling_device pointer
  801. * @tz: a valid thermal zone device pointer
  802. * @power: power in milliwatts to be converted
  803. * @state: pointer in which to store the resulting state
  804. *
  805. * Calculate a cooling device state for the cpus described by @cdev
  806. * that would allow them to consume at most @power mW and store it in
  807. * @state. Note that this calculation depends on external factors
  808. * such as the cpu load or the current static power. Calling this
  809. * function with the same power as input can yield different cooling
  810. * device states depending on those external factors.
  811. *
  812. * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
  813. * the calculated frequency could not be converted to a valid state.
  814. * The latter should not happen unless the frequencies available to
  815. * cpufreq have changed since the initialization of the cpu cooling
  816. * device.
  817. */
  818. static int cpufreq_power2state(struct thermal_cooling_device *cdev,
  819. struct thermal_zone_device *tz, u32 power,
  820. unsigned long *state)
  821. {
  822. unsigned int cpu, cur_freq, target_freq;
  823. int ret;
  824. s32 dyn_power;
  825. u32 last_load, normalised_power, static_power;
  826. struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
  827. cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
  828. /* None of our cpus are online */
  829. if (cpu >= nr_cpu_ids)
  830. return -ENODEV;
  831. cur_freq = cpufreq_quick_get(cpu);
  832. ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
  833. if (ret)
  834. return ret;
  835. dyn_power = power - static_power;
  836. dyn_power = dyn_power > 0 ? dyn_power : 0;
  837. last_load = cpufreq_device->last_load ?: 1;
  838. normalised_power = (dyn_power * 100) / last_load;
  839. target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
  840. *state = cpufreq_cooling_get_level(cpu, target_freq);
  841. if (*state == THERMAL_CSTATE_INVALID) {
  842. dev_warn_ratelimited(&cdev->device,
  843. "Failed to convert %dKHz for cpu %d into a cdev state\n",
  844. target_freq, cpu);
  845. return -EINVAL;
  846. }
  847. trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
  848. target_freq, *state, power);
  849. return 0;
  850. }
  851. /* Bind cpufreq callbacks to thermal cooling device ops */
  852. static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
  853. .get_max_state = cpufreq_get_max_state,
  854. .get_cur_state = cpufreq_get_cur_state,
  855. .set_cur_state = cpufreq_set_cur_state,
  856. .set_min_state = cpufreq_set_min_state,
  857. .get_min_state = cpufreq_get_min_state,
  858. };
  859. static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
  860. .get_max_state = cpufreq_get_max_state,
  861. .get_cur_state = cpufreq_get_cur_state,
  862. .set_cur_state = cpufreq_set_cur_state,
  863. .get_requested_power = cpufreq_get_requested_power,
  864. .state2power = cpufreq_state2power,
  865. .power2state = cpufreq_power2state,
  866. };
  867. /* Notifier for cpufreq policy change */
  868. static struct notifier_block thermal_cpufreq_notifier_block = {
  869. .notifier_call = cpufreq_thermal_notifier,
  870. };
  871. static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  872. unsigned int prev_max)
  873. {
  874. struct cpufreq_frequency_table *pos;
  875. unsigned int max = 0;
  876. cpufreq_for_each_valid_entry(pos, table) {
  877. if (pos->frequency > max && pos->frequency < prev_max)
  878. max = pos->frequency;
  879. }
  880. return max;
  881. }
  882. static void register_cdev(struct work_struct *work)
  883. {
  884. int ret = 0;
  885. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  886. "cpu_cooling/no-sched", cpufreq_hp_online,
  887. cpufreq_hp_offline);
  888. if (ret < 0)
  889. pr_err("Error registering for hotpug callback:%d\n", ret);
  890. }
  891. /**
  892. * __cpufreq_cooling_register - helper function to create cpufreq cooling device
  893. * @np: a valid struct device_node to the cooling device device tree node
  894. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  895. * Normally this should be same as cpufreq policy->related_cpus.
  896. * @capacitance: dynamic power coefficient for these cpus
  897. * @plat_static_func: function to calculate the static power consumed by these
  898. * cpus (optional)
  899. * @plat_mitig_func: function that does the mitigation by changing the
  900. * frequencies (Optional). By default, cpufreq framweork will
  901. * be notified of the new limits.
  902. *
  903. * This interface function registers the cpufreq cooling device with the name
  904. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  905. * cooling devices. It also gives the opportunity to link the cooling device
  906. * with a device tree node, in order to bind it via the thermal DT code.
  907. *
  908. * Return: a valid struct thermal_cooling_device pointer on success,
  909. * on failure, it returns a corresponding ERR_PTR().
  910. */
  911. static struct thermal_cooling_device *
  912. __cpufreq_cooling_register(struct device_node *np,
  913. const struct cpumask *clip_cpus, u32 capacitance,
  914. get_static_t plat_static_func,
  915. struct cpu_cooling_ops *plat_ops)
  916. {
  917. struct cpufreq_policy *policy;
  918. struct thermal_cooling_device *cool_dev;
  919. struct cpufreq_cooling_device *cpufreq_dev;
  920. char dev_name[THERMAL_NAME_LENGTH];
  921. struct cpufreq_frequency_table *pos, *table;
  922. unsigned int freq, i, num_cpus;
  923. int ret;
  924. struct thermal_cooling_device_ops *cooling_ops;
  925. policy = cpufreq_cpu_get(cpumask_first(clip_cpus));
  926. if (!policy) {
  927. pr_debug("%s: CPUFreq policy not found\n", __func__);
  928. return ERR_PTR(-EPROBE_DEFER);
  929. }
  930. table = policy->freq_table;
  931. if (!table) {
  932. pr_debug("%s: CPUFreq table not found\n", __func__);
  933. cool_dev = ERR_PTR(-ENODEV);
  934. goto put_policy;
  935. }
  936. cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
  937. if (!cpufreq_dev) {
  938. cool_dev = ERR_PTR(-ENOMEM);
  939. goto put_policy;
  940. }
  941. num_cpus = cpumask_weight(clip_cpus);
  942. cpufreq_dev->time_in_idle = kcalloc(num_cpus,
  943. sizeof(*cpufreq_dev->time_in_idle),
  944. GFP_KERNEL);
  945. if (!cpufreq_dev->time_in_idle) {
  946. cool_dev = ERR_PTR(-ENOMEM);
  947. goto free_cdev;
  948. }
  949. cpufreq_dev->time_in_idle_timestamp =
  950. kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
  951. GFP_KERNEL);
  952. if (!cpufreq_dev->time_in_idle_timestamp) {
  953. cool_dev = ERR_PTR(-ENOMEM);
  954. goto free_time_in_idle;
  955. }
  956. /* Find max levels */
  957. cpufreq_for_each_valid_entry(pos, table)
  958. cpufreq_dev->max_level++;
  959. /* Last level will indicate the core will be isolated. */
  960. cpufreq_dev->max_level++;
  961. cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) *
  962. cpufreq_dev->max_level, GFP_KERNEL);
  963. if (!cpufreq_dev->freq_table) {
  964. cool_dev = ERR_PTR(-ENOMEM);
  965. goto free_time_in_idle_timestamp;
  966. }
  967. /* max_level is an index, not a counter */
  968. cpufreq_dev->max_level--;
  969. cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
  970. if (capacitance) {
  971. cpufreq_dev->plat_get_static_power = plat_static_func;
  972. ret = build_dyn_power_table(cpufreq_dev, capacitance);
  973. if (ret) {
  974. cool_dev = ERR_PTR(ret);
  975. goto free_table;
  976. }
  977. cooling_ops = &cpufreq_power_cooling_ops;
  978. } else {
  979. cooling_ops = &cpufreq_cooling_ops;
  980. }
  981. cpufreq_dev->plat_ops = plat_ops;
  982. ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
  983. if (ret) {
  984. cool_dev = ERR_PTR(ret);
  985. goto free_power_table;
  986. }
  987. /* Fill freq-table in descending order of frequencies */
  988. for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) {
  989. freq = find_next_max(table, freq);
  990. cpufreq_dev->freq_table[i] = freq;
  991. /* Warn for duplicate entries */
  992. if (!freq)
  993. pr_warn("%s: table has duplicate entries\n", __func__);
  994. else
  995. pr_debug("%s: freq:%u KHz\n", __func__, freq);
  996. }
  997. snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
  998. cpufreq_dev->id);
  999. cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
  1000. cooling_ops);
  1001. if (IS_ERR(cool_dev))
  1002. goto remove_idr;
  1003. cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
  1004. cpufreq_dev->floor_freq =
  1005. cpufreq_dev->freq_table[cpufreq_dev->max_level];
  1006. cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level;
  1007. cpufreq_dev->cool_dev = cool_dev;
  1008. mutex_lock(&cooling_cpufreq_lock);
  1009. mutex_lock(&cooling_list_lock);
  1010. list_add(&cpufreq_dev->node, &cpufreq_dev_list);
  1011. mutex_unlock(&cooling_list_lock);
  1012. /* Register the notifier for first cpufreq cooling device */
  1013. if (!cpufreq_dev_count++ && !cpufreq_dev->plat_ops)
  1014. cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
  1015. CPUFREQ_POLICY_NOTIFIER);
  1016. if (!cpuhp_registered) {
  1017. cpuhp_registered = 1;
  1018. register_pm_notifier(&cpufreq_cooling_pm_nb);
  1019. cpumask_clear(&cpus_pending_online);
  1020. cpumask_clear(&cpus_isolated_by_thermal);
  1021. INIT_WORK(&cpuhp_register_work, register_cdev);
  1022. queue_work(system_wq, &cpuhp_register_work);
  1023. }
  1024. mutex_unlock(&cooling_cpufreq_lock);
  1025. goto put_policy;
  1026. remove_idr:
  1027. release_idr(&cpufreq_idr, cpufreq_dev->id);
  1028. free_power_table:
  1029. kfree(cpufreq_dev->dyn_power_table);
  1030. free_table:
  1031. kfree(cpufreq_dev->freq_table);
  1032. free_time_in_idle_timestamp:
  1033. kfree(cpufreq_dev->time_in_idle_timestamp);
  1034. free_time_in_idle:
  1035. kfree(cpufreq_dev->time_in_idle);
  1036. free_cdev:
  1037. kfree(cpufreq_dev);
  1038. put_policy:
  1039. cpufreq_cpu_put(policy);
  1040. return cool_dev;
  1041. }
  1042. /**
  1043. * cpufreq_cooling_register - function to create cpufreq cooling device.
  1044. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  1045. *
  1046. * This interface function registers the cpufreq cooling device with the name
  1047. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  1048. * cooling devices.
  1049. *
  1050. * Return: a valid struct thermal_cooling_device pointer on success,
  1051. * on failure, it returns a corresponding ERR_PTR().
  1052. */
  1053. struct thermal_cooling_device *
  1054. cpufreq_cooling_register(const struct cpumask *clip_cpus)
  1055. {
  1056. return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL);
  1057. }
  1058. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  1059. /**
  1060. * of_cpufreq_cooling_register - function to create cpufreq cooling device.
  1061. * @np: a valid struct device_node to the cooling device device tree node
  1062. * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
  1063. *
  1064. * This interface function registers the cpufreq cooling device with the name
  1065. * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
  1066. * cooling devices. Using this API, the cpufreq cooling device will be
  1067. * linked to the device tree node provided.
  1068. *
  1069. * Return: a valid struct thermal_cooling_device pointer on success,
  1070. * on failure, it returns a corresponding ERR_PTR().
  1071. */
  1072. struct thermal_cooling_device *
  1073. of_cpufreq_cooling_register(struct device_node *np,
  1074. const struct cpumask *clip_cpus)
  1075. {
  1076. if (!np)
  1077. return ERR_PTR(-EINVAL);
  1078. return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL);
  1079. }
  1080. EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
  1081. /**
  1082. * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  1083. * @clip_cpus: cpumask of cpus where the frequency constraints will happen
  1084. * @capacitance: dynamic power coefficient for these cpus
  1085. * @plat_static_func: function to calculate the static power consumed by these
  1086. * cpus (optional)
  1087. *
  1088. * This interface function registers the cpufreq cooling device with
  1089. * the name "thermal-cpufreq-%x". This api can support multiple
  1090. * instances of cpufreq cooling devices. Using this function, the
  1091. * cooling device will implement the power extensions by using a
  1092. * simple cpu power model. The cpus must have registered their OPPs
  1093. * using the OPP library.
  1094. *
  1095. * An optional @plat_static_func may be provided to calculate the
  1096. * static power consumed by these cpus. If the platform's static
  1097. * power consumption is unknown or negligible, make it NULL.
  1098. *
  1099. * Return: a valid struct thermal_cooling_device pointer on success,
  1100. * on failure, it returns a corresponding ERR_PTR().
  1101. */
  1102. struct thermal_cooling_device *
  1103. cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
  1104. get_static_t plat_static_func)
  1105. {
  1106. return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
  1107. plat_static_func, NULL);
  1108. }
  1109. EXPORT_SYMBOL(cpufreq_power_cooling_register);
  1110. /**
  1111. * cpufreq_platform_cooling_register() - create cpufreq cooling device with
  1112. * additional platform specific mitigation function.
  1113. *
  1114. * @clip_cpus: cpumask of cpus where the frequency constraints will happen
  1115. * @plat_ops: the platform mitigation functions that will be called insted of
  1116. * cpufreq, if provided.
  1117. *
  1118. * Return: a valid struct thermal_cooling_device pointer on success,
  1119. * on failure, it returns a corresponding ERR_PTR().
  1120. */
  1121. struct thermal_cooling_device *
  1122. cpufreq_platform_cooling_register(const struct cpumask *clip_cpus,
  1123. struct cpu_cooling_ops *plat_ops)
  1124. {
  1125. struct device_node *cpu_node;
  1126. cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus));
  1127. return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL,
  1128. plat_ops);
  1129. }
  1130. EXPORT_SYMBOL(cpufreq_platform_cooling_register);
  1131. /**
  1132. * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
  1133. * @np: a valid struct device_node to the cooling device device tree node
  1134. * @clip_cpus: cpumask of cpus where the frequency constraints will happen
  1135. * @capacitance: dynamic power coefficient for these cpus
  1136. * @plat_static_func: function to calculate the static power consumed by these
  1137. * cpus (optional)
  1138. *
  1139. * This interface function registers the cpufreq cooling device with
  1140. * the name "thermal-cpufreq-%x". This api can support multiple
  1141. * instances of cpufreq cooling devices. Using this API, the cpufreq
  1142. * cooling device will be linked to the device tree node provided.
  1143. * Using this function, the cooling device will implement the power
  1144. * extensions by using a simple cpu power model. The cpus must have
  1145. * registered their OPPs using the OPP library.
  1146. *
  1147. * An optional @plat_static_func may be provided to calculate the
  1148. * static power consumed by these cpus. If the platform's static
  1149. * power consumption is unknown or negligible, make it NULL.
  1150. *
  1151. * Return: a valid struct thermal_cooling_device pointer on success,
  1152. * on failure, it returns a corresponding ERR_PTR().
  1153. */
  1154. struct thermal_cooling_device *
  1155. of_cpufreq_power_cooling_register(struct device_node *np,
  1156. const struct cpumask *clip_cpus,
  1157. u32 capacitance,
  1158. get_static_t plat_static_func)
  1159. {
  1160. if (!np)
  1161. return ERR_PTR(-EINVAL);
  1162. return __cpufreq_cooling_register(np, clip_cpus, capacitance,
  1163. plat_static_func, NULL);
  1164. }
  1165. EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
  1166. /**
  1167. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  1168. * @cdev: thermal cooling device pointer.
  1169. *
  1170. * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  1171. */
  1172. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  1173. {
  1174. struct cpufreq_cooling_device *cpufreq_dev;
  1175. if (!cdev)
  1176. return;
  1177. cpufreq_dev = cdev->devdata;
  1178. /* Unregister the notifier for the last cpufreq cooling device */
  1179. mutex_lock(&cooling_cpufreq_lock);
  1180. if (!--cpufreq_dev_count) {
  1181. unregister_pm_notifier(&cpufreq_cooling_pm_nb);
  1182. if (!cpufreq_dev->plat_ops)
  1183. cpufreq_unregister_notifier(
  1184. &thermal_cpufreq_notifier_block,
  1185. CPUFREQ_POLICY_NOTIFIER);
  1186. }
  1187. mutex_lock(&cooling_list_lock);
  1188. list_del(&cpufreq_dev->node);
  1189. mutex_unlock(&cooling_list_lock);
  1190. mutex_unlock(&cooling_cpufreq_lock);
  1191. thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
  1192. release_idr(&cpufreq_idr, cpufreq_dev->id);
  1193. kfree(cpufreq_dev->dyn_power_table);
  1194. kfree(cpufreq_dev->time_in_idle_timestamp);
  1195. kfree(cpufreq_dev->time_in_idle);
  1196. kfree(cpufreq_dev->freq_table);
  1197. kfree(cpufreq_dev);
  1198. }
  1199. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);