devfreq_cooling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. 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: %ld\n",
  165. freq, IS_ERR(opp) ? PTR_ERR(opp) : 0);
  166. return 0;
  167. }
  168. return dfc->power_ops->get_static_power(df, voltage);
  169. }
  170. /**
  171. * get_dynamic_power - calculate the dynamic power
  172. * @dfc: Pointer to devfreq cooling device
  173. * @freq: Frequency in Hz
  174. * @voltage: Voltage in millivolts
  175. *
  176. * Calculate the dynamic power in milliwatts consumed by the device at
  177. * frequency @freq and voltage @voltage. If the get_dynamic_power()
  178. * was supplied as part of the devfreq_cooling_power struct, then that
  179. * function is used. Otherwise, a simple power model (Pdyn = Coeff *
  180. * Voltage^2 * Frequency) is used.
  181. */
  182. static unsigned long
  183. get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
  184. unsigned long voltage)
  185. {
  186. u64 power;
  187. u32 freq_mhz;
  188. struct devfreq_cooling_power *dfc_power = dfc->power_ops;
  189. if (dfc_power->get_dynamic_power)
  190. return dfc_power->get_dynamic_power(dfc->devfreq, freq,
  191. voltage);
  192. freq_mhz = freq / 1000000;
  193. power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
  194. do_div(power, 1000000000);
  195. return power;
  196. }
  197. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  198. struct thermal_zone_device *tz,
  199. u32 *power)
  200. {
  201. struct devfreq_cooling_device *dfc = cdev->devdata;
  202. struct devfreq *df = dfc->devfreq;
  203. struct devfreq_dev_status *status = &df->last_status;
  204. unsigned long state;
  205. unsigned long freq = status->current_frequency;
  206. u32 dyn_power, static_power;
  207. /* Get dynamic power for state */
  208. state = freq_get_state(dfc, freq);
  209. if (state == THERMAL_CSTATE_INVALID)
  210. return -EAGAIN;
  211. dyn_power = dfc->power_table[state];
  212. /* Scale dynamic power for utilization */
  213. dyn_power = (dyn_power * status->busy_time) / status->total_time;
  214. /* Get static power */
  215. static_power = get_static_power(dfc, freq);
  216. trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
  217. static_power);
  218. *power = dyn_power + static_power;
  219. return 0;
  220. }
  221. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  222. struct thermal_zone_device *tz,
  223. unsigned long state,
  224. u32 *power)
  225. {
  226. struct devfreq_cooling_device *dfc = cdev->devdata;
  227. unsigned long freq;
  228. u32 static_power;
  229. if (state >= dfc->freq_table_size)
  230. return -EINVAL;
  231. freq = dfc->freq_table[state];
  232. static_power = get_static_power(dfc, freq);
  233. *power = dfc->power_table[state] + static_power;
  234. return 0;
  235. }
  236. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  237. struct thermal_zone_device *tz,
  238. u32 power, unsigned long *state)
  239. {
  240. struct devfreq_cooling_device *dfc = cdev->devdata;
  241. struct devfreq *df = dfc->devfreq;
  242. struct devfreq_dev_status *status = &df->last_status;
  243. unsigned long freq = status->current_frequency;
  244. unsigned long busy_time;
  245. s32 dyn_power;
  246. u32 static_power;
  247. int i;
  248. static_power = get_static_power(dfc, freq);
  249. dyn_power = power - static_power;
  250. dyn_power = dyn_power > 0 ? dyn_power : 0;
  251. /* Scale dynamic power for utilization */
  252. busy_time = status->busy_time ?: 1;
  253. dyn_power = (dyn_power * status->total_time) / busy_time;
  254. /*
  255. * Find the first cooling state that is within the power
  256. * budget for dynamic power.
  257. */
  258. for (i = 0; i < dfc->freq_table_size - 1; i++)
  259. if (dyn_power >= dfc->power_table[i])
  260. break;
  261. *state = i;
  262. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  263. return 0;
  264. }
  265. static struct thermal_cooling_device_ops devfreq_cooling_ops = {
  266. .get_max_state = devfreq_cooling_get_max_state,
  267. .get_cur_state = devfreq_cooling_get_cur_state,
  268. .set_cur_state = devfreq_cooling_set_cur_state,
  269. };
  270. /**
  271. * devfreq_cooling_gen_tables() - Generate power and freq tables.
  272. * @dfc: Pointer to devfreq cooling device.
  273. *
  274. * Generate power and frequency tables: the power table hold the
  275. * device's maximum power usage at each cooling state (OPP). The
  276. * static and dynamic power using the appropriate voltage and
  277. * frequency for the state, is acquired from the struct
  278. * devfreq_cooling_power, and summed to make the maximum power draw.
  279. *
  280. * The frequency table holds the frequencies in descending order.
  281. * That way its indexed by cooling device state.
  282. *
  283. * The tables are malloced, and pointers put in dfc. They must be
  284. * freed when unregistering the devfreq cooling device.
  285. *
  286. * Return: 0 on success, negative error code on failure.
  287. */
  288. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
  289. {
  290. struct devfreq *df = dfc->devfreq;
  291. struct device *dev = df->dev.parent;
  292. int ret, num_opps;
  293. unsigned long freq;
  294. u32 *power_table = NULL;
  295. u32 *freq_table;
  296. int i;
  297. num_opps = dev_pm_opp_get_opp_count(dev);
  298. if (dfc->power_ops) {
  299. power_table = kcalloc(num_opps, sizeof(*power_table),
  300. GFP_KERNEL);
  301. if (!power_table)
  302. return -ENOMEM;
  303. }
  304. freq_table = kcalloc(num_opps, sizeof(*freq_table),
  305. GFP_KERNEL);
  306. if (!freq_table) {
  307. ret = -ENOMEM;
  308. goto free_power_table;
  309. }
  310. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  311. unsigned long power_dyn, voltage;
  312. struct dev_pm_opp *opp;
  313. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  314. if (IS_ERR(opp)) {
  315. ret = PTR_ERR(opp);
  316. goto free_tables;
  317. }
  318. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  319. dev_pm_opp_put(opp);
  320. if (dfc->power_ops) {
  321. power_dyn = get_dynamic_power(dfc, freq, voltage);
  322. dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
  323. freq / 1000000, voltage, power_dyn, power_dyn);
  324. power_table[i] = power_dyn;
  325. }
  326. freq_table[i] = freq;
  327. }
  328. if (dfc->power_ops)
  329. dfc->power_table = power_table;
  330. dfc->freq_table = freq_table;
  331. dfc->freq_table_size = num_opps;
  332. return 0;
  333. free_tables:
  334. kfree(freq_table);
  335. free_power_table:
  336. kfree(power_table);
  337. return ret;
  338. }
  339. /**
  340. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  341. * with OF and power information.
  342. * @np: Pointer to OF device_node.
  343. * @df: Pointer to devfreq device.
  344. * @dfc_power: Pointer to devfreq_cooling_power.
  345. *
  346. * Register a devfreq cooling device. The available OPPs must be
  347. * registered on the device.
  348. *
  349. * If @dfc_power is provided, the cooling device is registered with the
  350. * power extensions. For the power extensions to work correctly,
  351. * devfreq should use the simple_ondemand governor, other governors
  352. * are not currently supported.
  353. */
  354. struct thermal_cooling_device *
  355. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  356. struct devfreq_cooling_power *dfc_power)
  357. {
  358. struct thermal_cooling_device *cdev;
  359. struct devfreq_cooling_device *dfc;
  360. char dev_name[THERMAL_NAME_LENGTH];
  361. int err;
  362. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  363. if (!dfc)
  364. return ERR_PTR(-ENOMEM);
  365. dfc->devfreq = df;
  366. if (dfc_power) {
  367. dfc->power_ops = dfc_power;
  368. devfreq_cooling_ops.get_requested_power =
  369. devfreq_cooling_get_requested_power;
  370. devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
  371. devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
  372. }
  373. err = devfreq_cooling_gen_tables(dfc);
  374. if (err)
  375. goto free_dfc;
  376. err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
  377. if (err < 0)
  378. goto free_tables;
  379. dfc->id = err;
  380. snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
  381. cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
  382. &devfreq_cooling_ops);
  383. if (IS_ERR(cdev)) {
  384. err = PTR_ERR(cdev);
  385. dev_err(df->dev.parent,
  386. "Failed to register devfreq cooling device (%d)\n",
  387. err);
  388. goto release_ida;
  389. }
  390. dfc->cdev = cdev;
  391. return cdev;
  392. release_ida:
  393. ida_simple_remove(&devfreq_ida, dfc->id);
  394. free_tables:
  395. kfree(dfc->power_table);
  396. kfree(dfc->freq_table);
  397. free_dfc:
  398. kfree(dfc);
  399. return ERR_PTR(err);
  400. }
  401. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
  402. /**
  403. * of_devfreq_cooling_register() - Register devfreq cooling device,
  404. * with OF information.
  405. * @np: Pointer to OF device_node.
  406. * @df: Pointer to devfreq device.
  407. */
  408. struct thermal_cooling_device *
  409. of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
  410. {
  411. return of_devfreq_cooling_register_power(np, df, NULL);
  412. }
  413. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
  414. /**
  415. * devfreq_cooling_register() - Register devfreq cooling device.
  416. * @df: Pointer to devfreq device.
  417. */
  418. struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
  419. {
  420. return of_devfreq_cooling_register(NULL, df);
  421. }
  422. EXPORT_SYMBOL_GPL(devfreq_cooling_register);
  423. /**
  424. * devfreq_cooling_unregister() - Unregister devfreq cooling device.
  425. * @dfc: Pointer to devfreq cooling device to unregister.
  426. */
  427. void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
  428. {
  429. struct devfreq_cooling_device *dfc;
  430. if (!cdev)
  431. return;
  432. dfc = cdev->devdata;
  433. thermal_cooling_device_unregister(dfc->cdev);
  434. ida_simple_remove(&devfreq_ida, dfc->id);
  435. kfree(dfc->power_table);
  436. kfree(dfc->freq_table);
  437. kfree(dfc);
  438. }
  439. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);