devfreq_cooling.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * devfreq_cooling: Thermal cooling device implementation for devices using
  4. * devfreq
  5. *
  6. * Copyright (C) 2014-2015 ARM Limited
  7. *
  8. * TODO:
  9. * - If OPPs are added or removed after devfreq cooling has
  10. * registered, the devfreq cooling won't react to it.
  11. */
  12. #include <linux/devfreq.h>
  13. #include <linux/devfreq_cooling.h>
  14. #include <linux/export.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/pm_opp.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/thermal.h>
  20. #include <trace/events/thermal.h>
  21. #define HZ_PER_KHZ 1000
  22. #define SCALE_ERROR_MITIGATION 100
  23. static DEFINE_IDA(devfreq_ida);
  24. /**
  25. * struct devfreq_cooling_device - Devfreq cooling device
  26. * @id: unique integer value corresponding to each
  27. * devfreq_cooling_device registered.
  28. * @cdev: Pointer to associated thermal cooling device.
  29. * @devfreq: Pointer to associated devfreq device.
  30. * @cooling_state: Current cooling state.
  31. * @power_table: Pointer to table with maximum power draw for each
  32. * cooling state. State is the index into the table, and
  33. * the power is in mW.
  34. * @freq_table: Pointer to a table with the frequencies sorted in descending
  35. * order. You can index the table by cooling device state
  36. * @freq_table_size: Size of the @freq_table and @power_table
  37. * @power_ops: Pointer to devfreq_cooling_power, used to generate the
  38. * @power_table.
  39. * @res_util: Resource utilization scaling factor for the power.
  40. * It is multiplied by 100 to minimize the error. It is used
  41. * for estimation of the power budget instead of using
  42. * 'utilization' (which is 'busy_time / 'total_time').
  43. * The 'res_util' range is from 100 to (power_table[state] * 100)
  44. * for the corresponding 'state'.
  45. * @capped_state: index to cooling state with in dynamic power budget
  46. * @req_max_freq: PM QoS request for limiting the maximum frequency
  47. * of the devfreq device.
  48. */
  49. struct devfreq_cooling_device {
  50. int id;
  51. struct thermal_cooling_device *cdev;
  52. struct devfreq *devfreq;
  53. unsigned long cooling_state;
  54. u32 *power_table;
  55. u32 *freq_table;
  56. size_t freq_table_size;
  57. struct devfreq_cooling_power *power_ops;
  58. u32 res_util;
  59. int capped_state;
  60. struct dev_pm_qos_request req_max_freq;
  61. };
  62. static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
  63. unsigned long *state)
  64. {
  65. struct devfreq_cooling_device *dfc = cdev->devdata;
  66. *state = dfc->freq_table_size - 1;
  67. return 0;
  68. }
  69. static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  70. unsigned long *state)
  71. {
  72. struct devfreq_cooling_device *dfc = cdev->devdata;
  73. *state = dfc->cooling_state;
  74. return 0;
  75. }
  76. static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  77. unsigned long state)
  78. {
  79. struct devfreq_cooling_device *dfc = cdev->devdata;
  80. struct devfreq *df = dfc->devfreq;
  81. struct device *dev = df->dev.parent;
  82. unsigned long freq;
  83. if (state == dfc->cooling_state)
  84. return 0;
  85. dev_dbg(dev, "Setting cooling state %lu\n", state);
  86. if (state >= dfc->freq_table_size)
  87. return -EINVAL;
  88. freq = dfc->freq_table[state];
  89. dev_pm_qos_update_request(&dfc->req_max_freq,
  90. DIV_ROUND_UP(freq, HZ_PER_KHZ));
  91. dfc->cooling_state = state;
  92. return 0;
  93. }
  94. /**
  95. * freq_get_state() - get the cooling state corresponding to a frequency
  96. * @dfc: Pointer to devfreq cooling device
  97. * @freq: frequency in Hz
  98. *
  99. * Return: the cooling state associated with the @freq, or
  100. * THERMAL_CSTATE_INVALID if it wasn't found.
  101. */
  102. static unsigned long
  103. freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
  104. {
  105. int i;
  106. for (i = 0; i < dfc->freq_table_size; i++) {
  107. if (dfc->freq_table[i] == freq)
  108. return i;
  109. }
  110. return THERMAL_CSTATE_INVALID;
  111. }
  112. static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
  113. {
  114. struct device *dev = df->dev.parent;
  115. unsigned long voltage;
  116. struct dev_pm_opp *opp;
  117. opp = dev_pm_opp_find_freq_exact(dev, freq, true);
  118. if (PTR_ERR(opp) == -ERANGE)
  119. opp = dev_pm_opp_find_freq_exact(dev, freq, false);
  120. if (IS_ERR(opp)) {
  121. dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
  122. freq, PTR_ERR(opp));
  123. return 0;
  124. }
  125. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  126. dev_pm_opp_put(opp);
  127. if (voltage == 0) {
  128. dev_err_ratelimited(dev,
  129. "Failed to get voltage for frequency %lu\n",
  130. freq);
  131. }
  132. return voltage;
  133. }
  134. /**
  135. * get_static_power() - calculate the static power
  136. * @dfc: Pointer to devfreq cooling device
  137. * @freq: Frequency in Hz
  138. *
  139. * Calculate the static power in milliwatts using the supplied
  140. * get_static_power(). The current voltage is calculated using the
  141. * OPP library. If no get_static_power() was supplied, assume the
  142. * static power is negligible.
  143. */
  144. static unsigned long
  145. get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
  146. {
  147. struct devfreq *df = dfc->devfreq;
  148. unsigned long voltage;
  149. if (!dfc->power_ops->get_static_power)
  150. return 0;
  151. voltage = get_voltage(df, freq);
  152. if (voltage == 0)
  153. return 0;
  154. return dfc->power_ops->get_static_power(df, voltage);
  155. }
  156. /**
  157. * get_dynamic_power - calculate the dynamic power
  158. * @dfc: Pointer to devfreq cooling device
  159. * @freq: Frequency in Hz
  160. * @voltage: Voltage in millivolts
  161. *
  162. * Calculate the dynamic power in milliwatts consumed by the device at
  163. * frequency @freq and voltage @voltage. If the get_dynamic_power()
  164. * was supplied as part of the devfreq_cooling_power struct, then that
  165. * function is used. Otherwise, a simple power model (Pdyn = Coeff *
  166. * Voltage^2 * Frequency) is used.
  167. */
  168. static unsigned long
  169. get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
  170. unsigned long voltage)
  171. {
  172. u64 power;
  173. u32 freq_mhz;
  174. struct devfreq_cooling_power *dfc_power = dfc->power_ops;
  175. if (dfc_power->get_dynamic_power)
  176. return dfc_power->get_dynamic_power(dfc->devfreq, freq,
  177. voltage);
  178. freq_mhz = freq / 1000000;
  179. power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
  180. do_div(power, 1000000000);
  181. return power;
  182. }
  183. static inline unsigned long get_total_power(struct devfreq_cooling_device *dfc,
  184. unsigned long freq,
  185. unsigned long voltage)
  186. {
  187. return get_static_power(dfc, freq) + get_dynamic_power(dfc, freq,
  188. voltage);
  189. }
  190. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  191. u32 *power)
  192. {
  193. struct devfreq_cooling_device *dfc = cdev->devdata;
  194. struct devfreq *df = dfc->devfreq;
  195. struct devfreq_dev_status *status = &df->last_status;
  196. unsigned long state;
  197. unsigned long freq = status->current_frequency;
  198. unsigned long voltage;
  199. u32 dyn_power = 0;
  200. u32 static_power = 0;
  201. int res;
  202. state = freq_get_state(dfc, freq);
  203. if (state == THERMAL_CSTATE_INVALID) {
  204. res = -EAGAIN;
  205. goto fail;
  206. }
  207. if (dfc->power_ops->get_real_power) {
  208. voltage = get_voltage(df, freq);
  209. if (voltage == 0) {
  210. res = -EINVAL;
  211. goto fail;
  212. }
  213. res = dfc->power_ops->get_real_power(df, power, freq, voltage);
  214. if (!res) {
  215. state = dfc->capped_state;
  216. dfc->res_util = dfc->power_table[state];
  217. dfc->res_util *= SCALE_ERROR_MITIGATION;
  218. if (*power > 1)
  219. dfc->res_util /= *power;
  220. } else {
  221. goto fail;
  222. }
  223. } else {
  224. dyn_power = dfc->power_table[state];
  225. /* Scale dynamic power for utilization */
  226. dyn_power *= status->busy_time;
  227. dyn_power /= status->total_time;
  228. /* Get static power */
  229. static_power = get_static_power(dfc, freq);
  230. *power = dyn_power + static_power;
  231. }
  232. trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
  233. static_power, *power);
  234. return 0;
  235. fail:
  236. /* It is safe to set max in this case */
  237. dfc->res_util = SCALE_ERROR_MITIGATION;
  238. return res;
  239. }
  240. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  241. unsigned long state,
  242. u32 *power)
  243. {
  244. struct devfreq_cooling_device *dfc = cdev->devdata;
  245. unsigned long freq;
  246. u32 static_power;
  247. if (state >= dfc->freq_table_size)
  248. return -EINVAL;
  249. freq = dfc->freq_table[state];
  250. static_power = get_static_power(dfc, freq);
  251. *power = dfc->power_table[state] + static_power;
  252. return 0;
  253. }
  254. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  255. u32 power, unsigned long *state)
  256. {
  257. struct devfreq_cooling_device *dfc = cdev->devdata;
  258. struct devfreq *df = dfc->devfreq;
  259. struct devfreq_dev_status *status = &df->last_status;
  260. unsigned long freq = status->current_frequency;
  261. unsigned long busy_time;
  262. s32 dyn_power;
  263. u32 static_power;
  264. s32 est_power;
  265. int i;
  266. if (dfc->power_ops->get_real_power) {
  267. /* Scale for resource utilization */
  268. est_power = power * dfc->res_util;
  269. est_power /= SCALE_ERROR_MITIGATION;
  270. } else {
  271. static_power = get_static_power(dfc, freq);
  272. dyn_power = power - static_power;
  273. dyn_power = dyn_power > 0 ? dyn_power : 0;
  274. /* Scale dynamic power for utilization */
  275. busy_time = status->busy_time ?: 1;
  276. est_power = (dyn_power * status->total_time) / busy_time;
  277. }
  278. /*
  279. * Find the first cooling state that is within the power
  280. * budget for dynamic power.
  281. */
  282. for (i = 0; i < dfc->freq_table_size - 1; i++)
  283. if (est_power >= dfc->power_table[i])
  284. break;
  285. *state = i;
  286. dfc->capped_state = i;
  287. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  288. return 0;
  289. }
  290. static struct thermal_cooling_device_ops devfreq_cooling_ops = {
  291. .get_max_state = devfreq_cooling_get_max_state,
  292. .get_cur_state = devfreq_cooling_get_cur_state,
  293. .set_cur_state = devfreq_cooling_set_cur_state,
  294. };
  295. /**
  296. * devfreq_cooling_gen_tables() - Generate power and freq tables.
  297. * @dfc: Pointer to devfreq cooling device.
  298. *
  299. * Generate power and frequency tables: the power table hold the
  300. * device's maximum power usage at each cooling state (OPP). The
  301. * static and dynamic power using the appropriate voltage and
  302. * frequency for the state, is acquired from the struct
  303. * devfreq_cooling_power, and summed to make the maximum power draw.
  304. *
  305. * The frequency table holds the frequencies in descending order.
  306. * That way its indexed by cooling device state.
  307. *
  308. * The tables are malloced, and pointers put in dfc. They must be
  309. * freed when unregistering the devfreq cooling device.
  310. *
  311. * Return: 0 on success, negative error code on failure.
  312. */
  313. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
  314. {
  315. struct devfreq *df = dfc->devfreq;
  316. struct device *dev = df->dev.parent;
  317. int ret, num_opps;
  318. unsigned long freq;
  319. u32 *power_table = NULL;
  320. u32 *freq_table;
  321. int i;
  322. num_opps = dev_pm_opp_get_opp_count(dev);
  323. if (dfc->power_ops) {
  324. power_table = kcalloc(num_opps, sizeof(*power_table),
  325. GFP_KERNEL);
  326. if (!power_table)
  327. return -ENOMEM;
  328. }
  329. freq_table = kcalloc(num_opps, sizeof(*freq_table),
  330. GFP_KERNEL);
  331. if (!freq_table) {
  332. ret = -ENOMEM;
  333. goto free_power_table;
  334. }
  335. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  336. unsigned long power, voltage;
  337. struct dev_pm_opp *opp;
  338. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  339. if (IS_ERR(opp)) {
  340. ret = PTR_ERR(opp);
  341. goto free_tables;
  342. }
  343. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  344. dev_pm_opp_put(opp);
  345. if (dfc->power_ops) {
  346. if (dfc->power_ops->get_real_power)
  347. power = get_total_power(dfc, freq, voltage);
  348. else
  349. power = get_dynamic_power(dfc, freq, voltage);
  350. dev_dbg(dev, "Power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
  351. freq / 1000000, voltage, power, power);
  352. power_table[i] = power;
  353. }
  354. freq_table[i] = freq;
  355. }
  356. if (dfc->power_ops)
  357. dfc->power_table = power_table;
  358. dfc->freq_table = freq_table;
  359. dfc->freq_table_size = num_opps;
  360. return 0;
  361. free_tables:
  362. kfree(freq_table);
  363. free_power_table:
  364. kfree(power_table);
  365. return ret;
  366. }
  367. /**
  368. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  369. * with OF and power information.
  370. * @np: Pointer to OF device_node.
  371. * @df: Pointer to devfreq device.
  372. * @dfc_power: Pointer to devfreq_cooling_power.
  373. *
  374. * Register a devfreq cooling device. The available OPPs must be
  375. * registered on the device.
  376. *
  377. * If @dfc_power is provided, the cooling device is registered with the
  378. * power extensions. For the power extensions to work correctly,
  379. * devfreq should use the simple_ondemand governor, other governors
  380. * are not currently supported.
  381. */
  382. struct thermal_cooling_device *
  383. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  384. struct devfreq_cooling_power *dfc_power)
  385. {
  386. struct thermal_cooling_device *cdev;
  387. struct devfreq_cooling_device *dfc;
  388. char dev_name[THERMAL_NAME_LENGTH];
  389. int err;
  390. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  391. if (!dfc)
  392. return ERR_PTR(-ENOMEM);
  393. dfc->devfreq = df;
  394. if (dfc_power) {
  395. dfc->power_ops = dfc_power;
  396. devfreq_cooling_ops.get_requested_power =
  397. devfreq_cooling_get_requested_power;
  398. devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
  399. devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
  400. }
  401. err = devfreq_cooling_gen_tables(dfc);
  402. if (err)
  403. goto free_dfc;
  404. err = dev_pm_qos_add_request(df->dev.parent, &dfc->req_max_freq,
  405. DEV_PM_QOS_MAX_FREQUENCY,
  406. PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
  407. if (err < 0)
  408. goto free_tables;
  409. err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
  410. if (err < 0)
  411. goto remove_qos_req;
  412. dfc->id = err;
  413. snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
  414. cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
  415. &devfreq_cooling_ops);
  416. if (IS_ERR(cdev)) {
  417. err = PTR_ERR(cdev);
  418. dev_err(df->dev.parent,
  419. "Failed to register devfreq cooling device (%d)\n",
  420. err);
  421. goto release_ida;
  422. }
  423. dfc->cdev = cdev;
  424. return cdev;
  425. release_ida:
  426. ida_simple_remove(&devfreq_ida, dfc->id);
  427. remove_qos_req:
  428. dev_pm_qos_remove_request(&dfc->req_max_freq);
  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. * @cdev: 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. ida_simple_remove(&devfreq_ida, dfc->id);
  470. dev_pm_qos_remove_request(&dfc->req_max_freq);
  471. kfree(dfc->power_table);
  472. kfree(dfc->freq_table);
  473. kfree(dfc);
  474. }
  475. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);