panthor_devfreq.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2019 Collabora ltd. */
  3. #include <linux/clk.h>
  4. #include <linux/devfreq.h>
  5. #include <linux/devfreq_cooling.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/pm_opp.h>
  8. #include <drm/drm_managed.h>
  9. #include "panthor_devfreq.h"
  10. #include "panthor_device.h"
  11. /**
  12. * struct panthor_devfreq - Device frequency management
  13. */
  14. struct panthor_devfreq {
  15. /** @devfreq: devfreq device. */
  16. struct devfreq *devfreq;
  17. /** @gov_data: Governor data. */
  18. struct devfreq_simple_ondemand_data gov_data;
  19. /** @busy_time: Busy time. */
  20. ktime_t busy_time;
  21. /** @idle_time: Idle time. */
  22. ktime_t idle_time;
  23. /** @time_last_update: Last update time. */
  24. ktime_t time_last_update;
  25. /** @last_busy_state: True if the GPU was busy last time we updated the state. */
  26. bool last_busy_state;
  27. /**
  28. * @lock: Lock used to protect busy_time, idle_time, time_last_update and
  29. * last_busy_state.
  30. *
  31. * These fields can be accessed concurrently by panthor_devfreq_get_dev_status()
  32. * and panthor_devfreq_record_{busy,idle}().
  33. */
  34. spinlock_t lock;
  35. };
  36. static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq)
  37. {
  38. ktime_t now, last;
  39. now = ktime_get();
  40. last = pdevfreq->time_last_update;
  41. if (pdevfreq->last_busy_state)
  42. pdevfreq->busy_time += ktime_sub(now, last);
  43. else
  44. pdevfreq->idle_time += ktime_sub(now, last);
  45. pdevfreq->time_last_update = now;
  46. }
  47. static int panthor_devfreq_target(struct device *dev, unsigned long *freq,
  48. u32 flags)
  49. {
  50. struct panthor_device *ptdev = dev_get_drvdata(dev);
  51. struct dev_pm_opp *opp;
  52. int err;
  53. opp = devfreq_recommended_opp(dev, freq, flags);
  54. if (IS_ERR(opp))
  55. return PTR_ERR(opp);
  56. dev_pm_opp_put(opp);
  57. err = dev_pm_opp_set_rate(dev, *freq);
  58. if (!err)
  59. ptdev->current_frequency = *freq;
  60. return err;
  61. }
  62. static void panthor_devfreq_reset(struct panthor_devfreq *pdevfreq)
  63. {
  64. pdevfreq->busy_time = 0;
  65. pdevfreq->idle_time = 0;
  66. pdevfreq->time_last_update = ktime_get();
  67. }
  68. static int panthor_devfreq_get_dev_status(struct device *dev,
  69. struct devfreq_dev_status *status)
  70. {
  71. struct panthor_device *ptdev = dev_get_drvdata(dev);
  72. struct panthor_devfreq *pdevfreq = ptdev->devfreq;
  73. unsigned long irqflags;
  74. status->current_frequency = clk_get_rate(ptdev->clks.core);
  75. spin_lock_irqsave(&pdevfreq->lock, irqflags);
  76. panthor_devfreq_update_utilization(pdevfreq);
  77. status->total_time = ktime_to_ns(ktime_add(pdevfreq->busy_time,
  78. pdevfreq->idle_time));
  79. status->busy_time = ktime_to_ns(pdevfreq->busy_time);
  80. panthor_devfreq_reset(pdevfreq);
  81. spin_unlock_irqrestore(&pdevfreq->lock, irqflags);
  82. drm_dbg(&ptdev->base, "busy %lu total %lu %lu %% freq %lu MHz\n",
  83. status->busy_time, status->total_time,
  84. status->busy_time / (status->total_time / 100),
  85. status->current_frequency / 1000 / 1000);
  86. return 0;
  87. }
  88. static struct devfreq_dev_profile panthor_devfreq_profile = {
  89. .timer = DEVFREQ_TIMER_DELAYED,
  90. .polling_ms = 50, /* ~3 frames */
  91. .target = panthor_devfreq_target,
  92. .get_dev_status = panthor_devfreq_get_dev_status,
  93. };
  94. int panthor_devfreq_init(struct panthor_device *ptdev)
  95. {
  96. /* There's actually 2 regulators (mali and sram), but the OPP core only
  97. * supports one.
  98. *
  99. * We assume the sram regulator is coupled with the mali one and let
  100. * the coupling logic deal with voltage updates.
  101. */
  102. static const char * const reg_names[] = { "mali", NULL };
  103. struct thermal_cooling_device *cooling;
  104. struct device *dev = ptdev->base.dev;
  105. struct panthor_devfreq *pdevfreq;
  106. struct dev_pm_opp *opp;
  107. unsigned long cur_freq;
  108. unsigned long freq = ULONG_MAX;
  109. int ret;
  110. pdevfreq = drmm_kzalloc(&ptdev->base, sizeof(*ptdev->devfreq), GFP_KERNEL);
  111. if (!pdevfreq)
  112. return -ENOMEM;
  113. ptdev->devfreq = pdevfreq;
  114. ret = devm_pm_opp_set_regulators(dev, reg_names);
  115. if (ret) {
  116. if (ret != -EPROBE_DEFER)
  117. DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n");
  118. return ret;
  119. }
  120. ret = devm_pm_opp_of_add_table(dev);
  121. if (ret)
  122. return ret;
  123. spin_lock_init(&pdevfreq->lock);
  124. panthor_devfreq_reset(pdevfreq);
  125. cur_freq = clk_get_rate(ptdev->clks.core);
  126. /* Regulator coupling only takes care of synchronizing/balancing voltage
  127. * updates, but the coupled regulator needs to be enabled manually.
  128. *
  129. * We use devm_regulator_get_enable_optional() and keep the sram supply
  130. * enabled until the device is removed, just like we do for the mali
  131. * supply, which is enabled when dev_pm_opp_set_opp(dev, opp) is called,
  132. * and disabled when the opp_table is torn down, using the devm action.
  133. *
  134. * If we really care about disabling regulators on suspend, we should:
  135. * - use devm_regulator_get_optional() here
  136. * - call dev_pm_opp_set_opp(dev, NULL) before leaving this function
  137. * (this disables the regulator passed to the OPP layer)
  138. * - call dev_pm_opp_set_opp(dev, NULL) and
  139. * regulator_disable(ptdev->regulators.sram) in
  140. * panthor_devfreq_suspend()
  141. * - call dev_pm_opp_set_opp(dev, default_opp) and
  142. * regulator_enable(ptdev->regulators.sram) in
  143. * panthor_devfreq_resume()
  144. *
  145. * But without knowing if it's beneficial or not (in term of power
  146. * consumption), or how much it slows down the suspend/resume steps,
  147. * let's just keep regulators enabled for the device lifetime.
  148. */
  149. ret = devm_regulator_get_enable_optional(dev, "sram");
  150. if (ret && ret != -ENODEV) {
  151. if (ret != -EPROBE_DEFER)
  152. DRM_DEV_ERROR(dev, "Couldn't retrieve/enable sram supply\n");
  153. return ret;
  154. }
  155. opp = devfreq_recommended_opp(dev, &cur_freq, 0);
  156. if (IS_ERR(opp))
  157. return PTR_ERR(opp);
  158. panthor_devfreq_profile.initial_freq = cur_freq;
  159. ptdev->current_frequency = cur_freq;
  160. /*
  161. * Set the recommend OPP this will enable and configure the regulator
  162. * if any and will avoid a switch off by regulator_late_cleanup()
  163. */
  164. ret = dev_pm_opp_set_opp(dev, opp);
  165. dev_pm_opp_put(opp);
  166. if (ret) {
  167. DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n");
  168. return ret;
  169. }
  170. /* Find the fastest defined rate */
  171. opp = dev_pm_opp_find_freq_floor(dev, &freq);
  172. if (IS_ERR(opp))
  173. return PTR_ERR(opp);
  174. ptdev->fast_rate = freq;
  175. dev_pm_opp_put(opp);
  176. /*
  177. * Setup default thresholds for the simple_ondemand governor.
  178. * The values are chosen based on experiments.
  179. */
  180. pdevfreq->gov_data.upthreshold = 45;
  181. pdevfreq->gov_data.downdifferential = 5;
  182. pdevfreq->devfreq = devm_devfreq_add_device(dev, &panthor_devfreq_profile,
  183. DEVFREQ_GOV_SIMPLE_ONDEMAND,
  184. &pdevfreq->gov_data);
  185. if (IS_ERR(pdevfreq->devfreq)) {
  186. DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
  187. ret = PTR_ERR(pdevfreq->devfreq);
  188. pdevfreq->devfreq = NULL;
  189. return ret;
  190. }
  191. cooling = devfreq_cooling_em_register(pdevfreq->devfreq, NULL);
  192. if (IS_ERR(cooling))
  193. DRM_DEV_INFO(dev, "Failed to register cooling device\n");
  194. return 0;
  195. }
  196. int panthor_devfreq_resume(struct panthor_device *ptdev)
  197. {
  198. struct panthor_devfreq *pdevfreq = ptdev->devfreq;
  199. if (!pdevfreq->devfreq)
  200. return 0;
  201. panthor_devfreq_reset(pdevfreq);
  202. return devfreq_resume_device(pdevfreq->devfreq);
  203. }
  204. int panthor_devfreq_suspend(struct panthor_device *ptdev)
  205. {
  206. struct panthor_devfreq *pdevfreq = ptdev->devfreq;
  207. if (!pdevfreq->devfreq)
  208. return 0;
  209. return devfreq_suspend_device(pdevfreq->devfreq);
  210. }
  211. void panthor_devfreq_record_busy(struct panthor_device *ptdev)
  212. {
  213. struct panthor_devfreq *pdevfreq = ptdev->devfreq;
  214. unsigned long irqflags;
  215. if (!pdevfreq->devfreq)
  216. return;
  217. spin_lock_irqsave(&pdevfreq->lock, irqflags);
  218. panthor_devfreq_update_utilization(pdevfreq);
  219. pdevfreq->last_busy_state = true;
  220. spin_unlock_irqrestore(&pdevfreq->lock, irqflags);
  221. }
  222. void panthor_devfreq_record_idle(struct panthor_device *ptdev)
  223. {
  224. struct panthor_devfreq *pdevfreq = ptdev->devfreq;
  225. unsigned long irqflags;
  226. if (!pdevfreq->devfreq)
  227. return;
  228. spin_lock_irqsave(&pdevfreq->lock, irqflags);
  229. panthor_devfreq_update_utilization(pdevfreq);
  230. pdevfreq->last_busy_state = false;
  231. spin_unlock_irqrestore(&pdevfreq->lock, irqflags);
  232. }