devfreq_cooling.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/energy_model.h>
  15. #include <linux/export.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 <linux/units.h>
  21. #include "thermal_trace.h"
  22. #define SCALE_ERROR_MITIGATION 100
  23. /**
  24. * struct devfreq_cooling_device - Devfreq cooling device
  25. * devfreq_cooling_device registered.
  26. * @cdev: Pointer to associated thermal cooling device.
  27. * @cooling_ops: devfreq callbacks to thermal cooling device ops
  28. * @devfreq: Pointer to associated devfreq device.
  29. * @cooling_state: Current cooling state.
  30. * @freq_table: Pointer to a table with the frequencies sorted in descending
  31. * order. You can index the table by cooling device state
  32. * @max_state: It is the last index, that is, one less than the number of the
  33. * OPPs
  34. * @power_ops: Pointer to devfreq_cooling_power, a more precised model.
  35. * @res_util: Resource utilization scaling factor for the power.
  36. * It is multiplied by 100 to minimize the error. It is used
  37. * for estimation of the power budget instead of using
  38. * 'utilization' (which is 'busy_time' / 'total_time').
  39. * The 'res_util' range is from 100 to power * 100 for the
  40. * corresponding 'state'.
  41. * @capped_state: index to cooling state with in dynamic power budget
  42. * @req_max_freq: PM QoS request for limiting the maximum frequency
  43. * of the devfreq device.
  44. * @em_pd: Energy Model for the associated Devfreq device
  45. */
  46. struct devfreq_cooling_device {
  47. struct thermal_cooling_device *cdev;
  48. struct thermal_cooling_device_ops cooling_ops;
  49. struct devfreq *devfreq;
  50. unsigned long cooling_state;
  51. u32 *freq_table;
  52. size_t max_state;
  53. struct devfreq_cooling_power *power_ops;
  54. u32 res_util;
  55. int capped_state;
  56. struct dev_pm_qos_request req_max_freq;
  57. struct em_perf_domain *em_pd;
  58. };
  59. static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
  60. unsigned long *state)
  61. {
  62. struct devfreq_cooling_device *dfc = cdev->devdata;
  63. *state = dfc->max_state;
  64. return 0;
  65. }
  66. static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
  67. unsigned long *state)
  68. {
  69. struct devfreq_cooling_device *dfc = cdev->devdata;
  70. *state = dfc->cooling_state;
  71. return 0;
  72. }
  73. static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
  74. unsigned long state)
  75. {
  76. struct devfreq_cooling_device *dfc = cdev->devdata;
  77. struct devfreq *df = dfc->devfreq;
  78. struct device *dev = df->dev.parent;
  79. struct em_perf_state *table;
  80. unsigned long freq;
  81. int perf_idx;
  82. if (state == dfc->cooling_state)
  83. return 0;
  84. dev_dbg(dev, "Setting cooling state %lu\n", state);
  85. if (state > dfc->max_state)
  86. return -EINVAL;
  87. if (dfc->em_pd) {
  88. perf_idx = dfc->max_state - state;
  89. rcu_read_lock();
  90. table = em_perf_state_from_pd(dfc->em_pd);
  91. freq = table[perf_idx].frequency * 1000;
  92. rcu_read_unlock();
  93. } else {
  94. freq = dfc->freq_table[state];
  95. }
  96. dev_pm_qos_update_request(&dfc->req_max_freq,
  97. DIV_ROUND_UP(freq, HZ_PER_KHZ));
  98. dfc->cooling_state = state;
  99. return 0;
  100. }
  101. /**
  102. * get_perf_idx() - get the performance index corresponding to a frequency
  103. * @em_pd: Pointer to device's Energy Model
  104. * @freq: frequency in kHz
  105. *
  106. * Return: the performance index associated with the @freq, or
  107. * -EINVAL if it wasn't found.
  108. */
  109. static int get_perf_idx(struct em_perf_domain *em_pd, unsigned long freq)
  110. {
  111. struct em_perf_state *table;
  112. int i, idx = -EINVAL;
  113. rcu_read_lock();
  114. table = em_perf_state_from_pd(em_pd);
  115. for (i = 0; i < em_pd->nr_perf_states; i++) {
  116. if (table[i].frequency != freq)
  117. continue;
  118. idx = i;
  119. break;
  120. }
  121. rcu_read_unlock();
  122. return idx;
  123. }
  124. static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
  125. {
  126. struct device *dev = df->dev.parent;
  127. unsigned long voltage;
  128. struct dev_pm_opp *opp;
  129. opp = dev_pm_opp_find_freq_exact(dev, freq, true);
  130. if (PTR_ERR(opp) == -ERANGE)
  131. opp = dev_pm_opp_find_freq_exact(dev, freq, false);
  132. if (IS_ERR(opp)) {
  133. dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
  134. freq, PTR_ERR(opp));
  135. return 0;
  136. }
  137. voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
  138. dev_pm_opp_put(opp);
  139. if (voltage == 0) {
  140. dev_err_ratelimited(dev,
  141. "Failed to get voltage for frequency %lu\n",
  142. freq);
  143. }
  144. return voltage;
  145. }
  146. static void _normalize_load(struct devfreq_dev_status *status)
  147. {
  148. if (status->total_time > 0xfffff) {
  149. status->total_time >>= 10;
  150. status->busy_time >>= 10;
  151. }
  152. status->busy_time <<= 10;
  153. status->busy_time /= status->total_time ? : 1;
  154. status->busy_time = status->busy_time ? : 1;
  155. status->total_time = 1024;
  156. }
  157. static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
  158. u32 *power)
  159. {
  160. struct devfreq_cooling_device *dfc = cdev->devdata;
  161. struct devfreq *df = dfc->devfreq;
  162. struct devfreq_dev_status status;
  163. struct em_perf_state *table;
  164. unsigned long state;
  165. unsigned long freq;
  166. unsigned long voltage;
  167. int res, perf_idx;
  168. mutex_lock(&df->lock);
  169. status = df->last_status;
  170. mutex_unlock(&df->lock);
  171. freq = status.current_frequency;
  172. if (dfc->power_ops && dfc->power_ops->get_real_power) {
  173. voltage = get_voltage(df, freq);
  174. if (voltage == 0) {
  175. res = -EINVAL;
  176. goto fail;
  177. }
  178. res = dfc->power_ops->get_real_power(df, power, freq, voltage);
  179. if (!res) {
  180. state = dfc->max_state - dfc->capped_state;
  181. /* Convert EM power into milli-Watts first */
  182. rcu_read_lock();
  183. table = em_perf_state_from_pd(dfc->em_pd);
  184. dfc->res_util = table[state].power;
  185. rcu_read_unlock();
  186. dfc->res_util /= MICROWATT_PER_MILLIWATT;
  187. dfc->res_util *= SCALE_ERROR_MITIGATION;
  188. if (*power > 1)
  189. dfc->res_util /= *power;
  190. } else {
  191. goto fail;
  192. }
  193. } else {
  194. /* Energy Model frequencies are in kHz */
  195. perf_idx = get_perf_idx(dfc->em_pd, freq / 1000);
  196. if (perf_idx < 0) {
  197. res = -EAGAIN;
  198. goto fail;
  199. }
  200. _normalize_load(&status);
  201. /* Convert EM power into milli-Watts first */
  202. rcu_read_lock();
  203. table = em_perf_state_from_pd(dfc->em_pd);
  204. *power = table[perf_idx].power;
  205. rcu_read_unlock();
  206. *power /= MICROWATT_PER_MILLIWATT;
  207. /* Scale power for utilization */
  208. *power *= status.busy_time;
  209. *power >>= 10;
  210. }
  211. trace_thermal_power_devfreq_get_power(cdev, &status, freq, *power);
  212. return 0;
  213. fail:
  214. /* It is safe to set max in this case */
  215. dfc->res_util = SCALE_ERROR_MITIGATION;
  216. return res;
  217. }
  218. static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
  219. unsigned long state, u32 *power)
  220. {
  221. struct devfreq_cooling_device *dfc = cdev->devdata;
  222. struct em_perf_state *table;
  223. int perf_idx;
  224. if (state > dfc->max_state)
  225. return -EINVAL;
  226. perf_idx = dfc->max_state - state;
  227. rcu_read_lock();
  228. table = em_perf_state_from_pd(dfc->em_pd);
  229. *power = table[perf_idx].power;
  230. rcu_read_unlock();
  231. *power /= MICROWATT_PER_MILLIWATT;
  232. return 0;
  233. }
  234. static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
  235. u32 power, unsigned long *state)
  236. {
  237. struct devfreq_cooling_device *dfc = cdev->devdata;
  238. struct devfreq *df = dfc->devfreq;
  239. struct devfreq_dev_status status;
  240. unsigned long freq, em_power_mw;
  241. struct em_perf_state *table;
  242. s32 est_power;
  243. int i;
  244. mutex_lock(&df->lock);
  245. status = df->last_status;
  246. mutex_unlock(&df->lock);
  247. freq = status.current_frequency;
  248. if (dfc->power_ops && dfc->power_ops->get_real_power) {
  249. /* Scale for resource utilization */
  250. est_power = power * dfc->res_util;
  251. est_power /= SCALE_ERROR_MITIGATION;
  252. } else {
  253. /* Scale dynamic power for utilization */
  254. _normalize_load(&status);
  255. est_power = power << 10;
  256. est_power /= status.busy_time;
  257. }
  258. /*
  259. * Find the first cooling state that is within the power
  260. * budget. The EM power table is sorted ascending.
  261. */
  262. rcu_read_lock();
  263. table = em_perf_state_from_pd(dfc->em_pd);
  264. for (i = dfc->max_state; i > 0; i--) {
  265. /* Convert EM power to milli-Watts to make safe comparison */
  266. em_power_mw = table[i].power;
  267. em_power_mw /= MICROWATT_PER_MILLIWATT;
  268. if (est_power >= em_power_mw)
  269. break;
  270. }
  271. rcu_read_unlock();
  272. *state = dfc->max_state - i;
  273. dfc->capped_state = *state;
  274. trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
  275. return 0;
  276. }
  277. /**
  278. * devfreq_cooling_gen_tables() - Generate frequency table.
  279. * @dfc: Pointer to devfreq cooling device.
  280. * @num_opps: Number of OPPs
  281. *
  282. * Generate frequency table which holds the frequencies in descending
  283. * order. That way its indexed by cooling device state. This is for
  284. * compatibility with drivers which do not register Energy Model.
  285. *
  286. * Return: 0 on success, negative error code on failure.
  287. */
  288. static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc,
  289. int num_opps)
  290. {
  291. struct devfreq *df = dfc->devfreq;
  292. struct device *dev = df->dev.parent;
  293. unsigned long freq;
  294. int i;
  295. dfc->freq_table = kcalloc(num_opps, sizeof(*dfc->freq_table),
  296. GFP_KERNEL);
  297. if (!dfc->freq_table)
  298. return -ENOMEM;
  299. for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
  300. struct dev_pm_opp *opp;
  301. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  302. if (IS_ERR(opp)) {
  303. kfree(dfc->freq_table);
  304. return PTR_ERR(opp);
  305. }
  306. dev_pm_opp_put(opp);
  307. dfc->freq_table[i] = freq;
  308. }
  309. return 0;
  310. }
  311. /**
  312. * of_devfreq_cooling_register_power() - Register devfreq cooling device,
  313. * with OF and power information.
  314. * @np: Pointer to OF device_node.
  315. * @df: Pointer to devfreq device.
  316. * @dfc_power: Pointer to devfreq_cooling_power.
  317. *
  318. * Register a devfreq cooling device. The available OPPs must be
  319. * registered on the device.
  320. *
  321. * If @dfc_power is provided, the cooling device is registered with the
  322. * power extensions. For the power extensions to work correctly,
  323. * devfreq should use the simple_ondemand governor, other governors
  324. * are not currently supported.
  325. */
  326. struct thermal_cooling_device *
  327. of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
  328. struct devfreq_cooling_power *dfc_power)
  329. {
  330. struct thermal_cooling_device *cdev;
  331. struct device *dev = df->dev.parent;
  332. struct devfreq_cooling_device *dfc;
  333. struct em_perf_domain *em;
  334. struct thermal_cooling_device_ops *ops;
  335. char *name;
  336. int err, num_opps;
  337. dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
  338. if (!dfc)
  339. return ERR_PTR(-ENOMEM);
  340. dfc->devfreq = df;
  341. ops = &dfc->cooling_ops;
  342. ops->get_max_state = devfreq_cooling_get_max_state;
  343. ops->get_cur_state = devfreq_cooling_get_cur_state;
  344. ops->set_cur_state = devfreq_cooling_set_cur_state;
  345. em = em_pd_get(dev);
  346. if (em && !em_is_artificial(em)) {
  347. dfc->em_pd = em;
  348. ops->get_requested_power =
  349. devfreq_cooling_get_requested_power;
  350. ops->state2power = devfreq_cooling_state2power;
  351. ops->power2state = devfreq_cooling_power2state;
  352. dfc->power_ops = dfc_power;
  353. num_opps = em_pd_nr_perf_states(dfc->em_pd);
  354. } else {
  355. /* Backward compatibility for drivers which do not use IPA */
  356. dev_dbg(dev, "missing proper EM for cooling device\n");
  357. num_opps = dev_pm_opp_get_opp_count(dev);
  358. err = devfreq_cooling_gen_tables(dfc, num_opps);
  359. if (err)
  360. goto free_dfc;
  361. }
  362. if (num_opps <= 0) {
  363. err = -EINVAL;
  364. goto free_dfc;
  365. }
  366. /* max_state is an index, not a counter */
  367. dfc->max_state = num_opps - 1;
  368. err = dev_pm_qos_add_request(dev, &dfc->req_max_freq,
  369. DEV_PM_QOS_MAX_FREQUENCY,
  370. PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
  371. if (err < 0)
  372. goto free_table;
  373. err = -ENOMEM;
  374. name = kasprintf(GFP_KERNEL, "devfreq-%s", dev_name(dev));
  375. if (!name)
  376. goto remove_qos_req;
  377. cdev = thermal_of_cooling_device_register(np, name, dfc, ops);
  378. kfree(name);
  379. if (IS_ERR(cdev)) {
  380. err = PTR_ERR(cdev);
  381. dev_err(dev,
  382. "Failed to register devfreq cooling device (%d)\n",
  383. err);
  384. goto remove_qos_req;
  385. }
  386. dfc->cdev = cdev;
  387. return cdev;
  388. remove_qos_req:
  389. dev_pm_qos_remove_request(&dfc->req_max_freq);
  390. free_table:
  391. kfree(dfc->freq_table);
  392. free_dfc:
  393. kfree(dfc);
  394. return ERR_PTR(err);
  395. }
  396. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
  397. /**
  398. * of_devfreq_cooling_register() - Register devfreq cooling device,
  399. * with OF information.
  400. * @np: Pointer to OF device_node.
  401. * @df: Pointer to devfreq device.
  402. */
  403. struct thermal_cooling_device *
  404. of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
  405. {
  406. return of_devfreq_cooling_register_power(np, df, NULL);
  407. }
  408. EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
  409. /**
  410. * devfreq_cooling_register() - Register devfreq cooling device.
  411. * @df: Pointer to devfreq device.
  412. */
  413. struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
  414. {
  415. return of_devfreq_cooling_register(NULL, df);
  416. }
  417. EXPORT_SYMBOL_GPL(devfreq_cooling_register);
  418. /**
  419. * devfreq_cooling_em_register() - Register devfreq cooling device with
  420. * power information and automatically register Energy Model (EM)
  421. * @df: Pointer to devfreq device.
  422. * @dfc_power: Pointer to devfreq_cooling_power.
  423. *
  424. * Register a devfreq cooling device and automatically register EM. The
  425. * available OPPs must be registered for the device.
  426. *
  427. * If @dfc_power is provided, the cooling device is registered with the
  428. * power extensions. It is using the simple Energy Model which requires
  429. * "dynamic-power-coefficient" a devicetree property. To not break drivers
  430. * which miss that DT property, the function won't bail out when the EM
  431. * registration failed. The cooling device will be registered if everything
  432. * else is OK.
  433. */
  434. struct thermal_cooling_device *
  435. devfreq_cooling_em_register(struct devfreq *df,
  436. struct devfreq_cooling_power *dfc_power)
  437. {
  438. struct thermal_cooling_device *cdev;
  439. struct device *dev;
  440. int ret;
  441. if (IS_ERR_OR_NULL(df))
  442. return ERR_PTR(-EINVAL);
  443. dev = df->dev.parent;
  444. ret = dev_pm_opp_of_register_em(dev, NULL);
  445. if (ret)
  446. dev_dbg(dev, "Unable to register EM for devfreq cooling device (%d)\n",
  447. ret);
  448. cdev = of_devfreq_cooling_register_power(dev->of_node, df, dfc_power);
  449. if (IS_ERR_OR_NULL(cdev))
  450. em_dev_unregister_perf_domain(dev);
  451. return cdev;
  452. }
  453. EXPORT_SYMBOL_GPL(devfreq_cooling_em_register);
  454. /**
  455. * devfreq_cooling_unregister() - Unregister devfreq cooling device.
  456. * @cdev: Pointer to devfreq cooling device to unregister.
  457. *
  458. * Unregisters devfreq cooling device and related Energy Model if it was
  459. * present.
  460. */
  461. void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
  462. {
  463. struct devfreq_cooling_device *dfc;
  464. struct device *dev;
  465. if (IS_ERR_OR_NULL(cdev))
  466. return;
  467. dfc = cdev->devdata;
  468. dev = dfc->devfreq->dev.parent;
  469. thermal_cooling_device_unregister(dfc->cdev);
  470. dev_pm_qos_remove_request(&dfc->req_max_freq);
  471. em_dev_unregister_perf_domain(dev);
  472. kfree(dfc->freq_table);
  473. kfree(dfc);
  474. }
  475. EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);