drm_plane_helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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/drmP.h>
  27. #include <drm/drm_rect.h>
  28. #define SUBPIXEL_MASK 0xffff
  29. /*
  30. * This is the minimal list of formats that seem to be safe for modeset use
  31. * with all current DRM drivers. Most hardware can actually support more
  32. * formats than this and drivers may specify a more accurate list when
  33. * creating the primary plane. However drivers that still call
  34. * drm_plane_init() will use this minimal format list as the default.
  35. */
  36. const static uint32_t safe_modeset_formats[] = {
  37. DRM_FORMAT_XRGB8888,
  38. DRM_FORMAT_ARGB8888,
  39. };
  40. /*
  41. * Returns the connectors currently associated with a CRTC. This function
  42. * should be called twice: once with a NULL connector list to retrieve
  43. * the list size, and once with the properly allocated list to be filled in.
  44. */
  45. static int get_connectors_for_crtc(struct drm_crtc *crtc,
  46. struct drm_connector **connector_list,
  47. int num_connectors)
  48. {
  49. struct drm_device *dev = crtc->dev;
  50. struct drm_connector *connector;
  51. int count = 0;
  52. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  53. if (connector->encoder && connector->encoder->crtc == crtc) {
  54. if (connector_list != NULL && count < num_connectors)
  55. *(connector_list++) = connector;
  56. count++;
  57. }
  58. return count;
  59. }
  60. /**
  61. * drm_primary_helper_update() - Helper for primary plane update
  62. * @plane: plane object to update
  63. * @crtc: owning CRTC of owning plane
  64. * @fb: framebuffer to flip onto plane
  65. * @crtc_x: x offset of primary plane on crtc
  66. * @crtc_y: y offset of primary plane on crtc
  67. * @crtc_w: width of primary plane rectangle on crtc
  68. * @crtc_h: height of primary plane rectangle on crtc
  69. * @src_x: x offset of @fb for panning
  70. * @src_y: y offset of @fb for panning
  71. * @src_w: width of source rectangle in @fb
  72. * @src_h: height of source rectangle in @fb
  73. *
  74. * Provides a default plane update handler for primary planes. This is handler
  75. * is called in response to a userspace SetPlane operation on the plane with a
  76. * non-NULL framebuffer. We call the driver's modeset handler to update the
  77. * framebuffer.
  78. *
  79. * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
  80. * return an error.
  81. *
  82. * Note that we make some assumptions about hardware limitations that may not be
  83. * true for all hardware --
  84. * 1) Primary plane cannot be repositioned.
  85. * 2) Primary plane cannot be scaled.
  86. * 3) Primary plane must cover the entire CRTC.
  87. * 4) Subpixel positioning is not supported.
  88. * Drivers for hardware that don't have these restrictions can provide their
  89. * own implementation rather than using this helper.
  90. *
  91. * RETURNS:
  92. * Zero on success, error code on failure
  93. */
  94. int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  95. struct drm_framebuffer *fb,
  96. int crtc_x, int crtc_y,
  97. unsigned int crtc_w, unsigned int crtc_h,
  98. uint32_t src_x, uint32_t src_y,
  99. uint32_t src_w, uint32_t src_h)
  100. {
  101. struct drm_mode_set set = {
  102. .crtc = crtc,
  103. .fb = fb,
  104. .mode = &crtc->mode,
  105. .x = src_x >> 16,
  106. .y = src_y >> 16,
  107. };
  108. struct drm_rect dest = {
  109. .x1 = crtc_x,
  110. .y1 = crtc_y,
  111. .x2 = crtc_x + crtc_w,
  112. .y2 = crtc_y + crtc_h,
  113. };
  114. struct drm_rect clip = {
  115. .x2 = crtc->mode.hdisplay,
  116. .y2 = crtc->mode.vdisplay,
  117. };
  118. struct drm_connector **connector_list;
  119. struct drm_framebuffer *tmpfb;
  120. int num_connectors, ret;
  121. if (!crtc->enabled) {
  122. DRM_DEBUG_KMS("Cannot update primary plane of a disabled CRTC.\n");
  123. return -EINVAL;
  124. }
  125. /* Disallow subpixel positioning */
  126. if ((src_x | src_y | src_w | src_h) & SUBPIXEL_MASK) {
  127. DRM_DEBUG_KMS("Primary plane does not support subpixel positioning\n");
  128. return -EINVAL;
  129. }
  130. /* Primary planes are locked to their owning CRTC */
  131. if (plane->possible_crtcs != drm_crtc_mask(crtc)) {
  132. DRM_DEBUG_KMS("Cannot change primary plane CRTC\n");
  133. return -EINVAL;
  134. }
  135. /* Disallow scaling */
  136. if (crtc_w != src_w || crtc_h != src_h) {
  137. DRM_DEBUG_KMS("Can't scale primary plane\n");
  138. return -EINVAL;
  139. }
  140. /* Make sure primary plane covers entire CRTC */
  141. drm_rect_intersect(&dest, &clip);
  142. if (dest.x1 != 0 || dest.y1 != 0 ||
  143. dest.x2 != crtc->mode.hdisplay || dest.y2 != crtc->mode.vdisplay) {
  144. DRM_DEBUG_KMS("Primary plane must cover entire CRTC\n");
  145. return -EINVAL;
  146. }
  147. /* Framebuffer must be big enough to cover entire plane */
  148. ret = drm_crtc_check_viewport(crtc, crtc_x, crtc_y, &crtc->mode, fb);
  149. if (ret)
  150. return ret;
  151. /* Find current connectors for CRTC */
  152. num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
  153. BUG_ON(num_connectors == 0);
  154. connector_list = kzalloc(num_connectors * sizeof(*connector_list),
  155. GFP_KERNEL);
  156. if (!connector_list)
  157. return -ENOMEM;
  158. get_connectors_for_crtc(crtc, connector_list, num_connectors);
  159. set.connectors = connector_list;
  160. set.num_connectors = num_connectors;
  161. /*
  162. * set_config() adjusts crtc->primary->fb; however the DRM setplane
  163. * code that called us expects to handle the framebuffer update and
  164. * reference counting; save and restore the current fb before
  165. * calling it.
  166. *
  167. * N.B., we call set_config() directly here rather than using
  168. * drm_mode_set_config_internal. We're reprogramming the same
  169. * connectors that were already in use, so we shouldn't need the extra
  170. * cross-CRTC fb refcounting to accomodate stealing connectors.
  171. * drm_mode_setplane() already handles the basic refcounting for the
  172. * framebuffers involved in this operation.
  173. */
  174. tmpfb = plane->fb;
  175. ret = crtc->funcs->set_config(&set);
  176. plane->fb = tmpfb;
  177. kfree(connector_list);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL(drm_primary_helper_update);
  181. /**
  182. * drm_primary_helper_disable() - Helper for primary plane disable
  183. * @plane: plane to disable
  184. *
  185. * Provides a default plane disable handler for primary planes. This is handler
  186. * is called in response to a userspace SetPlane operation on the plane with a
  187. * NULL framebuffer parameter. We call the driver's modeset handler with a NULL
  188. * framebuffer to disable the CRTC if no other planes are currently enabled.
  189. * If other planes are still enabled on the same CRTC, we return -EBUSY.
  190. *
  191. * Note that some hardware may be able to disable the primary plane without
  192. * disabling the whole CRTC. Drivers for such hardware should provide their
  193. * own disable handler that disables just the primary plane (and they'll likely
  194. * need to provide their own update handler as well to properly re-enable a
  195. * disabled primary plane).
  196. *
  197. * RETURNS:
  198. * Zero on success, error code on failure
  199. */
  200. int drm_primary_helper_disable(struct drm_plane *plane)
  201. {
  202. struct drm_plane *p;
  203. struct drm_mode_set set = {
  204. .crtc = plane->crtc,
  205. .fb = NULL,
  206. };
  207. if (plane->crtc == NULL || plane->fb == NULL)
  208. /* Already disabled */
  209. return 0;
  210. list_for_each_entry(p, &plane->dev->mode_config.plane_list, head)
  211. if (p != plane && p->fb) {
  212. DRM_DEBUG_KMS("Cannot disable primary plane while other planes are still active on CRTC.\n");
  213. return -EBUSY;
  214. }
  215. /*
  216. * N.B. We call set_config() directly here rather than
  217. * drm_mode_set_config_internal() since drm_mode_setplane() already
  218. * handles the basic refcounting and we don't need the special
  219. * cross-CRTC refcounting (no chance of stealing connectors from
  220. * other CRTC's with this update).
  221. */
  222. return plane->crtc->funcs->set_config(&set);
  223. }
  224. EXPORT_SYMBOL(drm_primary_helper_disable);
  225. /**
  226. * drm_primary_helper_destroy() - Helper for primary plane destruction
  227. * @plane: plane to destroy
  228. *
  229. * Provides a default plane destroy handler for primary planes. This handler
  230. * is called during CRTC destruction. We disable the primary plane, remove
  231. * it from the DRM plane list, and deallocate the plane structure.
  232. */
  233. void drm_primary_helper_destroy(struct drm_plane *plane)
  234. {
  235. plane->funcs->disable_plane(plane);
  236. drm_plane_cleanup(plane);
  237. kfree(plane);
  238. }
  239. EXPORT_SYMBOL(drm_primary_helper_destroy);
  240. const struct drm_plane_funcs drm_primary_helper_funcs = {
  241. .update_plane = drm_primary_helper_update,
  242. .disable_plane = drm_primary_helper_disable,
  243. .destroy = drm_primary_helper_destroy,
  244. };
  245. EXPORT_SYMBOL(drm_primary_helper_funcs);
  246. /**
  247. * drm_primary_helper_create_plane() - Create a generic primary plane
  248. * @dev: drm device
  249. * @formats: pixel formats supported, or NULL for a default safe list
  250. * @num_formats: size of @formats; ignored if @formats is NULL
  251. *
  252. * Allocates and initializes a primary plane that can be used with the primary
  253. * plane helpers. Drivers that wish to use driver-specific plane structures or
  254. * provide custom handler functions may perform their own allocation and
  255. * initialization rather than calling this function.
  256. */
  257. struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
  258. const uint32_t *formats,
  259. int num_formats)
  260. {
  261. struct drm_plane *primary;
  262. int ret;
  263. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  264. if (primary == NULL) {
  265. DRM_DEBUG_KMS("Failed to allocate primary plane\n");
  266. return NULL;
  267. }
  268. if (formats == NULL) {
  269. formats = safe_modeset_formats;
  270. num_formats = ARRAY_SIZE(safe_modeset_formats);
  271. }
  272. /* possible_crtc's will be filled in later by crtc_init */
  273. ret = drm_plane_init(dev, primary, 0, &drm_primary_helper_funcs,
  274. formats, num_formats,
  275. DRM_PLANE_TYPE_PRIMARY);
  276. if (ret) {
  277. kfree(primary);
  278. primary = NULL;
  279. }
  280. return primary;
  281. }
  282. EXPORT_SYMBOL(drm_primary_helper_create_plane);