devfreq_cooling.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * devfreq_cooling: Thermal cooling device implementation for devices using
  3. * devfreq
  4. *
  5. * Copyright (C) 2014-2015 ARM Limited
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * TODO:
  17. * - If OPPs are added or removed after devfreq cooling has
  18. * registered, the devfreq cooling won't react to it.
  19. */
  20. #include <linux/devfreq.h>
  21. #include <linux/devfreq_cooling.h>
  22. #include <linux/export.h>
  23. #include <linux/idr.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm_opp.h>
  26. #include <linux/thermal.h>
  27. #include <trace/events/thermal.h>
  28. #define SCALE_ERROR_MITIGATION 100
  29. static DEFINE_IDA(devfreq_ida);
  30. /**
  31. * struct devfreq_cooling_device - Devfreq cooling device
  32. * @id: unique integer value corresponding to each
  33. * devfreq_cooling_device registered.
  34. * @cdev: Pointer to associated thermal cooling device.
  35. * @devfreq: Pointer to associated devfreq device.
  36. * @cooling_state: Current cooling state.
  37. * @power_table: Pointer to table with maximum power draw for each
  38. * cooling state. State is the index into the table, and
  39. * the power is in mW.
  40. * @freq_table: Pointer to a table with the frequencies sorted in descending
  41. * order. You can index the table by cooling device state
  42. * @freq_table_size: Size of the @freq_table and @power_table
  43. * @power_ops: Pointer to devfreq_cooling_power, used to generate the
  44. * @power_table.
  45. * @res_util: Resource utilization scaling factor for the power.
  46. * It is multiplied by 100 to minimize the error. It is used
  47. * for estimation of the power budget instead of using
  48. * 'utilization' (which is 'busy_time / 'total_time').
  49. * The 'res_util' range is from 100 to (power_table[state] * 100)
  50. * for the corresponding 'state'.
  51. */
  52. struct devfreq_cooling_device {
  53. int id;
  54. struct thermal_cooling_device *cdev;
  55. struct devfreq *devfreq;
  56. unsigned long cooling_state;
  57. u32 *power_table;
  58. u32 *freq_table;
  59. size_t freq_table_size;
  60. struct devfreq_cooling_power *power_ops;
  61. u32 res_util;
  62. int capped_state;
  63. };
  64. /**
  65. * partition_enable_opps() - disable all opps above a given state
  66. * @dfc: Pointer to devfreq we are operating on
  67. * @cdev_state: cooling device state we're setting
  68. *
  69. * Go through the OPPs of the device, enabling all OPPs until
  70. * @cdev_state and disabling those frequencies above it.
  71. */
  72. static int partition_enable_opps(struct devfreq_cooling_device *dfc,
  73. unsigned long cdev_state)
  74. {
  75. int i;
  76. struct device *dev = dfc->devfreq->dev.parent;
  77. for (i = 0; i < dfc->freq_table_size; i++) {
  78. struct dev_pm_opp *opp;
  79. int ret = 0;
  80. unsigned int freq = dfc->freq_table[i];
  81. bool want_enable = i >= cdev_state ? true : false;
  82. opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
  83. if (PTR_ERR(opp) == -ERANGE)
  84. continue;
  85. else if (IS_ERR(opp))
  86. return PTR_ERR(opp);
  87. dev_pm_opp_put(opp);
  88. if (want_enable)
  89. ret = dev_pm_opp_enable(dev, freq);
  90. else
  91. ret = dev_pm_opp_disable(dev, freq);
  92. if (ret)
  93. return ret;
  94. }
  95. return 0;
  96. }
  97. static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
  98. unsigned long *state)
  99. {
  100. struct devfreq_cooling_device *dfc = cdev->devdata;
  101. *state = dfc->freq_table_size - 1;
  102. return 0;
  103. }
  104. static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  105. unsigned long *state)
  106. {
  107. struct devfreq_cooling_device *dfc = cdev->devdata;
  108. *state = dfc->cooling_state;
  109. return 0;
  110. }
  111. static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  112. unsigned long state)
  113. {
  114. struct devfreq_cooling_device *dfc = cdev->devdata;
  115. struct devfreq *df = dfc->devfreq;
  116. struct device *dev = df->dev.parent;
  117. int ret;
  118. if (state == dfc->cooling_state)
  119. return 0;
  120. dev_dbg(dev, "Setting cooling state %lu\n", state);
  121. if (state >= dfc->freq_table_size)
  122. return -EINVAL;
  123. ret = partition_enable_opps(dfc, state);
  124. if (ret)
  125. return ret;
  126. dfc->cooling_state = state;
  127. return 0;
  128. }
  129. /**
  130. * freq_get_state() - get the cooling state corresponding to a frequency
  131. * @dfc: Pointer to devfreq cooling device
  132. * @freq: frequency in Hz
  133. *
  134. * Return: the cooling state associated with the @freq, or
  135. * THERMAL_CSTATE_INVALID if it wasn't found.
  136. */
  137. static unsigned long
  138. freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
  139. {
  140. int i;
  141. for (i = 0; i < dfc->freq_table_size; i++) {
  142. if (dfc->freq_table[i] == freq)
  143. return i;
  144. }
  145. return THERMAL_CSTATE_INVALID;
  146. }
  147. static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
  148. {
  149. struct device *dev = df->dev.parent;
  150. unsigned long voltage;
  151. struct dev_pm_opp *opp;
  152. opp = dev_pm_opp_find_freq_exact(dev, freq, true);
  153. if (PTR_ERR(opp) == -ERANGE)
  154. opp = dev_pm_opp_find_freq_exact(dev, freq, false);
  155. if (IS_ERR(opp)) {
  156. dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
  157. freq, PTR_ERR(opp));
  158. return 0;
  159. }
  160. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  161. dev_pm_opp_put(opp);
  162. if (voltage == 0) {
  163. dev_err_ratelimited(dev,
  164. "Failed to get voltage for frequency %lu\n",
  165. freq);
  166. }
  167. return voltage;
  168. }
  169. /**
  170. * get_static_power() - calculate the static power
  171. * @dfc: Pointer to devfreq cooling device
  172. * @freq: Frequency in Hz
  173. *
  174. * Calculate the static power in milliwatts using the supplied
  175. * get_static_power(). The current voltage is calculated using the
  176. * OPP library. If no get_static_power() was supplied, assume the
  177. * static power is negligible.
  178. */
  179. static unsigned long
  180. get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
  181. {
  182. struct devfreq *df = dfc->devfreq;
  183. unsigned long voltage;
  184. if (!dfc->power_ops->get_static_power)
  185. return 0;
  186. voltage = get_voltage(df, freq);
  187. if (voltage == 0)
  188. return 0;
  189. return dfc->power_ops->get_static_power(df, voltage);
  190. }
  191. /**
  192. * get_dynamic_power - calculate the dynamic power
  193. * @dfc: Pointer to devfreq cooling device
  194. * @freq: Frequency in Hz
  195. * @voltage: Voltage in millivolts
  196. *
  197. * Calculate the dynamic power in milliwatts consumed by the device at
  198. * frequency @freq and voltage @voltage. If the get_dynamic_power()
  199. * was supplied as part of the devfreq_cooling_power struct, then that
  200. * function is used. Otherwise, a simple power model (Pdyn = Coeff *
  201. * Voltage^2 * Frequency) is used.
  202. */
  203. static unsigned long
  204. get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
  205. unsigned long voltage)
  206. {
  207. u64 power;
  208. u32 freq_mhz;
  209. struct devfreq_cooling_power *dfc_power = dfc->power_ops;
  210. if (dfc_power->get_dynamic_power)
  211. return dfc_power->get_dynamic_power(dfc->devfreq, freq,
  212. voltage);
  213. freq_mhz = freq / 1000000;
  214. power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
  215. do_div(power, 1000000000);
  216. return power;
  217. }
  218. static inline unsigned long get_total_power(struct devfreq_cooling_device *dfc,
  219. unsigned long freq,
  220. unsigned long voltage)
  221. {
  222. return get_static_power(dfc, freq) + get_dynamic_power(dfc, freq,
  223. voltage);
  224. }
  225. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  226. struct thermal_zone_device *tz,
  227. u32 *power)
  228. {
  229. struct devfreq_cooling_device *dfc = cdev->devdata;
  230. struct devfreq *df = dfc->devfreq;
  231. struct devfreq_dev_status *status = &df->last_status;
  232. unsigned long state;
  233. unsigned long freq = status->current_frequency;
  234. unsigned long voltage;
  235. u32 dyn_power = 0;
  236. u32 static_power = 0;
  237. int res;
  238. state = freq_get_state(dfc, freq);
  239. if (state == THERMAL_CSTATE_INVALID) {
  240. res = -EAGAIN;
  241. goto fail;
  242. }
  243. if (dfc->power_ops->get_real_power) {
  244. voltage = get_voltage(df, freq);
  245. if (voltage == 0) {
  246. res = -EINVAL;
  247. goto fail;
  248. }
  249. res = dfc->power_ops->get_real_power(df, power, freq, voltage);
  250. if (!res) {
  251. state = dfc->capped_state;
  252. dfc->res_util = dfc->power_table[state];
  253. dfc->res_util *= SCALE_ERROR_MITIGATION;
  254. if (*power > 1)
  255. dfc->res_util /= *power;
  256. } else {
  257. goto fail;
  258. }
  259. } else {
  260. dyn_power = dfc->power_table[state];
  261. /* Scale dynamic power for utilization */
  262. dyn_power *= status->busy_time;
  263. dyn_power /= status->total_time;
  264. /* Get static power */
  265. static_power = get_static_power(dfc, freq);
  266. *power = dyn_power + static_power;
  267. }
  268. trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
  269. static_power, *power);
  270. return 0;
  271. fail:
  272. /* It is safe to set max in this case */
  273. dfc->res_util = SCALE_ERROR_MITIGATION;
  274. return res;
  275. }
  276. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  277. struct thermal_zone_device *tz,
  278. unsigned long state,
  279. u32 *power)
  280. {
  281. struct devfreq_cooling_device *dfc = cdev->devdata;
  282. unsigned long freq;
  283. u32 static_power;
  284. if (state >= dfc->freq_table_size)
  285. return -EINVAL;
  286. freq = dfc->freq_table[state];
  287. static_power = get_static_power(dfc, freq);
  288. *power = dfc->power_table[state] + static_power;
  289. return 0;
  290. }
  291. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  292. struct thermal_zone_device *tz,
  293. u32 power, unsigned long *state)
  294. {
  295. struct devfreq_cooling_device *dfc = cdev->devdata;
  296. struct devfreq *df = dfc->devfreq;
  297. struct devfreq_dev_status *status = &df->last_status;
  298. unsigned long freq = status->current_frequency;
  299. unsigned long busy_time;
  300. s32 dyn_power;
  301. u32 static_power;
  302. s32 est_power;
  303. int i;
  304. if (dfc->power_ops->get_real_power) {
  305. /* Scale for resource utilization */
  306. est_power = power * dfc->res_util;
  307. est_power /= SCALE_ERROR_MITIGATION;
  308. } else {
  309. static_power = get_static_power(dfc, freq);
  310. dyn_power = power - static_power;
  311. dyn_power = dyn_power > 0 ? dyn_power : 0;
  312. /* Scale dynamic power for utilization */
  313. busy_time = status->busy_time ?: 1;
  314. est_power = (dyn_power * status->total_time) / busy_time;
  315. }
  316. /*
  317. * Find the first cooling state that is within the power
  318. * budget for dynamic power.
  319. */
  320. for (i = 0; i < dfc->freq_table_size - 1; i++)
  321. if (est_power >= dfc->power_table[i])
  322. break;
  323. *state = i;
  324. dfc->capped_state = i;
  325. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  326. return 0;
  327. }
  328. static struct thermal_cooling_device_ops devfreq_cooling_ops = {
  329. .get_max_state = devfreq_cooling_get_max_state,
  330. .get_cur_state = devfreq_cooling_get_cur_state,
  331. .set_cur_state = devfreq_cooling_set_cur_state,
  332. };
  333. /**
  334. * devfreq_cooling_gen_tables() - Generate power and freq tables.
  335. * @dfc: Pointer to devfreq cooling device.
  336. *
  337. * Generate power and frequency tables: the power table hold the
  338. * device's maximum power usage at each cooling state (OPP). The
  339. * static and dynamic power using the appropriate voltage and
  340. * frequency for the state, is acquired from the struct
  341. * devfreq_cooling_power, and summed to make the maximum power draw.
  342. *
  343. * The frequency table holds the frequencies in descending order.
  344. * That way its indexed by cooling device state.
  345. *
  346. * The tables are malloced, and pointers put in dfc. They must be
  347. * freed when unregistering the devfreq cooling device.
  348. *
  349. * Return: 0 on success, negative error code on failure.
  350. */
  351. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
  352. {
  353. struct devfreq *df = dfc->devfreq;
  354. struct device *dev = df->dev.parent;
  355. int ret, num_opps;
  356. unsigned long freq;
  357. u32 *power_table = NULL;
  358. u32 *freq_table;
  359. int i;
  360. num_opps = dev_pm_opp_get_opp_count(dev);
  361. if (dfc->power_ops) {
  362. power_table = kcalloc(num_opps, sizeof(*power_table),
  363. GFP_KERNEL);
  364. if (!power_table)
  365. return -ENOMEM;
  366. }
  367. freq_table = kcalloc(num_opps, sizeof(*freq_table),
  368. GFP_KERNEL);
  369. if (!freq_table) {
  370. ret = -ENOMEM;
  371. goto free_power_table;
  372. }
  373. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  374. unsigned long power, voltage;
  375. struct dev_pm_opp *opp;
  376. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  377. if (IS_ERR(opp)) {
  378. ret = PTR_ERR(opp);
  379. goto free_tables;
  380. }
  381. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  382. dev_pm_opp_put(opp);
  383. if (dfc->power_ops) {
  384. if (dfc->power_ops->get_real_power)
  385. power = get_total_power(dfc, freq, voltage);
  386. else
  387. power = get_dynamic_power(dfc, freq, voltage);
  388. dev_dbg(dev, "Power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
  389. freq / 1000000, voltage, power, power);
  390. power_table[i] = power;
  391. }
  392. freq_table[i] = freq;
  393. }
  394. if (dfc->power_ops)
  395. dfc->power_table = power_table;
  396. dfc->freq_table = freq_table;
  397. dfc->freq_table_size = num_opps;
  398. return 0;
  399. free_tables:
  400. kfree(freq_table);
  401. free_power_table:
  402. kfree(power_table);
  403. return ret;
  404. }
  405. /**
  406. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  407. * with OF and power information.
  408. * @np: Pointer to OF device_node.
  409. * @df: Pointer to devfreq device.
  410. * @dfc_power: Pointer to devfreq_cooling_power.
  411. *
  412. * Register a devfreq cooling device. The available OPPs must be
  413. * registered on the device.
  414. *
  415. * If @dfc_power is provided, the cooling device is registered with the
  416. * power extensions. For the power extensions to work correctly,
  417. * devfreq should use the simple_ondemand governor, other governors
  418. * are not currently supported.
  419. */
  420. struct thermal_cooling_device *
  421. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  422. struct devfreq_cooling_power *dfc_power)
  423. {
  424. struct thermal_cooling_device *cdev;
  425. struct devfreq_cooling_device *dfc;
  426. char dev_name[THERMAL_NAME_LENGTH];
  427. int err;
  428. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  429. if (!dfc)
  430. return ERR_PTR(-ENOMEM);
  431. dfc->devfreq = df;
  432. if (dfc_power) {
  433. dfc->power_ops = dfc_power;
  434. devfreq_cooling_ops.get_requested_power =
  435. devfreq_cooling_get_requested_power;
  436. devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
  437. devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
  438. }
  439. err = devfreq_cooling_gen_tables(dfc);
  440. if (err)
  441. goto free_dfc;
  442. err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
  443. if (err < 0)
  444. goto free_tables;
  445. dfc->id = err;
  446. snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
  447. cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
  448. &devfreq_cooling_ops);
  449. if (IS_ERR(cdev)) {
  450. err = PTR_ERR(cdev);
  451. dev_err(df->dev.parent,
  452. "Failed to register devfreq cooling device (%d)\n",
  453. err);
  454. goto release_ida;
  455. }
  456. dfc->cdev = cdev;
  457. return cdev;
  458. release_ida:
  459. ida_simple_remove(&devfreq_ida, dfc->id);
  460. free_tables:
  461. kfree(dfc->power_table);
  462. kfree(dfc->freq_table);
  463. free_dfc:
  464. kfree(dfc);
  465. return ERR_PTR(err);
  466. }
  467. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
  468. /**
  469. * of_devfreq_cooling_register() - Register devfreq cooling device,
  470. * with OF information.
  471. * @np: Pointer to OF device_node.
  472. * @df: Pointer to devfreq device.
  473. */
  474. struct thermal_cooling_device *
  475. of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
  476. {
  477. return of_devfreq_cooling_register_power(np, df, NULL);
  478. }
  479. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
  480. /**
  481. * devfreq_cooling_register() - Register devfreq cooling device.
  482. * @df: Pointer to devfreq device.
  483. */
  484. struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
  485. {
  486. return of_devfreq_cooling_register(NULL, df);
  487. }
  488. EXPORT_SYMBOL_GPL(devfreq_cooling_register);
  489. /**
  490. * devfreq_cooling_unregister() - Unregister devfreq cooling device.
  491. * @dfc: Pointer to devfreq cooling device to unregister.
  492. */
  493. void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
  494. {
  495. struct devfreq_cooling_device *dfc;
  496. if (!cdev)
  497. return;
  498. dfc = cdev->devdata;
  499. thermal_cooling_device_unregister(dfc->cdev);
  500. ida_simple_remove(&devfreq_ida, dfc->id);
  501. kfree(dfc->power_table);
  502. kfree(dfc->freq_table);
  503. kfree(dfc);
  504. }
  505. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);