drm_plane_helper.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2014 Intel Corporation
  3. *
  4. * DRM universal plane helper functions
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <linux/export.h>
  26. #include <linux/list.h>
  27. #include <drm/drm_atomic.h>
  28. #include <drm/drm_atomic_helper.h>
  29. #include <drm/drm_atomic_uapi.h>
  30. #include <drm/drm_device.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_encoder.h>
  33. #include <drm/drm_plane_helper.h>
  34. #include <drm/drm_print.h>
  35. #include <drm/drm_rect.h>
  36. #define SUBPIXEL_MASK 0xffff
  37. /**
  38. * DOC: overview
  39. *
  40. * This helper library contains helpers to implement primary plane support on
  41. * top of the normal CRTC configuration interface.
  42. * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
  43. * plane together with the CRTC state this does not allow userspace to disable
  44. * the primary plane itself. The default primary plane only expose XRBG8888 and
  45. * ARGB8888 as valid pixel formats for the attached framebuffer.
  46. *
  47. * Drivers are highly recommended to implement proper support for primary
  48. * planes, and newly merged drivers must not rely upon these transitional
  49. * helpers.
  50. *
  51. * The plane helpers share the function table structures with other helpers,
  52. * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
  53. * the details.
  54. */
  55. /*
  56. * Returns the connectors currently associated with a CRTC. This function
  57. * should be called twice: once with a NULL connector list to retrieve
  58. * the list size, and once with the properly allocated list to be filled in.
  59. */
  60. static int get_connectors_for_crtc(struct drm_crtc *crtc,
  61. struct drm_connector **connector_list,
  62. int num_connectors)
  63. {
  64. struct drm_device *dev = crtc->dev;
  65. struct drm_connector *connector;
  66. struct drm_connector_list_iter conn_iter;
  67. int count = 0;
  68. /*
  69. * Note: Once we change the plane hooks to more fine-grained locking we
  70. * need to grab the connection_mutex here to be able to make these
  71. * checks.
  72. */
  73. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  74. drm_connector_list_iter_begin(dev, &conn_iter);
  75. drm_for_each_connector_iter(connector, &conn_iter) {
  76. if (connector->encoder && connector->encoder->crtc == crtc) {
  77. if (connector_list != NULL && count < num_connectors)
  78. *(connector_list++) = connector;
  79. count++;
  80. }
  81. }
  82. drm_connector_list_iter_end(&conn_iter);
  83. return count;
  84. }
  85. static int drm_plane_helper_check_update(struct drm_plane *plane,
  86. struct drm_crtc *crtc,
  87. struct drm_framebuffer *fb,
  88. struct drm_rect *src,
  89. struct drm_rect *dst,
  90. unsigned int rotation,
  91. int min_scale,
  92. int max_scale,
  93. bool can_position,
  94. bool can_update_disabled,
  95. bool *visible)
  96. {
  97. struct drm_plane_state plane_state = {
  98. .plane = plane,
  99. .crtc = crtc,
  100. .fb = fb,
  101. .src_x = src->x1,
  102. .src_y = src->y1,
  103. .src_w = drm_rect_width(src),
  104. .src_h = drm_rect_height(src),
  105. .crtc_x = dst->x1,
  106. .crtc_y = dst->y1,
  107. .crtc_w = drm_rect_width(dst),
  108. .crtc_h = drm_rect_height(dst),
  109. .rotation = rotation,
  110. };
  111. struct drm_crtc_state crtc_state = {
  112. .crtc = crtc,
  113. .enable = crtc->enabled,
  114. .mode = crtc->mode,
  115. };
  116. int ret;
  117. ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
  118. min_scale, max_scale,
  119. can_position,
  120. can_update_disabled);
  121. if (ret)
  122. return ret;
  123. *src = plane_state.src;
  124. *dst = plane_state.dst;
  125. *visible = plane_state.visible;
  126. return 0;
  127. }
  128. /**
  129. * drm_plane_helper_update_primary - Helper for updating primary planes
  130. * @plane: plane to update
  131. * @crtc: the plane's new CRTC
  132. * @fb: the plane's new framebuffer
  133. * @crtc_x: x coordinate within CRTC
  134. * @crtc_y: y coordinate within CRTC
  135. * @crtc_w: width coordinate within CRTC
  136. * @crtc_h: height coordinate within CRTC
  137. * @src_x: x coordinate within source
  138. * @src_y: y coordinate within source
  139. * @src_w: width coordinate within source
  140. * @src_h: height coordinate within source
  141. * @ctx: modeset locking context
  142. *
  143. * This helper validates the given parameters and updates the primary plane.
  144. *
  145. * This function is only useful for non-atomic modesetting. Don't use
  146. * it in new drivers.
  147. *
  148. * Returns:
  149. * Zero on success, or an errno code otherwise.
  150. */
  151. int drm_plane_helper_update_primary(struct drm_plane *plane, struct drm_crtc *crtc,
  152. struct drm_framebuffer *fb,
  153. int crtc_x, int crtc_y,
  154. unsigned int crtc_w, unsigned int crtc_h,
  155. uint32_t src_x, uint32_t src_y,
  156. uint32_t src_w, uint32_t src_h,
  157. struct drm_modeset_acquire_ctx *ctx)
  158. {
  159. struct drm_mode_set set = {
  160. .crtc = crtc,
  161. .fb = fb,
  162. .mode = &crtc->mode,
  163. .x = src_x >> 16,
  164. .y = src_y >> 16,
  165. };
  166. struct drm_rect src = {
  167. .x1 = src_x,
  168. .y1 = src_y,
  169. .x2 = src_x + src_w,
  170. .y2 = src_y + src_h,
  171. };
  172. struct drm_rect dest = {
  173. .x1 = crtc_x,
  174. .y1 = crtc_y,
  175. .x2 = crtc_x + crtc_w,
  176. .y2 = crtc_y + crtc_h,
  177. };
  178. struct drm_device *dev = plane->dev;
  179. struct drm_connector **connector_list;
  180. int num_connectors, ret;
  181. bool visible;
  182. if (drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev)))
  183. return -EINVAL;
  184. ret = drm_plane_helper_check_update(plane, crtc, fb,
  185. &src, &dest,
  186. DRM_MODE_ROTATE_0,
  187. DRM_PLANE_NO_SCALING,
  188. DRM_PLANE_NO_SCALING,
  189. false, false, &visible);
  190. if (ret)
  191. return ret;
  192. if (!visible)
  193. /*
  194. * Primary plane isn't visible. Note that unless a driver
  195. * provides their own disable function, this will just
  196. * wind up returning -EINVAL to userspace.
  197. */
  198. return plane->funcs->disable_plane(plane, ctx);
  199. /* Find current connectors for CRTC */
  200. num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
  201. BUG_ON(num_connectors == 0);
  202. connector_list = kzalloc_objs(*connector_list, num_connectors);
  203. if (!connector_list)
  204. return -ENOMEM;
  205. get_connectors_for_crtc(crtc, connector_list, num_connectors);
  206. set.connectors = connector_list;
  207. set.num_connectors = num_connectors;
  208. /*
  209. * We call set_config() directly here rather than using
  210. * drm_mode_set_config_internal. We're reprogramming the same
  211. * connectors that were already in use, so we shouldn't need the extra
  212. * cross-CRTC fb refcounting to accommodate stealing connectors.
  213. * drm_mode_setplane() already handles the basic refcounting for the
  214. * framebuffers involved in this operation.
  215. */
  216. ret = crtc->funcs->set_config(&set, ctx);
  217. kfree(connector_list);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(drm_plane_helper_update_primary);
  221. /**
  222. * drm_plane_helper_disable_primary - Helper for disabling primary planes
  223. * @plane: plane to disable
  224. * @ctx: modeset locking context
  225. *
  226. * This helper returns an error when trying to disable the primary
  227. * plane.
  228. *
  229. * This function is only useful for non-atomic modesetting. Don't use
  230. * it in new drivers.
  231. *
  232. * Returns:
  233. * An errno code.
  234. */
  235. int drm_plane_helper_disable_primary(struct drm_plane *plane,
  236. struct drm_modeset_acquire_ctx *ctx)
  237. {
  238. struct drm_device *dev = plane->dev;
  239. drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
  240. return -EINVAL;
  241. }
  242. EXPORT_SYMBOL(drm_plane_helper_disable_primary);
  243. /**
  244. * drm_plane_helper_destroy() - Helper for primary plane destruction
  245. * @plane: plane to destroy
  246. *
  247. * Provides a default plane destroy handler for primary planes. This handler
  248. * is called during CRTC destruction. We disable the primary plane, remove
  249. * it from the DRM plane list, and deallocate the plane structure.
  250. */
  251. void drm_plane_helper_destroy(struct drm_plane *plane)
  252. {
  253. drm_plane_cleanup(plane);
  254. kfree(plane);
  255. }
  256. EXPORT_SYMBOL(drm_plane_helper_destroy);