devfreq_cooling.c 15 KB

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