drm_modeset_helper.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_crtc_helper.h>
  24. #include <drm/drm_fb_helper.h>
  25. #include <drm/drm_modeset_helper.h>
  26. #include <drm/drm_plane_helper.h>
  27. /**
  28. * DOC: aux kms helpers
  29. *
  30. * This helper library contains various one-off functions which don't really fit
  31. * anywhere else in the DRM modeset helper library.
  32. */
  33. /**
  34. * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
  35. * connector list
  36. * @dev: drm device to operate on
  37. *
  38. * Some userspace presumes that the first connected connector is the main
  39. * display, where it's supposed to display e.g. the login screen. For
  40. * laptops, this should be the main panel. Use this function to sort all
  41. * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
  42. * painstakingly trying to initialize them in the right order.
  43. */
  44. void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
  45. {
  46. struct drm_connector *connector, *tmp;
  47. struct list_head panel_list;
  48. INIT_LIST_HEAD(&panel_list);
  49. spin_lock_irq(&dev->mode_config.connector_list_lock);
  50. list_for_each_entry_safe(connector, tmp,
  51. &dev->mode_config.connector_list, head) {
  52. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
  53. connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  54. connector->connector_type == DRM_MODE_CONNECTOR_DSI)
  55. list_move_tail(&connector->head, &panel_list);
  56. }
  57. list_splice(&panel_list, &dev->mode_config.connector_list);
  58. spin_unlock_irq(&dev->mode_config.connector_list_lock);
  59. }
  60. EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
  61. /**
  62. * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
  63. * @dev: DRM device
  64. * @fb: drm_framebuffer object to fill out
  65. * @mode_cmd: metadata from the userspace fb creation request
  66. *
  67. * This helper can be used in a drivers fb_create callback to pre-fill the fb's
  68. * metadata fields.
  69. */
  70. void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
  71. struct drm_framebuffer *fb,
  72. const struct drm_mode_fb_cmd2 *mode_cmd)
  73. {
  74. int i;
  75. fb->dev = dev;
  76. fb->format = drm_get_format_info(dev, mode_cmd);
  77. fb->width = mode_cmd->width;
  78. fb->height = mode_cmd->height;
  79. for (i = 0; i < 4; i++) {
  80. fb->pitches[i] = mode_cmd->pitches[i];
  81. fb->offsets[i] = mode_cmd->offsets[i];
  82. }
  83. fb->modifier = mode_cmd->modifier[0];
  84. fb->flags = mode_cmd->flags;
  85. }
  86. EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
  87. /*
  88. * This is the minimal list of formats that seem to be safe for modeset use
  89. * with all current DRM drivers. Most hardware can actually support more
  90. * formats than this and drivers may specify a more accurate list when
  91. * creating the primary plane. However drivers that still call
  92. * drm_plane_init() will use this minimal format list as the default.
  93. */
  94. static const uint32_t safe_modeset_formats[] = {
  95. DRM_FORMAT_XRGB8888,
  96. DRM_FORMAT_ARGB8888,
  97. };
  98. static struct drm_plane *create_primary_plane(struct drm_device *dev)
  99. {
  100. struct drm_plane *primary;
  101. int ret;
  102. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  103. if (primary == NULL) {
  104. DRM_DEBUG_KMS("Failed to allocate primary plane\n");
  105. return NULL;
  106. }
  107. /*
  108. * Remove the format_default field from drm_plane when dropping
  109. * this helper.
  110. */
  111. primary->format_default = true;
  112. /* possible_crtc's will be filled in later by crtc_init */
  113. ret = drm_universal_plane_init(dev, primary, 0,
  114. &drm_primary_helper_funcs,
  115. safe_modeset_formats,
  116. ARRAY_SIZE(safe_modeset_formats),
  117. NULL,
  118. DRM_PLANE_TYPE_PRIMARY, NULL);
  119. if (ret) {
  120. kfree(primary);
  121. primary = NULL;
  122. }
  123. return primary;
  124. }
  125. /**
  126. * drm_crtc_init - Legacy CRTC initialization function
  127. * @dev: DRM device
  128. * @crtc: CRTC object to init
  129. * @funcs: callbacks for the new CRTC
  130. *
  131. * Initialize a CRTC object with a default helper-provided primary plane and no
  132. * cursor plane.
  133. *
  134. * Returns:
  135. * Zero on success, error code on failure.
  136. */
  137. int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  138. const struct drm_crtc_funcs *funcs)
  139. {
  140. struct drm_plane *primary;
  141. primary = create_primary_plane(dev);
  142. return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
  143. NULL);
  144. }
  145. EXPORT_SYMBOL(drm_crtc_init);
  146. /**
  147. * drm_mode_config_helper_suspend - Modeset suspend helper
  148. * @dev: DRM device
  149. *
  150. * This helper function takes care of suspending the modeset side. It disables
  151. * output polling if initialized, suspends fbdev if used and finally calls
  152. * drm_atomic_helper_suspend().
  153. * If suspending fails, fbdev and polling is re-enabled.
  154. *
  155. * Returns:
  156. * Zero on success, negative error code on error.
  157. *
  158. * See also:
  159. * drm_kms_helper_poll_disable() and drm_fb_helper_set_suspend_unlocked().
  160. */
  161. int drm_mode_config_helper_suspend(struct drm_device *dev)
  162. {
  163. struct drm_atomic_state *state;
  164. if (!dev)
  165. return 0;
  166. drm_kms_helper_poll_disable(dev);
  167. drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 1);
  168. state = drm_atomic_helper_suspend(dev);
  169. if (IS_ERR(state)) {
  170. drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
  171. drm_kms_helper_poll_enable(dev);
  172. return PTR_ERR(state);
  173. }
  174. dev->mode_config.suspend_state = state;
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(drm_mode_config_helper_suspend);
  178. /**
  179. * drm_mode_config_helper_resume - Modeset resume helper
  180. * @dev: DRM device
  181. *
  182. * This helper function takes care of resuming the modeset side. It calls
  183. * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling
  184. * if initiaized.
  185. *
  186. * Returns:
  187. * Zero on success, negative error code on error.
  188. *
  189. * See also:
  190. * drm_fb_helper_set_suspend_unlocked() and drm_kms_helper_poll_enable().
  191. */
  192. int drm_mode_config_helper_resume(struct drm_device *dev)
  193. {
  194. int ret;
  195. if (!dev)
  196. return 0;
  197. if (WARN_ON(!dev->mode_config.suspend_state))
  198. return -EINVAL;
  199. ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state);
  200. if (ret)
  201. DRM_ERROR("Failed to resume (%d)\n", ret);
  202. dev->mode_config.suspend_state = NULL;
  203. drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
  204. drm_kms_helper_poll_enable(dev);
  205. return ret;
  206. }
  207. EXPORT_SYMBOL(drm_mode_config_helper_resume);