devfreq_cooling.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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(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(freq, voltage);
  225. freq_mhz = freq / 1000000;
  226. power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
  227. do_div(power, 1000000000);
  228. return power;
  229. }
  230. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  231. struct thermal_zone_device *tz,
  232. u32 *power)
  233. {
  234. struct devfreq_cooling_device *dfc = cdev->devdata;
  235. struct devfreq *df = dfc->devfreq;
  236. struct devfreq_dev_status *status = &df->last_status;
  237. unsigned long state;
  238. unsigned long freq = status->current_frequency;
  239. u32 dyn_power, static_power;
  240. /* Get dynamic power for state */
  241. state = freq_get_state(dfc, freq);
  242. if (state == THERMAL_CSTATE_INVALID)
  243. return -EAGAIN;
  244. dyn_power = dfc->power_table[state];
  245. /* Scale dynamic power for utilization */
  246. dyn_power = (dyn_power * status->busy_time) / status->total_time;
  247. /* Get static power */
  248. static_power = get_static_power(dfc, freq);
  249. trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
  250. static_power);
  251. *power = dyn_power + static_power;
  252. return 0;
  253. }
  254. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  255. struct thermal_zone_device *tz,
  256. unsigned long state,
  257. u32 *power)
  258. {
  259. struct devfreq_cooling_device *dfc = cdev->devdata;
  260. unsigned long freq;
  261. u32 static_power;
  262. if (state < 0 || state >= dfc->freq_table_size)
  263. return -EINVAL;
  264. freq = dfc->freq_table[state];
  265. static_power = get_static_power(dfc, freq);
  266. *power = dfc->power_table[state] + static_power;
  267. return 0;
  268. }
  269. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  270. struct thermal_zone_device *tz,
  271. u32 power, unsigned long *state)
  272. {
  273. struct devfreq_cooling_device *dfc = cdev->devdata;
  274. struct devfreq *df = dfc->devfreq;
  275. struct devfreq_dev_status *status = &df->last_status;
  276. unsigned long freq = status->current_frequency;
  277. unsigned long busy_time;
  278. s32 dyn_power;
  279. u32 static_power;
  280. int i;
  281. static_power = get_static_power(dfc, freq);
  282. dyn_power = power - static_power;
  283. dyn_power = dyn_power > 0 ? dyn_power : 0;
  284. /* Scale dynamic power for utilization */
  285. busy_time = status->busy_time ?: 1;
  286. dyn_power = (dyn_power * status->total_time) / busy_time;
  287. /*
  288. * Find the first cooling state that is within the power
  289. * budget for dynamic power.
  290. */
  291. for (i = 0; i < dfc->freq_table_size - 1; i++)
  292. if (dyn_power >= dfc->power_table[i])
  293. break;
  294. *state = i;
  295. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  296. return 0;
  297. }
  298. static struct thermal_cooling_device_ops devfreq_cooling_ops = {
  299. .get_max_state = devfreq_cooling_get_max_state,
  300. .get_cur_state = devfreq_cooling_get_cur_state,
  301. .set_cur_state = devfreq_cooling_set_cur_state,
  302. };
  303. /**
  304. * devfreq_cooling_gen_tables() - Generate power and freq tables.
  305. * @dfc: Pointer to devfreq cooling device.
  306. *
  307. * Generate power and frequency tables: the power table hold the
  308. * device's maximum power usage at each cooling state (OPP). The
  309. * static and dynamic power using the appropriate voltage and
  310. * frequency for the state, is acquired from the struct
  311. * devfreq_cooling_power, and summed to make the maximum power draw.
  312. *
  313. * The frequency table holds the frequencies in descending order.
  314. * That way its indexed by cooling device state.
  315. *
  316. * The tables are malloced, and pointers put in dfc. They must be
  317. * freed when unregistering the devfreq cooling device.
  318. *
  319. * Return: 0 on success, negative error code on failure.
  320. */
  321. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
  322. {
  323. struct devfreq *df = dfc->devfreq;
  324. struct device *dev = df->dev.parent;
  325. int ret, num_opps;
  326. unsigned long freq;
  327. u32 *power_table = NULL;
  328. u32 *freq_table;
  329. int i;
  330. num_opps = dev_pm_opp_get_opp_count(dev);
  331. if (dfc->power_ops) {
  332. power_table = kcalloc(num_opps, sizeof(*power_table),
  333. GFP_KERNEL);
  334. if (!power_table)
  335. return -ENOMEM;
  336. }
  337. freq_table = kcalloc(num_opps, sizeof(*freq_table),
  338. GFP_KERNEL);
  339. if (!freq_table) {
  340. ret = -ENOMEM;
  341. goto free_power_table;
  342. }
  343. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  344. unsigned long power_dyn, voltage;
  345. struct dev_pm_opp *opp;
  346. rcu_read_lock();
  347. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  348. if (IS_ERR(opp)) {
  349. rcu_read_unlock();
  350. ret = PTR_ERR(opp);
  351. goto free_tables;
  352. }
  353. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  354. rcu_read_unlock();
  355. if (dfc->power_ops) {
  356. power_dyn = get_dynamic_power(dfc, freq, voltage);
  357. dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
  358. freq / 1000000, voltage, power_dyn, power_dyn);
  359. power_table[i] = power_dyn;
  360. }
  361. freq_table[i] = freq;
  362. }
  363. if (dfc->power_ops)
  364. dfc->power_table = power_table;
  365. dfc->freq_table = freq_table;
  366. dfc->freq_table_size = num_opps;
  367. return 0;
  368. free_tables:
  369. kfree(freq_table);
  370. free_power_table:
  371. kfree(power_table);
  372. return ret;
  373. }
  374. /**
  375. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  376. * with OF and power information.
  377. * @np: Pointer to OF device_node.
  378. * @df: Pointer to devfreq device.
  379. * @dfc_power: Pointer to devfreq_cooling_power.
  380. *
  381. * Register a devfreq cooling device. The available OPPs must be
  382. * registered on the device.
  383. *
  384. * If @dfc_power is provided, the cooling device is registered with the
  385. * power extensions. For the power extensions to work correctly,
  386. * devfreq should use the simple_ondemand governor, other governors
  387. * are not currently supported.
  388. */
  389. struct thermal_cooling_device *
  390. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  391. struct devfreq_cooling_power *dfc_power)
  392. {
  393. struct thermal_cooling_device *cdev;
  394. struct devfreq_cooling_device *dfc;
  395. char dev_name[THERMAL_NAME_LENGTH];
  396. int err;
  397. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  398. if (!dfc)
  399. return ERR_PTR(-ENOMEM);
  400. dfc->devfreq = df;
  401. if (dfc_power) {
  402. dfc->power_ops = dfc_power;
  403. devfreq_cooling_ops.get_requested_power =
  404. devfreq_cooling_get_requested_power;
  405. devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
  406. devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
  407. }
  408. err = devfreq_cooling_gen_tables(dfc);
  409. if (err)
  410. goto free_dfc;
  411. err = get_idr(&devfreq_idr, &dfc->id);
  412. if (err)
  413. goto free_tables;
  414. snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
  415. cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
  416. &devfreq_cooling_ops);
  417. if (IS_ERR(cdev)) {
  418. err = PTR_ERR(cdev);
  419. dev_err(df->dev.parent,
  420. "Failed to register devfreq cooling device (%d)\n",
  421. err);
  422. goto release_idr;
  423. }
  424. dfc->cdev = cdev;
  425. return cdev;
  426. release_idr:
  427. release_idr(&devfreq_idr, dfc->id);
  428. free_tables:
  429. kfree(dfc->power_table);
  430. kfree(dfc->freq_table);
  431. free_dfc:
  432. kfree(dfc);
  433. return ERR_PTR(err);
  434. }
  435. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
  436. /**
  437. * of_devfreq_cooling_register() - Register devfreq cooling device,
  438. * with OF information.
  439. * @np: Pointer to OF device_node.
  440. * @df: Pointer to devfreq device.
  441. */
  442. struct thermal_cooling_device *
  443. of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
  444. {
  445. return of_devfreq_cooling_register_power(np, df, NULL);
  446. }
  447. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
  448. /**
  449. * devfreq_cooling_register() - Register devfreq cooling device.
  450. * @df: Pointer to devfreq device.
  451. */
  452. struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
  453. {
  454. return of_devfreq_cooling_register(NULL, df);
  455. }
  456. EXPORT_SYMBOL_GPL(devfreq_cooling_register);
  457. /**
  458. * devfreq_cooling_unregister() - Unregister devfreq cooling device.
  459. * @dfc: Pointer to devfreq cooling device to unregister.
  460. */
  461. void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
  462. {
  463. struct devfreq_cooling_device *dfc;
  464. if (!cdev)
  465. return;
  466. dfc = cdev->devdata;
  467. thermal_cooling_device_unregister(dfc->cdev);
  468. release_idr(&devfreq_idr, dfc->id);
  469. kfree(dfc->power_table);
  470. kfree(dfc->freq_table);
  471. kfree(dfc);
  472. }
  473. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);