drm_self_refresh_helper.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2019 Google, Inc.
  4. *
  5. * Authors:
  6. * Sean Paul <seanpaul@chromium.org>
  7. */
  8. #include <linux/average.h>
  9. #include <linux/bitops.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/workqueue.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_connector.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_device.h>
  18. #include <drm/drm_mode_config.h>
  19. #include <drm/drm_modeset_lock.h>
  20. #include <drm/drm_print.h>
  21. #include <drm/drm_self_refresh_helper.h>
  22. /**
  23. * DOC: overview
  24. *
  25. * This helper library provides an easy way for drivers to leverage the atomic
  26. * framework to implement panel self refresh (SR) support. Drivers are
  27. * responsible for initializing and cleaning up the SR helpers on load/unload
  28. * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup).
  29. * The connector is responsible for setting
  30. * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware
  31. * (meaning it knows how to initiate self refresh on the panel).
  32. *
  33. * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the
  34. * helpers will monitor activity and call back into the driver to enable/disable
  35. * SR as appropriate. The best way to think about this is that it's a DPMS
  36. * on/off request with &drm_crtc_state.self_refresh_active set in crtc state
  37. * that tells you to disable/enable SR on the panel instead of power-cycling it.
  38. *
  39. * During SR, drivers may choose to fully disable their crtc/encoder/bridge
  40. * hardware (in which case no driver changes are necessary), or they can inspect
  41. * &drm_crtc_state.self_refresh_active if they want to enter low power mode
  42. * without full disable (in case full disable/enable is too slow).
  43. *
  44. * SR will be deactivated if there are any atomic updates affecting the
  45. * pipe that is in SR mode. If a crtc is driving multiple connectors, all
  46. * connectors must be SR aware and all will enter/exit SR mode at the same time.
  47. *
  48. * If the crtc and connector are SR aware, but the panel connected does not
  49. * support it (or is otherwise unable to enter SR), the driver should fail
  50. * atomic_check when &drm_crtc_state.self_refresh_active is true.
  51. */
  52. #define SELF_REFRESH_AVG_SEED_MS 200
  53. DECLARE_EWMA(psr_time, 4, 4)
  54. struct drm_self_refresh_data {
  55. struct drm_crtc *crtc;
  56. struct delayed_work entry_work;
  57. struct mutex avg_mutex;
  58. struct ewma_psr_time entry_avg_ms;
  59. struct ewma_psr_time exit_avg_ms;
  60. };
  61. static void drm_self_refresh_helper_entry_work(struct work_struct *work)
  62. {
  63. struct drm_self_refresh_data *sr_data = container_of(
  64. to_delayed_work(work),
  65. struct drm_self_refresh_data, entry_work);
  66. struct drm_crtc *crtc = sr_data->crtc;
  67. struct drm_device *dev = crtc->dev;
  68. struct drm_modeset_acquire_ctx ctx;
  69. struct drm_atomic_state *state;
  70. struct drm_connector *conn;
  71. struct drm_connector_state *conn_state;
  72. struct drm_crtc_state *crtc_state;
  73. int i, ret = 0;
  74. drm_modeset_acquire_init(&ctx, 0);
  75. state = drm_atomic_state_alloc(dev);
  76. if (!state) {
  77. ret = -ENOMEM;
  78. goto out_drop_locks;
  79. }
  80. retry:
  81. state->acquire_ctx = &ctx;
  82. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  83. if (IS_ERR(crtc_state)) {
  84. ret = PTR_ERR(crtc_state);
  85. goto out;
  86. }
  87. if (!crtc_state->enable)
  88. goto out;
  89. ret = drm_atomic_add_affected_connectors(state, crtc);
  90. if (ret)
  91. goto out;
  92. for_each_new_connector_in_state(state, conn, conn_state, i) {
  93. if (!conn_state->self_refresh_aware)
  94. goto out;
  95. }
  96. crtc_state->active = false;
  97. crtc_state->self_refresh_active = true;
  98. ret = drm_atomic_commit(state);
  99. if (ret)
  100. goto out;
  101. out:
  102. if (ret == -EDEADLK) {
  103. drm_atomic_state_clear(state);
  104. ret = drm_modeset_backoff(&ctx);
  105. if (!ret)
  106. goto retry;
  107. }
  108. drm_atomic_state_put(state);
  109. out_drop_locks:
  110. drm_modeset_drop_locks(&ctx);
  111. drm_modeset_acquire_fini(&ctx);
  112. }
  113. /**
  114. * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
  115. * @state: the state which has just been applied to hardware
  116. * @commit_time_ms: the amount of time in ms that this commit took to complete
  117. * @new_self_refresh_mask: bitmask of crtc's that have self_refresh_active in
  118. * new state
  119. *
  120. * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
  121. * update the average entry/exit self refresh times on self refresh transitions.
  122. * These averages will be used when calculating how long to delay before
  123. * entering self refresh mode after activity.
  124. */
  125. void
  126. drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
  127. unsigned int commit_time_ms,
  128. unsigned int new_self_refresh_mask)
  129. {
  130. struct drm_crtc *crtc;
  131. struct drm_crtc_state *old_crtc_state;
  132. int i;
  133. for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
  134. bool new_self_refresh_active = new_self_refresh_mask & BIT(i);
  135. struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
  136. struct ewma_psr_time *time;
  137. if (old_crtc_state->self_refresh_active ==
  138. new_self_refresh_active)
  139. continue;
  140. if (new_self_refresh_active)
  141. time = &sr_data->entry_avg_ms;
  142. else
  143. time = &sr_data->exit_avg_ms;
  144. mutex_lock(&sr_data->avg_mutex);
  145. ewma_psr_time_add(time, commit_time_ms);
  146. mutex_unlock(&sr_data->avg_mutex);
  147. }
  148. }
  149. EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
  150. /**
  151. * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  152. * @state: the state currently being checked
  153. *
  154. * Called at the end of atomic check. This function checks the state for flags
  155. * incompatible with self refresh exit and changes them. This is a bit
  156. * disingenuous since userspace is expecting one thing and we're giving it
  157. * another. However in order to keep self refresh entirely hidden from
  158. * userspace, this is required.
  159. *
  160. * At the end, we queue up the self refresh entry work so we can enter PSR after
  161. * the desired delay.
  162. */
  163. void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
  164. {
  165. struct drm_crtc *crtc;
  166. struct drm_crtc_state *crtc_state;
  167. int i;
  168. if (state->async_update || !state->allow_modeset) {
  169. for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
  170. if (crtc_state->self_refresh_active) {
  171. state->async_update = false;
  172. state->allow_modeset = true;
  173. break;
  174. }
  175. }
  176. }
  177. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  178. struct drm_self_refresh_data *sr_data;
  179. unsigned int delay;
  180. /* Don't trigger the entry timer when we're already in SR */
  181. if (crtc_state->self_refresh_active)
  182. continue;
  183. sr_data = crtc->self_refresh_data;
  184. if (!sr_data)
  185. continue;
  186. mutex_lock(&sr_data->avg_mutex);
  187. delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
  188. ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
  189. mutex_unlock(&sr_data->avg_mutex);
  190. mod_delayed_work(system_wq, &sr_data->entry_work,
  191. msecs_to_jiffies(delay));
  192. }
  193. }
  194. EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
  195. /**
  196. * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
  197. * @crtc: the crtc which supports self refresh supported displays
  198. *
  199. * Returns zero if successful or -errno on failure
  200. */
  201. int drm_self_refresh_helper_init(struct drm_crtc *crtc)
  202. {
  203. struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
  204. /* Helper is already initialized */
  205. if (WARN_ON(sr_data))
  206. return -EINVAL;
  207. sr_data = kzalloc_obj(*sr_data);
  208. if (!sr_data)
  209. return -ENOMEM;
  210. INIT_DELAYED_WORK(&sr_data->entry_work,
  211. drm_self_refresh_helper_entry_work);
  212. sr_data->crtc = crtc;
  213. mutex_init(&sr_data->avg_mutex);
  214. ewma_psr_time_init(&sr_data->entry_avg_ms);
  215. ewma_psr_time_init(&sr_data->exit_avg_ms);
  216. /*
  217. * Seed the averages so they're non-zero (and sufficiently large
  218. * for even poorly performing panels). As time goes on, this will be
  219. * averaged out and the values will trend to their true value.
  220. */
  221. ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
  222. ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
  223. crtc->self_refresh_data = sr_data;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(drm_self_refresh_helper_init);
  227. /**
  228. * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc
  229. * @crtc: the crtc to cleanup
  230. */
  231. void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
  232. {
  233. struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
  234. /* Helper is already uninitialized */
  235. if (!sr_data)
  236. return;
  237. crtc->self_refresh_data = NULL;
  238. cancel_delayed_work_sync(&sr_data->entry_work);
  239. kfree(sr_data);
  240. }
  241. EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);