devfreq_cooling.c 15 KB

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