devfreq_cooling.c 14 KB

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