msm_gpu_devfreq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. #include "msm_gpu.h"
  7. #include "msm_gpu_trace.h"
  8. #include <linux/devfreq.h>
  9. #include <linux/devfreq_cooling.h>
  10. #include <linux/math64.h>
  11. #include <linux/units.h>
  12. /*
  13. * Power Management:
  14. */
  15. static int msm_devfreq_target(struct device *dev, unsigned long *freq,
  16. u32 flags)
  17. {
  18. struct msm_gpu *gpu = dev_to_gpu(dev);
  19. struct msm_gpu_devfreq *df = &gpu->devfreq;
  20. struct dev_pm_opp *opp;
  21. /*
  22. * Note that devfreq_recommended_opp() can modify the freq
  23. * to something that actually is in the opp table:
  24. */
  25. opp = devfreq_recommended_opp(dev, freq, flags);
  26. if (IS_ERR(opp))
  27. return PTR_ERR(opp);
  28. trace_msm_gpu_freq_change(dev_pm_opp_get_freq(opp));
  29. /*
  30. * If the GPU is idle, devfreq is not aware, so just stash
  31. * the new target freq (to use when we return to active)
  32. */
  33. if (df->idle_freq) {
  34. df->idle_freq = *freq;
  35. dev_pm_opp_put(opp);
  36. return 0;
  37. }
  38. if (gpu->funcs->gpu_set_freq) {
  39. mutex_lock(&df->lock);
  40. gpu->funcs->gpu_set_freq(gpu, opp, df->suspended);
  41. mutex_unlock(&df->lock);
  42. } else {
  43. dev_pm_opp_set_rate(dev, *freq);
  44. }
  45. dev_pm_opp_put(opp);
  46. return 0;
  47. }
  48. static unsigned long get_freq(struct msm_gpu *gpu)
  49. {
  50. struct msm_gpu_devfreq *df = &gpu->devfreq;
  51. /*
  52. * If the GPU is idle, use the shadow/saved freq to avoid
  53. * confusing devfreq (which is unaware that we are switching
  54. * to lowest freq until the device is active again)
  55. */
  56. if (df->idle_freq)
  57. return df->idle_freq;
  58. if (gpu->funcs->gpu_get_freq)
  59. return gpu->funcs->gpu_get_freq(gpu);
  60. return clk_get_rate(gpu->core_clk);
  61. }
  62. static int msm_devfreq_get_dev_status(struct device *dev,
  63. struct devfreq_dev_status *status)
  64. {
  65. struct msm_gpu *gpu = dev_to_gpu(dev);
  66. struct msm_gpu_devfreq *df = &gpu->devfreq;
  67. u64 busy_cycles, busy_time;
  68. unsigned long sample_rate;
  69. ktime_t time;
  70. mutex_lock(&df->lock);
  71. status->current_frequency = get_freq(gpu);
  72. time = ktime_get();
  73. status->total_time = ktime_us_delta(time, df->time);
  74. df->time = time;
  75. if (df->suspended) {
  76. mutex_unlock(&df->lock);
  77. status->busy_time = 0;
  78. return 0;
  79. }
  80. busy_cycles = gpu->funcs->gpu_busy(gpu, &sample_rate);
  81. busy_time = busy_cycles - df->busy_cycles;
  82. df->busy_cycles = busy_cycles;
  83. mutex_unlock(&df->lock);
  84. busy_time *= USEC_PER_SEC;
  85. busy_time = div64_ul(busy_time, sample_rate);
  86. if (WARN_ON(busy_time > ~0LU))
  87. busy_time = ~0LU;
  88. status->busy_time = busy_time;
  89. return 0;
  90. }
  91. static int msm_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
  92. {
  93. *freq = get_freq(dev_to_gpu(dev));
  94. return 0;
  95. }
  96. static struct devfreq_dev_profile msm_devfreq_profile = {
  97. .timer = DEVFREQ_TIMER_DELAYED,
  98. .polling_ms = 50,
  99. .target = msm_devfreq_target,
  100. .get_dev_status = msm_devfreq_get_dev_status,
  101. .get_cur_freq = msm_devfreq_get_cur_freq,
  102. };
  103. static void msm_devfreq_boost_work(struct kthread_work *work);
  104. static void msm_devfreq_idle_work(struct kthread_work *work);
  105. static bool has_devfreq(struct msm_gpu *gpu)
  106. {
  107. struct msm_gpu_devfreq *df = &gpu->devfreq;
  108. return !!df->devfreq;
  109. }
  110. void msm_devfreq_init(struct msm_gpu *gpu)
  111. {
  112. struct msm_gpu_devfreq *df = &gpu->devfreq;
  113. struct msm_drm_private *priv = gpu->dev->dev_private;
  114. int ret;
  115. /* We need target support to do devfreq */
  116. if (!gpu->funcs->gpu_busy)
  117. return;
  118. /*
  119. * Setup default values for simple_ondemand governor tuning. We
  120. * want to throttle up at 50% load for the double-buffer case,
  121. * where due to stalling waiting for vblank we could get stuck
  122. * at (for ex) 30fps at 50% utilization.
  123. */
  124. priv->gpu_devfreq_config.upthreshold = 50;
  125. priv->gpu_devfreq_config.downdifferential = 10;
  126. mutex_init(&df->lock);
  127. df->suspended = true;
  128. ret = dev_pm_qos_add_request(&gpu->pdev->dev, &df->boost_freq,
  129. DEV_PM_QOS_MIN_FREQUENCY, 0);
  130. if (ret < 0) {
  131. DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize QoS\n");
  132. return;
  133. }
  134. msm_devfreq_profile.initial_freq = gpu->fast_rate;
  135. /*
  136. * Don't set the freq_table or max_state and let devfreq build the table
  137. * from OPP
  138. * After a deferred probe, these may have be left to non-zero values,
  139. * so set them back to zero before creating the devfreq device
  140. */
  141. msm_devfreq_profile.freq_table = NULL;
  142. msm_devfreq_profile.max_state = 0;
  143. df->devfreq = devm_devfreq_add_device(&gpu->pdev->dev,
  144. &msm_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
  145. &priv->gpu_devfreq_config);
  146. if (IS_ERR(df->devfreq)) {
  147. DRM_DEV_ERROR(&gpu->pdev->dev, "Couldn't initialize GPU devfreq\n");
  148. dev_pm_qos_remove_request(&df->boost_freq);
  149. df->devfreq = NULL;
  150. return;
  151. }
  152. devfreq_suspend_device(df->devfreq);
  153. gpu->cooling = of_devfreq_cooling_register(gpu->pdev->dev.of_node, df->devfreq);
  154. if (IS_ERR(gpu->cooling)) {
  155. DRM_DEV_ERROR(&gpu->pdev->dev,
  156. "Couldn't register GPU cooling device\n");
  157. gpu->cooling = NULL;
  158. }
  159. msm_hrtimer_work_init(&df->boost_work, gpu->worker, msm_devfreq_boost_work,
  160. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  161. msm_hrtimer_work_init(&df->idle_work, gpu->worker, msm_devfreq_idle_work,
  162. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  163. }
  164. static void cancel_idle_work(struct msm_gpu_devfreq *df)
  165. {
  166. hrtimer_cancel(&df->idle_work.timer);
  167. kthread_cancel_work_sync(&df->idle_work.work);
  168. }
  169. static void cancel_boost_work(struct msm_gpu_devfreq *df)
  170. {
  171. hrtimer_cancel(&df->boost_work.timer);
  172. kthread_cancel_work_sync(&df->boost_work.work);
  173. }
  174. void msm_devfreq_cleanup(struct msm_gpu *gpu)
  175. {
  176. struct msm_gpu_devfreq *df = &gpu->devfreq;
  177. if (!has_devfreq(gpu))
  178. return;
  179. devfreq_cooling_unregister(gpu->cooling);
  180. dev_pm_qos_remove_request(&df->boost_freq);
  181. }
  182. void msm_devfreq_resume(struct msm_gpu *gpu)
  183. {
  184. struct msm_gpu_devfreq *df = &gpu->devfreq;
  185. unsigned long sample_rate;
  186. if (!has_devfreq(gpu))
  187. return;
  188. mutex_lock(&df->lock);
  189. df->busy_cycles = gpu->funcs->gpu_busy(gpu, &sample_rate);
  190. df->time = ktime_get();
  191. df->suspended = false;
  192. mutex_unlock(&df->lock);
  193. devfreq_resume_device(df->devfreq);
  194. }
  195. void msm_devfreq_suspend(struct msm_gpu *gpu)
  196. {
  197. struct msm_gpu_devfreq *df = &gpu->devfreq;
  198. if (!has_devfreq(gpu))
  199. return;
  200. mutex_lock(&df->lock);
  201. df->suspended = true;
  202. mutex_unlock(&df->lock);
  203. devfreq_suspend_device(df->devfreq);
  204. cancel_idle_work(df);
  205. cancel_boost_work(df);
  206. }
  207. static void msm_devfreq_boost_work(struct kthread_work *work)
  208. {
  209. struct msm_gpu_devfreq *df = container_of(work,
  210. struct msm_gpu_devfreq, boost_work.work);
  211. dev_pm_qos_update_request(&df->boost_freq, 0);
  212. }
  213. void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor)
  214. {
  215. struct msm_gpu_devfreq *df = &gpu->devfreq;
  216. uint64_t freq;
  217. if (!has_devfreq(gpu))
  218. return;
  219. freq = get_freq(gpu);
  220. freq *= factor;
  221. /*
  222. * A nice little trap is that PM QoS operates in terms of KHz,
  223. * while devfreq operates in terms of Hz:
  224. */
  225. do_div(freq, HZ_PER_KHZ);
  226. dev_pm_qos_update_request(&df->boost_freq, freq);
  227. msm_hrtimer_queue_work(&df->boost_work,
  228. ms_to_ktime(msm_devfreq_profile.polling_ms),
  229. HRTIMER_MODE_REL);
  230. }
  231. void msm_devfreq_active(struct msm_gpu *gpu)
  232. {
  233. struct msm_gpu_devfreq *df = &gpu->devfreq;
  234. unsigned int idle_time;
  235. unsigned long target_freq;
  236. if (!has_devfreq(gpu))
  237. return;
  238. /*
  239. * Cancel any pending transition to idle frequency:
  240. */
  241. cancel_idle_work(df);
  242. /*
  243. * Hold devfreq lock to synchronize with get_dev_status()/
  244. * target() callbacks
  245. */
  246. mutex_lock(&df->devfreq->lock);
  247. target_freq = df->idle_freq;
  248. idle_time = ktime_to_ms(ktime_sub(ktime_get(), df->idle_time));
  249. df->idle_freq = 0;
  250. /*
  251. * We could have become active again before the idle work had a
  252. * chance to run, in which case the df->idle_freq would have
  253. * still been zero. In this case, no need to change freq.
  254. */
  255. if (target_freq)
  256. msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
  257. mutex_unlock(&df->devfreq->lock);
  258. /*
  259. * If we've been idle for a significant fraction of a polling
  260. * interval, then we won't meet the threshold of busyness for
  261. * the governor to ramp up the freq.. so give some boost
  262. */
  263. if (idle_time > msm_devfreq_profile.polling_ms) {
  264. msm_devfreq_boost(gpu, 2);
  265. }
  266. }
  267. static void msm_devfreq_idle_work(struct kthread_work *work)
  268. {
  269. struct msm_gpu_devfreq *df = container_of(work,
  270. struct msm_gpu_devfreq, idle_work.work);
  271. struct msm_gpu *gpu = container_of(df, struct msm_gpu, devfreq);
  272. struct msm_drm_private *priv = gpu->dev->dev_private;
  273. unsigned long idle_freq, target_freq = 0;
  274. /*
  275. * Hold devfreq lock to synchronize with get_dev_status()/
  276. * target() callbacks
  277. */
  278. mutex_lock(&df->devfreq->lock);
  279. idle_freq = get_freq(gpu);
  280. if (priv->gpu_clamp_to_idle)
  281. msm_devfreq_target(&gpu->pdev->dev, &target_freq, 0);
  282. df->idle_time = ktime_get();
  283. df->idle_freq = idle_freq;
  284. mutex_unlock(&df->devfreq->lock);
  285. }
  286. void msm_devfreq_idle(struct msm_gpu *gpu)
  287. {
  288. struct msm_gpu_devfreq *df = &gpu->devfreq;
  289. if (!has_devfreq(gpu))
  290. return;
  291. msm_hrtimer_queue_work(&df->idle_work, ms_to_ktime(1),
  292. HRTIMER_MODE_REL);
  293. }