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/list.h>
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_helper.h>
  28. #include <drm/drm_atomic_uapi.h>
  29. #include <drm/drm_device.h>
  30. #include <drm/drm_drv.h>
  31. #include <drm/drm_encoder.h>
  32. #include <drm/drm_plane_helper.h>
  33. #include <drm/drm_print.h>
  34. #include <drm/drm_rect.h>
  35. #define SUBPIXEL_MASK 0xffff
  36. /**
  37. * DOC: overview
  38. *
  39. * This helper library contains helpers to implement primary plane support on
  40. * top of the normal CRTC configuration interface.
  41. * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
  42. * plane together with the CRTC state this does not allow userspace to disable
  43. * the primary plane itself. The default primary plane only expose XRBG8888 and
  44. * ARGB8888 as valid pixel formats for the attached framebuffer.
  45. *
  46. * Drivers are highly recommended to implement proper support for primary
  47. * planes, and newly merged drivers must not rely upon these transitional
  48. * helpers.
  49. *
  50. * The plane helpers share the function table structures with other helpers,
  51. * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
  52. * the details.
  53. */
  54. /*
  55. * Returns the connectors currently associated with a CRTC. This function
  56. * should be called twice: once with a NULL connector list to retrieve
  57. * the list size, and once with the properly allocated list to be filled in.
  58. */
  59. static int get_connectors_for_crtc(struct drm_crtc *crtc,
  60. struct drm_connector **connector_list,
  61. int num_connectors)
  62. {
  63. struct drm_device *dev = crtc->dev;
  64. struct drm_connector *connector;
  65. struct drm_connector_list_iter conn_iter;
  66. int count = 0;
  67. /*
  68. * Note: Once we change the plane hooks to more fine-grained locking we
  69. * need to grab the connection_mutex here to be able to make these
  70. * checks.
  71. */
  72. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  73. drm_connector_list_iter_begin(dev, &conn_iter);
  74. drm_for_each_connector_iter(connector, &conn_iter) {
  75. if (connector->encoder && connector->encoder->crtc == crtc) {
  76. if (connector_list != NULL && count < num_connectors)
  77. *(connector_list++) = connector;
  78. count++;
  79. }
  80. }
  81. drm_connector_list_iter_end(&conn_iter);
  82. return count;
  83. }
  84. static int drm_plane_helper_check_update(struct drm_plane *plane,
  85. struct drm_crtc *crtc,
  86. struct drm_framebuffer *fb,
  87. struct drm_rect *src,
  88. struct drm_rect *dst,
  89. unsigned int rotation,
  90. int min_scale,
  91. int max_scale,
  92. bool can_position,
  93. bool can_update_disabled,
  94. bool *visible)
  95. {
  96. struct drm_plane_state plane_state = {
  97. .plane = plane,
  98. .crtc = crtc,
  99. .fb = fb,
  100. .src_x = src->x1,
  101. .src_y = src->y1,
  102. .src_w = drm_rect_width(src),
  103. .src_h = drm_rect_height(src),
  104. .crtc_x = dst->x1,
  105. .crtc_y = dst->y1,
  106. .crtc_w = drm_rect_width(dst),
  107. .crtc_h = drm_rect_height(dst),
  108. .rotation = rotation,
  109. };
  110. struct drm_crtc_state crtc_state = {
  111. .crtc = crtc,
  112. .enable = crtc->enabled,
  113. .mode = crtc->mode,
  114. };
  115. int ret;
  116. ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
  117. min_scale, max_scale,
  118. can_position,
  119. can_update_disabled);
  120. if (ret)
  121. return ret;
  122. *src = plane_state.src;
  123. *dst = plane_state.dst;
  124. *visible = plane_state.visible;
  125. return 0;
  126. }
  127. /**
  128. * drm_plane_helper_update_primary - Helper for updating primary planes
  129. * @plane: plane to update
  130. * @crtc: the plane's new CRTC
  131. * @fb: the plane's new framebuffer
  132. * @crtc_x: x coordinate within CRTC
  133. * @crtc_y: y coordinate within CRTC
  134. * @crtc_w: width coordinate within CRTC
  135. * @crtc_h: height coordinate within CRTC
  136. * @src_x: x coordinate within source
  137. * @src_y: y coordinate within source
  138. * @src_w: width coordinate within source
  139. * @src_h: height coordinate within source
  140. * @ctx: modeset locking context
  141. *
  142. * This helper validates the given parameters and updates the primary plane.
  143. *
  144. * This function is only useful for non-atomic modesetting. Don't use
  145. * it in new drivers.
  146. *
  147. * Returns:
  148. * Zero on success, or an errno code otherwise.
  149. */
  150. int drm_plane_helper_update_primary(struct drm_plane *plane, struct drm_crtc *crtc,
  151. struct drm_framebuffer *fb,
  152. int crtc_x, int crtc_y,
  153. unsigned int crtc_w, unsigned int crtc_h,
  154. uint32_t src_x, uint32_t src_y,
  155. uint32_t src_w, uint32_t src_h,
  156. struct drm_modeset_acquire_ctx *ctx)
  157. {
  158. struct drm_mode_set set = {
  159. .crtc = crtc,
  160. .fb = fb,
  161. .mode = &crtc->mode,
  162. .x = src_x >> 16,
  163. .y = src_y >> 16,
  164. };
  165. struct drm_rect src = {
  166. .x1 = src_x,
  167. .y1 = src_y,
  168. .x2 = src_x + src_w,
  169. .y2 = src_y + src_h,
  170. };
  171. struct drm_rect dest = {
  172. .x1 = crtc_x,
  173. .y1 = crtc_y,
  174. .x2 = crtc_x + crtc_w,
  175. .y2 = crtc_y + crtc_h,
  176. };
  177. struct drm_device *dev = plane->dev;
  178. struct drm_connector **connector_list;
  179. int num_connectors, ret;
  180. bool visible;
  181. if (drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev)))
  182. return -EINVAL;
  183. ret = drm_plane_helper_check_update(plane, crtc, fb,
  184. &src, &dest,
  185. DRM_MODE_ROTATE_0,
  186. DRM_PLANE_NO_SCALING,
  187. DRM_PLANE_NO_SCALING,
  188. false, false, &visible);
  189. if (ret)
  190. return ret;
  191. if (!visible)
  192. /*
  193. * Primary plane isn't visible. Note that unless a driver
  194. * provides their own disable function, this will just
  195. * wind up returning -EINVAL to userspace.
  196. */
  197. return plane->funcs->disable_plane(plane, ctx);
  198. /* Find current connectors for CRTC */
  199. num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
  200. BUG_ON(num_connectors == 0);
  201. connector_list = kcalloc(num_connectors, sizeof(*connector_list),
  202. GFP_KERNEL);
  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);