devfreq_cooling.c 14 KB

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