drm_self_refresh_helper.c 8.3 KB

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