devfreq_cooling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. static DEFINE_IDA(devfreq_ida);
  29. /**
  30. * struct devfreq_cooling_device - Devfreq cooling device
  31. * @id: unique integer value corresponding to each
  32. * devfreq_cooling_device registered.
  33. * @cdev: Pointer to associated thermal cooling device.
  34. * @devfreq: Pointer to associated devfreq device.
  35. * @cooling_state: Current cooling state.
  36. * @power_table: Pointer to table with maximum power draw for each
  37. * cooling state. State is the index into the table, and
  38. * the power is in mW.
  39. * @freq_table: Pointer to a table with the frequencies sorted in descending
  40. * order. You can index the table by cooling device state
  41. * @freq_table_size: Size of the @freq_table and @power_table
  42. * @power_ops: Pointer to devfreq_cooling_power, used to generate the
  43. * @power_table.
  44. */
  45. struct devfreq_cooling_device {
  46. int id;
  47. struct thermal_cooling_device *cdev;
  48. struct devfreq *devfreq;
  49. unsigned long cooling_state;
  50. u32 *power_table;
  51. u32 *freq_table;
  52. size_t freq_table_size;
  53. struct devfreq_cooling_power *power_ops;
  54. };
  55. /**
  56. * partition_enable_opps() - disable all opps above a given state
  57. * @dfc: Pointer to devfreq we are operating on
  58. * @cdev_state: cooling device state we're setting
  59. *
  60. * Go through the OPPs of the device, enabling all OPPs until
  61. * @cdev_state and disabling those frequencies above it.
  62. */
  63. static int partition_enable_opps(struct devfreq_cooling_device *dfc,
  64. unsigned long cdev_state)
  65. {
  66. int i;
  67. struct device *dev = dfc->devfreq->dev.parent;
  68. for (i = 0; i < dfc->freq_table_size; i++) {
  69. struct dev_pm_opp *opp;
  70. int ret = 0;
  71. unsigned int freq = dfc->freq_table[i];
  72. bool want_enable = i >= cdev_state ? true : false;
  73. opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
  74. if (PTR_ERR(opp) == -ERANGE)
  75. continue;
  76. else if (IS_ERR(opp))
  77. return PTR_ERR(opp);
  78. dev_pm_opp_put(opp);
  79. if (want_enable)
  80. ret = dev_pm_opp_enable(dev, freq);
  81. else
  82. ret = dev_pm_opp_disable(dev, freq);
  83. if (ret)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
  89. unsigned long *state)
  90. {
  91. struct devfreq_cooling_device *dfc = cdev->devdata;
  92. *state = dfc->freq_table_size - 1;
  93. return 0;
  94. }
  95. static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  96. unsigned long *state)
  97. {
  98. struct devfreq_cooling_device *dfc = cdev->devdata;
  99. *state = dfc->cooling_state;
  100. return 0;
  101. }
  102. static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  103. unsigned long state)
  104. {
  105. struct devfreq_cooling_device *dfc = cdev->devdata;
  106. struct devfreq *df = dfc->devfreq;
  107. struct device *dev = df->dev.parent;
  108. int ret;
  109. if (state == dfc->cooling_state)
  110. return 0;
  111. dev_dbg(dev, "Setting cooling state %lu\n", state);
  112. if (state >= dfc->freq_table_size)
  113. return -EINVAL;
  114. ret = partition_enable_opps(dfc, state);
  115. if (ret)
  116. return ret;
  117. dfc->cooling_state = state;
  118. return 0;
  119. }
  120. /**
  121. * freq_get_state() - get the cooling state corresponding to a frequency
  122. * @dfc: Pointer to devfreq cooling device
  123. * @freq: frequency in Hz
  124. *
  125. * Return: the cooling state associated with the @freq, or
  126. * THERMAL_CSTATE_INVALID if it wasn't found.
  127. */
  128. static unsigned long
  129. freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
  130. {
  131. int i;
  132. for (i = 0; i < dfc->freq_table_size; i++) {
  133. if (dfc->freq_table[i] == freq)
  134. return i;
  135. }
  136. return THERMAL_CSTATE_INVALID;
  137. }
  138. /**
  139. * get_static_power() - calculate the static power
  140. * @dfc: Pointer to devfreq cooling device
  141. * @freq: Frequency in Hz
  142. *
  143. * Calculate the static power in milliwatts using the supplied
  144. * get_static_power(). The current voltage is calculated using the
  145. * OPP library. If no get_static_power() was supplied, assume the
  146. * static power is negligible.
  147. */
  148. static unsigned long
  149. get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
  150. {
  151. struct devfreq *df = dfc->devfreq;
  152. struct device *dev = df->dev.parent;
  153. unsigned long voltage;
  154. struct dev_pm_opp *opp;
  155. if (!dfc->power_ops->get_static_power)
  156. return 0;
  157. opp = dev_pm_opp_find_freq_exact(dev, freq, true);
  158. if (PTR_ERR(opp) == -ERANGE)
  159. opp = dev_pm_opp_find_freq_exact(dev, freq, false);
  160. if (IS_ERR(opp)) {
  161. dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
  162. freq, PTR_ERR(opp));
  163. return 0;
  164. }
  165. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  166. dev_pm_opp_put(opp);
  167. if (voltage == 0) {
  168. dev_err_ratelimited(dev,
  169. "Failed to get voltage for frequency %lu\n",
  170. freq);
  171. return 0;
  172. }
  173. return dfc->power_ops->get_static_power(df, voltage);
  174. }
  175. /**
  176. * get_dynamic_power - calculate the dynamic power
  177. * @dfc: Pointer to devfreq cooling device
  178. * @freq: Frequency in Hz
  179. * @voltage: Voltage in millivolts
  180. *
  181. * Calculate the dynamic power in milliwatts consumed by the device at
  182. * frequency @freq and voltage @voltage. If the get_dynamic_power()
  183. * was supplied as part of the devfreq_cooling_power struct, then that
  184. * function is used. Otherwise, a simple power model (Pdyn = Coeff *
  185. * Voltage^2 * Frequency) is used.
  186. */
  187. static unsigned long
  188. get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
  189. unsigned long voltage)
  190. {
  191. u64 power;
  192. u32 freq_mhz;
  193. struct devfreq_cooling_power *dfc_power = dfc->power_ops;
  194. if (dfc_power->get_dynamic_power)
  195. return dfc_power->get_dynamic_power(dfc->devfreq, freq,
  196. voltage);
  197. freq_mhz = freq / 1000000;
  198. power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
  199. do_div(power, 1000000000);
  200. return power;
  201. }
  202. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  203. struct thermal_zone_device *tz,
  204. u32 *power)
  205. {
  206. struct devfreq_cooling_device *dfc = cdev->devdata;
  207. struct devfreq *df = dfc->devfreq;
  208. struct devfreq_dev_status *status = &df->last_status;
  209. unsigned long state;
  210. unsigned long freq = status->current_frequency;
  211. u32 dyn_power, static_power;
  212. /* Get dynamic power for state */
  213. state = freq_get_state(dfc, freq);
  214. if (state == THERMAL_CSTATE_INVALID)
  215. return -EAGAIN;
  216. dyn_power = dfc->power_table[state];
  217. /* Scale dynamic power for utilization */
  218. dyn_power = (dyn_power * status->busy_time) / status->total_time;
  219. /* Get static power */
  220. static_power = get_static_power(dfc, freq);
  221. trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
  222. static_power);
  223. *power = dyn_power + static_power;
  224. return 0;
  225. }
  226. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  227. struct thermal_zone_device *tz,
  228. unsigned long state,
  229. u32 *power)
  230. {
  231. struct devfreq_cooling_device *dfc = cdev->devdata;
  232. unsigned long freq;
  233. u32 static_power;
  234. if (state >= dfc->freq_table_size)
  235. return -EINVAL;
  236. freq = dfc->freq_table[state];
  237. static_power = get_static_power(dfc, freq);
  238. *power = dfc->power_table[state] + static_power;
  239. return 0;
  240. }
  241. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  242. struct thermal_zone_device *tz,
  243. u32 power, unsigned long *state)
  244. {
  245. struct devfreq_cooling_device *dfc = cdev->devdata;
  246. struct devfreq *df = dfc->devfreq;
  247. struct devfreq_dev_status *status = &df->last_status;
  248. unsigned long freq = status->current_frequency;
  249. unsigned long busy_time;
  250. s32 dyn_power;
  251. u32 static_power;
  252. int i;
  253. static_power = get_static_power(dfc, freq);
  254. dyn_power = power - static_power;
  255. dyn_power = dyn_power > 0 ? dyn_power : 0;
  256. /* Scale dynamic power for utilization */
  257. busy_time = status->busy_time ?: 1;
  258. dyn_power = (dyn_power * status->total_time) / busy_time;
  259. /*
  260. * Find the first cooling state that is within the power
  261. * budget for dynamic power.
  262. */
  263. for (i = 0; i < dfc->freq_table_size - 1; i++)
  264. if (dyn_power >= dfc->power_table[i])
  265. break;
  266. *state = i;
  267. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  268. return 0;
  269. }
  270. static struct thermal_cooling_device_ops devfreq_cooling_ops = {
  271. .get_max_state = devfreq_cooling_get_max_state,
  272. .get_cur_state = devfreq_cooling_get_cur_state,
  273. .set_cur_state = devfreq_cooling_set_cur_state,
  274. };
  275. /**
  276. * devfreq_cooling_gen_tables() - Generate power and freq tables.
  277. * @dfc: Pointer to devfreq cooling device.
  278. *
  279. * Generate power and frequency tables: the power table hold the
  280. * device's maximum power usage at each cooling state (OPP). The
  281. * static and dynamic power using the appropriate voltage and
  282. * frequency for the state, is acquired from the struct
  283. * devfreq_cooling_power, and summed to make the maximum power draw.
  284. *
  285. * The frequency table holds the frequencies in descending order.
  286. * That way its indexed by cooling device state.
  287. *
  288. * The tables are malloced, and pointers put in dfc. They must be
  289. * freed when unregistering the devfreq cooling device.
  290. *
  291. * Return: 0 on success, negative error code on failure.
  292. */
  293. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
  294. {
  295. struct devfreq *df = dfc->devfreq;
  296. struct device *dev = df->dev.parent;
  297. int ret, num_opps;
  298. unsigned long freq;
  299. u32 *power_table = NULL;
  300. u32 *freq_table;
  301. int i;
  302. num_opps = dev_pm_opp_get_opp_count(dev);
  303. if (dfc->power_ops) {
  304. power_table = kcalloc(num_opps, sizeof(*power_table),
  305. GFP_KERNEL);
  306. if (!power_table)
  307. return -ENOMEM;
  308. }
  309. freq_table = kcalloc(num_opps, sizeof(*freq_table),
  310. GFP_KERNEL);
  311. if (!freq_table) {
  312. ret = -ENOMEM;
  313. goto free_power_table;
  314. }
  315. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  316. unsigned long power_dyn, voltage;
  317. struct dev_pm_opp *opp;
  318. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  319. if (IS_ERR(opp)) {
  320. ret = PTR_ERR(opp);
  321. goto free_tables;
  322. }
  323. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  324. dev_pm_opp_put(opp);
  325. if (dfc->power_ops) {
  326. power_dyn = get_dynamic_power(dfc, freq, voltage);
  327. dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
  328. freq / 1000000, voltage, power_dyn, power_dyn);
  329. power_table[i] = power_dyn;
  330. }
  331. freq_table[i] = freq;
  332. }
  333. if (dfc->power_ops)
  334. dfc->power_table = power_table;
  335. dfc->freq_table = freq_table;
  336. dfc->freq_table_size = num_opps;
  337. return 0;
  338. free_tables:
  339. kfree(freq_table);
  340. free_power_table:
  341. kfree(power_table);
  342. return ret;
  343. }
  344. /**
  345. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  346. * with OF and power information.
  347. * @np: Pointer to OF device_node.
  348. * @df: Pointer to devfreq device.
  349. * @dfc_power: Pointer to devfreq_cooling_power.
  350. *
  351. * Register a devfreq cooling device. The available OPPs must be
  352. * registered on the device.
  353. *
  354. * If @dfc_power is provided, the cooling device is registered with the
  355. * power extensions. For the power extensions to work correctly,
  356. * devfreq should use the simple_ondemand governor, other governors
  357. * are not currently supported.
  358. */
  359. struct thermal_cooling_device *
  360. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  361. struct devfreq_cooling_power *dfc_power)
  362. {
  363. struct thermal_cooling_device *cdev;
  364. struct devfreq_cooling_device *dfc;
  365. char dev_name[THERMAL_NAME_LENGTH];
  366. int err;
  367. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  368. if (!dfc)
  369. return ERR_PTR(-ENOMEM);
  370. dfc->devfreq = df;
  371. if (dfc_power) {
  372. dfc->power_ops = dfc_power;
  373. devfreq_cooling_ops.get_requested_power =
  374. devfreq_cooling_get_requested_power;
  375. devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
  376. devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
  377. }
  378. err = devfreq_cooling_gen_tables(dfc);
  379. if (err)
  380. goto free_dfc;
  381. err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
  382. if (err < 0)
  383. goto free_tables;
  384. dfc->id = err;
  385. snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
  386. cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
  387. &devfreq_cooling_ops);
  388. if (IS_ERR(cdev)) {
  389. err = PTR_ERR(cdev);
  390. dev_err(df->dev.parent,
  391. "Failed to register devfreq cooling device (%d)\n",
  392. err);
  393. goto release_ida;
  394. }
  395. dfc->cdev = cdev;
  396. return cdev;
  397. release_ida:
  398. ida_simple_remove(&devfreq_ida, dfc->id);
  399. free_tables:
  400. kfree(dfc->power_table);
  401. kfree(dfc->freq_table);
  402. free_dfc:
  403. kfree(dfc);
  404. return ERR_PTR(err);
  405. }
  406. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
  407. /**
  408. * of_devfreq_cooling_register() - Register devfreq cooling device,
  409. * with OF information.
  410. * @np: Pointer to OF device_node.
  411. * @df: Pointer to devfreq device.
  412. */
  413. struct thermal_cooling_device *
  414. of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
  415. {
  416. return of_devfreq_cooling_register_power(np, df, NULL);
  417. }
  418. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
  419. /**
  420. * devfreq_cooling_register() - Register devfreq cooling device.
  421. * @df: Pointer to devfreq device.
  422. */
  423. struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
  424. {
  425. return of_devfreq_cooling_register(NULL, df);
  426. }
  427. EXPORT_SYMBOL_GPL(devfreq_cooling_register);
  428. /**
  429. * devfreq_cooling_unregister() - Unregister devfreq cooling device.
  430. * @dfc: Pointer to devfreq cooling device to unregister.
  431. */
  432. void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
  433. {
  434. struct devfreq_cooling_device *dfc;
  435. if (!cdev)
  436. return;
  437. dfc = cdev->devdata;
  438. thermal_cooling_device_unregister(dfc->cdev);
  439. ida_simple_remove(&devfreq_ida, dfc->id);
  440. kfree(dfc->power_table);
  441. kfree(dfc->freq_table);
  442. kfree(dfc);
  443. }
  444. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);