cpufreq_cooling.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/thermal/cpufreq_cooling.c
  4. *
  5. * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
  6. *
  7. * Copyright (C) 2012-2018 Linaro Limited.
  8. *
  9. * Authors: Amit Daniel <amit.kachhap@linaro.org>
  10. * Viresh Kumar <viresh.kumar@linaro.org>
  11. *
  12. */
  13. #include <linux/cpu.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/cpu_cooling.h>
  16. #include <linux/device.h>
  17. #include <linux/energy_model.h>
  18. #include <linux/err.h>
  19. #include <linux/export.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/pm_qos.h>
  22. #include <linux/slab.h>
  23. #include <linux/thermal.h>
  24. #include <linux/units.h>
  25. #include "thermal_trace.h"
  26. /*
  27. * Cooling state <-> CPUFreq frequency
  28. *
  29. * Cooling states are translated to frequencies throughout this driver and this
  30. * is the relation between them.
  31. *
  32. * Highest cooling state corresponds to lowest possible frequency.
  33. *
  34. * i.e.
  35. * level 0 --> 1st Max Freq
  36. * level 1 --> 2nd Max Freq
  37. * ...
  38. */
  39. /**
  40. * struct time_in_idle - Idle time stats
  41. * @time: previous reading of the absolute time that this cpu was idle
  42. * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
  43. */
  44. struct time_in_idle {
  45. u64 time;
  46. u64 timestamp;
  47. };
  48. /**
  49. * struct cpufreq_cooling_device - data for cooling device with cpufreq
  50. * @last_load: load measured by the latest call to cpufreq_get_requested_power()
  51. * @cpufreq_state: integer value representing the current state of cpufreq
  52. * cooling devices.
  53. * @max_level: maximum cooling level. One less than total number of valid
  54. * cpufreq frequencies.
  55. * @em: Reference on the Energy Model of the device
  56. * @policy: cpufreq policy.
  57. * @cooling_ops: cpufreq callbacks to thermal cooling device ops
  58. * @idle_time: idle time stats
  59. * @qos_req: PM QoS contraint to apply
  60. *
  61. * This structure is required for keeping information of each registered
  62. * cpufreq_cooling_device.
  63. */
  64. struct cpufreq_cooling_device {
  65. u32 last_load;
  66. unsigned int cpufreq_state;
  67. unsigned int max_level;
  68. struct em_perf_domain *em;
  69. struct cpufreq_policy *policy;
  70. struct thermal_cooling_device_ops cooling_ops;
  71. #ifndef CONFIG_SMP
  72. struct time_in_idle *idle_time;
  73. #endif
  74. struct freq_qos_request qos_req;
  75. };
  76. #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
  77. /**
  78. * get_level: Find the level for a particular frequency
  79. * @cpufreq_cdev: cpufreq_cdev for which the property is required
  80. * @freq: Frequency
  81. *
  82. * Return: level corresponding to the frequency.
  83. */
  84. static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
  85. unsigned int freq)
  86. {
  87. struct em_perf_state *table;
  88. int i;
  89. rcu_read_lock();
  90. table = em_perf_state_from_pd(cpufreq_cdev->em);
  91. for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
  92. if (freq > table[i].frequency)
  93. break;
  94. }
  95. rcu_read_unlock();
  96. return cpufreq_cdev->max_level - i - 1;
  97. }
  98. static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
  99. u32 freq)
  100. {
  101. struct em_perf_state *table;
  102. unsigned long power_mw;
  103. int i;
  104. rcu_read_lock();
  105. table = em_perf_state_from_pd(cpufreq_cdev->em);
  106. for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
  107. if (freq > table[i].frequency)
  108. break;
  109. }
  110. power_mw = table[i + 1].power;
  111. power_mw /= MICROWATT_PER_MILLIWATT;
  112. rcu_read_unlock();
  113. return power_mw;
  114. }
  115. static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
  116. u32 power)
  117. {
  118. struct em_perf_state *table;
  119. unsigned long em_power_mw;
  120. u32 freq;
  121. int i;
  122. rcu_read_lock();
  123. table = em_perf_state_from_pd(cpufreq_cdev->em);
  124. for (i = cpufreq_cdev->max_level; i > 0; i--) {
  125. /* Convert EM power to milli-Watts to make safe comparison */
  126. em_power_mw = table[i].power;
  127. em_power_mw /= MICROWATT_PER_MILLIWATT;
  128. if (power >= em_power_mw)
  129. break;
  130. }
  131. freq = table[i].frequency;
  132. rcu_read_unlock();
  133. return freq;
  134. }
  135. /**
  136. * get_load() - get load for a cpu
  137. * @cpufreq_cdev: struct cpufreq_cooling_device for the cpu
  138. * @cpu: cpu number
  139. * @cpu_idx: index of the cpu in time_in_idle array
  140. *
  141. * Return: The average load of cpu @cpu in percentage since this
  142. * function was last called.
  143. */
  144. #ifdef CONFIG_SMP
  145. static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
  146. int cpu_idx)
  147. {
  148. unsigned long util = sched_cpu_util(cpu);
  149. return (util * 100) / arch_scale_cpu_capacity(cpu);
  150. }
  151. #else /* !CONFIG_SMP */
  152. static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
  153. int cpu_idx)
  154. {
  155. u32 load;
  156. u64 now, now_idle, delta_time, delta_idle;
  157. struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
  158. now_idle = get_cpu_idle_time(cpu, &now, 0);
  159. delta_idle = now_idle - idle_time->time;
  160. delta_time = now - idle_time->timestamp;
  161. if (delta_time <= delta_idle)
  162. load = 0;
  163. else
  164. load = div64_u64(100 * (delta_time - delta_idle), delta_time);
  165. idle_time->time = now_idle;
  166. idle_time->timestamp = now;
  167. return load;
  168. }
  169. #endif /* CONFIG_SMP */
  170. /**
  171. * get_dynamic_power() - calculate the dynamic power
  172. * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
  173. * @freq: current frequency
  174. *
  175. * Return: the dynamic power consumed by the cpus described by
  176. * @cpufreq_cdev.
  177. */
  178. static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
  179. unsigned long freq)
  180. {
  181. u32 raw_cpu_power;
  182. raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
  183. return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
  184. }
  185. /**
  186. * cpufreq_get_requested_power() - get the current power
  187. * @cdev: &thermal_cooling_device pointer
  188. * @power: pointer in which to store the resulting power
  189. *
  190. * Calculate the current power consumption of the cpus in milliwatts
  191. * and store it in @power. This function should actually calculate
  192. * the requested power, but it's hard to get the frequency that
  193. * cpufreq would have assigned if there were no thermal limits.
  194. * Instead, we calculate the current power on the assumption that the
  195. * immediate future will look like the immediate past.
  196. *
  197. * We use the current frequency and the average load since this
  198. * function was last called. In reality, there could have been
  199. * multiple opps since this function was last called and that affects
  200. * the load calculation. While it's not perfectly accurate, this
  201. * simplification is good enough and works. REVISIT this, as more
  202. * complex code may be needed if experiments show that it's not
  203. * accurate enough.
  204. *
  205. * Return: 0 on success, this function doesn't fail.
  206. */
  207. static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
  208. u32 *power)
  209. {
  210. unsigned long freq;
  211. int i = 0, cpu;
  212. u32 total_load = 0;
  213. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  214. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  215. freq = cpufreq_quick_get(policy->cpu);
  216. for_each_cpu(cpu, policy->related_cpus) {
  217. u32 load;
  218. if (cpu_online(cpu))
  219. load = get_load(cpufreq_cdev, cpu, i);
  220. else
  221. load = 0;
  222. total_load += load;
  223. }
  224. cpufreq_cdev->last_load = total_load;
  225. *power = get_dynamic_power(cpufreq_cdev, freq);
  226. trace_thermal_power_cpu_get_power_simple(policy->cpu, *power);
  227. return 0;
  228. }
  229. /**
  230. * cpufreq_state2power() - convert a cpu cdev state to power consumed
  231. * @cdev: &thermal_cooling_device pointer
  232. * @state: cooling device state to be converted
  233. * @power: pointer in which to store the resulting power
  234. *
  235. * Convert cooling device state @state into power consumption in
  236. * milliwatts assuming 100% load. Store the calculated power in
  237. * @power.
  238. *
  239. * Return: 0 on success, -EINVAL if the cooling device state is bigger
  240. * than maximum allowed.
  241. */
  242. static int cpufreq_state2power(struct thermal_cooling_device *cdev,
  243. unsigned long state, u32 *power)
  244. {
  245. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  246. unsigned int freq, num_cpus, idx;
  247. struct em_perf_state *table;
  248. /* Request state should be less than max_level */
  249. if (state > cpufreq_cdev->max_level)
  250. return -EINVAL;
  251. num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
  252. idx = cpufreq_cdev->max_level - state;
  253. rcu_read_lock();
  254. table = em_perf_state_from_pd(cpufreq_cdev->em);
  255. freq = table[idx].frequency;
  256. rcu_read_unlock();
  257. *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
  258. return 0;
  259. }
  260. /**
  261. * cpufreq_power2state() - convert power to a cooling device state
  262. * @cdev: &thermal_cooling_device pointer
  263. * @power: power in milliwatts to be converted
  264. * @state: pointer in which to store the resulting state
  265. *
  266. * Calculate a cooling device state for the cpus described by @cdev
  267. * that would allow them to consume at most @power mW and store it in
  268. * @state. Note that this calculation depends on external factors
  269. * such as the CPUs load. Calling this function with the same power
  270. * as input can yield different cooling device states depending on those
  271. * external factors.
  272. *
  273. * Return: 0 on success, this function doesn't fail.
  274. */
  275. static int cpufreq_power2state(struct thermal_cooling_device *cdev,
  276. u32 power, unsigned long *state)
  277. {
  278. unsigned int target_freq;
  279. u32 last_load, normalised_power;
  280. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  281. struct cpufreq_policy *policy = cpufreq_cdev->policy;
  282. last_load = cpufreq_cdev->last_load ?: 1;
  283. normalised_power = (power * 100) / last_load;
  284. target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
  285. *state = get_level(cpufreq_cdev, target_freq);
  286. trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
  287. power);
  288. return 0;
  289. }
  290. static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
  291. struct em_perf_domain *em) {
  292. struct cpufreq_policy *policy;
  293. unsigned int nr_levels;
  294. if (!em || em_is_artificial(em))
  295. return false;
  296. policy = cpufreq_cdev->policy;
  297. if (!cpumask_equal(policy->related_cpus, em_span_cpus(em))) {
  298. pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
  299. cpumask_pr_args(em_span_cpus(em)),
  300. cpumask_pr_args(policy->related_cpus));
  301. return false;
  302. }
  303. nr_levels = cpufreq_cdev->max_level + 1;
  304. if (em_pd_nr_perf_states(em) != nr_levels) {
  305. pr_err("The number of performance states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
  306. cpumask_pr_args(em_span_cpus(em)),
  307. em_pd_nr_perf_states(em), nr_levels);
  308. return false;
  309. }
  310. return true;
  311. }
  312. #endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
  313. #ifdef CONFIG_SMP
  314. static inline int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
  315. {
  316. return 0;
  317. }
  318. static inline void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
  319. {
  320. }
  321. #else
  322. static int allocate_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
  323. {
  324. unsigned int num_cpus = cpumask_weight(cpufreq_cdev->policy->related_cpus);
  325. cpufreq_cdev->idle_time = kcalloc(num_cpus,
  326. sizeof(*cpufreq_cdev->idle_time),
  327. GFP_KERNEL);
  328. if (!cpufreq_cdev->idle_time)
  329. return -ENOMEM;
  330. return 0;
  331. }
  332. static void free_idle_time(struct cpufreq_cooling_device *cpufreq_cdev)
  333. {
  334. kfree(cpufreq_cdev->idle_time);
  335. cpufreq_cdev->idle_time = NULL;
  336. }
  337. #endif /* CONFIG_SMP */
  338. static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
  339. unsigned long state)
  340. {
  341. struct cpufreq_policy *policy;
  342. unsigned long idx;
  343. #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
  344. /* Use the Energy Model table if available */
  345. if (cpufreq_cdev->em) {
  346. struct em_perf_state *table;
  347. unsigned int freq;
  348. idx = cpufreq_cdev->max_level - state;
  349. rcu_read_lock();
  350. table = em_perf_state_from_pd(cpufreq_cdev->em);
  351. freq = table[idx].frequency;
  352. rcu_read_unlock();
  353. return freq;
  354. }
  355. #endif
  356. /* Otherwise, fallback on the CPUFreq table */
  357. policy = cpufreq_cdev->policy;
  358. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  359. idx = cpufreq_cdev->max_level - state;
  360. else
  361. idx = state;
  362. return policy->freq_table[idx].frequency;
  363. }
  364. /* cpufreq cooling device callback functions are defined below */
  365. /**
  366. * cpufreq_get_max_state - callback function to get the max cooling state.
  367. * @cdev: thermal cooling device pointer.
  368. * @state: fill this variable with the max cooling state.
  369. *
  370. * Callback for the thermal cooling device to return the cpufreq
  371. * max cooling state.
  372. *
  373. * Return: 0 on success, this function doesn't fail.
  374. */
  375. static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  376. unsigned long *state)
  377. {
  378. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  379. *state = cpufreq_cdev->max_level;
  380. return 0;
  381. }
  382. /**
  383. * cpufreq_get_cur_state - callback function to get the current cooling state.
  384. * @cdev: thermal cooling device pointer.
  385. * @state: fill this variable with the current cooling state.
  386. *
  387. * Callback for the thermal cooling device to return the cpufreq
  388. * current cooling state.
  389. *
  390. * Return: 0 on success, this function doesn't fail.
  391. */
  392. static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  393. unsigned long *state)
  394. {
  395. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  396. *state = cpufreq_cdev->cpufreq_state;
  397. return 0;
  398. }
  399. /**
  400. * cpufreq_set_cur_state - callback function to set the current cooling state.
  401. * @cdev: thermal cooling device pointer.
  402. * @state: set this variable to the current cooling state.
  403. *
  404. * Callback for the thermal cooling device to change the cpufreq
  405. * current cooling state.
  406. *
  407. * Return: 0 on success, an error code otherwise.
  408. */
  409. static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
  410. unsigned long state)
  411. {
  412. struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
  413. unsigned int frequency;
  414. int ret;
  415. /* Request state should be less than max_level */
  416. if (state > cpufreq_cdev->max_level)
  417. return -EINVAL;
  418. /* Check if the old cooling action is same as new cooling action */
  419. if (cpufreq_cdev->cpufreq_state == state)
  420. return 0;
  421. frequency = get_state_freq(cpufreq_cdev, state);
  422. ret = freq_qos_update_request(&cpufreq_cdev->qos_req, frequency);
  423. if (ret >= 0) {
  424. cpufreq_cdev->cpufreq_state = state;
  425. ret = 0;
  426. }
  427. return ret;
  428. }
  429. /**
  430. * __cpufreq_cooling_register - helper function to create cpufreq cooling device
  431. * @np: a valid struct device_node to the cooling device tree node
  432. * @policy: cpufreq policy
  433. * Normally this should be same as cpufreq policy->related_cpus.
  434. * @em: Energy Model of the cpufreq policy
  435. *
  436. * This interface function registers the cpufreq cooling device with the name
  437. * "cpufreq-%s". This API can support multiple instances of cpufreq
  438. * cooling devices. It also gives the opportunity to link the cooling device
  439. * with a device tree node, in order to bind it via the thermal DT code.
  440. *
  441. * Return: a valid struct thermal_cooling_device pointer on success,
  442. * on failure, it returns a corresponding ERR_PTR().
  443. */
  444. static struct thermal_cooling_device *
  445. __cpufreq_cooling_register(struct device_node *np,
  446. struct cpufreq_policy *policy,
  447. struct em_perf_domain *em)
  448. {
  449. struct thermal_cooling_device *cdev;
  450. struct cpufreq_cooling_device *cpufreq_cdev;
  451. unsigned int i;
  452. struct device *dev;
  453. int ret;
  454. struct thermal_cooling_device_ops *cooling_ops;
  455. char *name;
  456. if (IS_ERR_OR_NULL(policy)) {
  457. pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
  458. return ERR_PTR(-EINVAL);
  459. }
  460. dev = get_cpu_device(policy->cpu);
  461. if (unlikely(!dev)) {
  462. pr_warn("No cpu device for cpu %d\n", policy->cpu);
  463. return ERR_PTR(-ENODEV);
  464. }
  465. i = cpufreq_table_count_valid_entries(policy);
  466. if (!i) {
  467. pr_debug("%s: CPUFreq table not found or has no valid entries\n",
  468. __func__);
  469. return ERR_PTR(-ENODEV);
  470. }
  471. cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
  472. if (!cpufreq_cdev)
  473. return ERR_PTR(-ENOMEM);
  474. cpufreq_cdev->policy = policy;
  475. ret = allocate_idle_time(cpufreq_cdev);
  476. if (ret) {
  477. cdev = ERR_PTR(ret);
  478. goto free_cdev;
  479. }
  480. /* max_level is an index, not a counter */
  481. cpufreq_cdev->max_level = i - 1;
  482. cooling_ops = &cpufreq_cdev->cooling_ops;
  483. cooling_ops->get_max_state = cpufreq_get_max_state;
  484. cooling_ops->get_cur_state = cpufreq_get_cur_state;
  485. cooling_ops->set_cur_state = cpufreq_set_cur_state;
  486. #ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
  487. if (em_is_sane(cpufreq_cdev, em)) {
  488. cpufreq_cdev->em = em;
  489. cooling_ops->get_requested_power = cpufreq_get_requested_power;
  490. cooling_ops->state2power = cpufreq_state2power;
  491. cooling_ops->power2state = cpufreq_power2state;
  492. } else
  493. #endif
  494. if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED) {
  495. pr_err("%s: unsorted frequency tables are not supported\n",
  496. __func__);
  497. cdev = ERR_PTR(-EINVAL);
  498. goto free_idle_time;
  499. }
  500. ret = freq_qos_add_request(&policy->constraints,
  501. &cpufreq_cdev->qos_req, FREQ_QOS_MAX,
  502. get_state_freq(cpufreq_cdev, 0));
  503. if (ret < 0) {
  504. pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
  505. ret);
  506. cdev = ERR_PTR(ret);
  507. goto free_idle_time;
  508. }
  509. cdev = ERR_PTR(-ENOMEM);
  510. name = kasprintf(GFP_KERNEL, "cpufreq-%s", dev_name(dev));
  511. if (!name)
  512. goto remove_qos_req;
  513. cdev = thermal_of_cooling_device_register(np, name, cpufreq_cdev,
  514. cooling_ops);
  515. kfree(name);
  516. if (IS_ERR(cdev))
  517. goto remove_qos_req;
  518. return cdev;
  519. remove_qos_req:
  520. freq_qos_remove_request(&cpufreq_cdev->qos_req);
  521. free_idle_time:
  522. free_idle_time(cpufreq_cdev);
  523. free_cdev:
  524. kfree(cpufreq_cdev);
  525. return cdev;
  526. }
  527. /**
  528. * cpufreq_cooling_register - function to create cpufreq cooling device.
  529. * @policy: cpufreq policy
  530. *
  531. * This interface function registers the cpufreq cooling device with the name
  532. * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
  533. * devices.
  534. *
  535. * Return: a valid struct thermal_cooling_device pointer on success,
  536. * on failure, it returns a corresponding ERR_PTR().
  537. */
  538. struct thermal_cooling_device *
  539. cpufreq_cooling_register(struct cpufreq_policy *policy)
  540. {
  541. return __cpufreq_cooling_register(NULL, policy, NULL);
  542. }
  543. EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
  544. /**
  545. * of_cpufreq_cooling_register - function to create cpufreq cooling device.
  546. * @policy: cpufreq policy
  547. *
  548. * This interface function registers the cpufreq cooling device with the name
  549. * "cpufreq-%s". This API can support multiple instances of cpufreq cooling
  550. * devices. Using this API, the cpufreq cooling device will be linked to the
  551. * device tree node provided.
  552. *
  553. * Using this function, the cooling device will implement the power
  554. * extensions by using the Energy Model (if present). The cpus must have
  555. * registered their OPPs using the OPP library.
  556. *
  557. * Return: a valid struct thermal_cooling_device pointer on success,
  558. * and NULL on failure.
  559. */
  560. struct thermal_cooling_device *
  561. of_cpufreq_cooling_register(struct cpufreq_policy *policy)
  562. {
  563. struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
  564. struct thermal_cooling_device *cdev = NULL;
  565. if (!np) {
  566. pr_err("cpufreq_cooling: OF node not available for cpu%d\n",
  567. policy->cpu);
  568. return NULL;
  569. }
  570. if (of_property_present(np, "#cooling-cells")) {
  571. struct em_perf_domain *em = em_cpu_get(policy->cpu);
  572. cdev = __cpufreq_cooling_register(np, policy, em);
  573. if (IS_ERR(cdev)) {
  574. pr_err("cpufreq_cooling: cpu%d failed to register as cooling device: %ld\n",
  575. policy->cpu, PTR_ERR(cdev));
  576. cdev = NULL;
  577. }
  578. }
  579. of_node_put(np);
  580. return cdev;
  581. }
  582. EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
  583. /**
  584. * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  585. * @cdev: thermal cooling device pointer.
  586. *
  587. * This interface function unregisters the "cpufreq-%x" cooling device.
  588. */
  589. void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
  590. {
  591. struct cpufreq_cooling_device *cpufreq_cdev;
  592. if (!cdev)
  593. return;
  594. cpufreq_cdev = cdev->devdata;
  595. thermal_cooling_device_unregister(cdev);
  596. freq_qos_remove_request(&cpufreq_cdev->qos_req);
  597. free_idle_time(cpufreq_cdev);
  598. kfree(cpufreq_cdev);
  599. }
  600. EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);