drm_plane_helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_plane_helper.h>
  28. #include <drm/drm_rect.h>
  29. #include <drm/drm_plane_helper.h>
  30. #define SUBPIXEL_MASK 0xffff
  31. /*
  32. * This is the minimal list of formats that seem to be safe for modeset use
  33. * with all current DRM drivers. Most hardware can actually support more
  34. * formats than this and drivers may specify a more accurate list when
  35. * creating the primary plane. However drivers that still call
  36. * drm_plane_init() will use this minimal format list as the default.
  37. */
  38. static const uint32_t safe_modeset_formats[] = {
  39. DRM_FORMAT_XRGB8888,
  40. DRM_FORMAT_ARGB8888,
  41. };
  42. /*
  43. * Returns the connectors currently associated with a CRTC. This function
  44. * should be called twice: once with a NULL connector list to retrieve
  45. * the list size, and once with the properly allocated list to be filled in.
  46. */
  47. static int get_connectors_for_crtc(struct drm_crtc *crtc,
  48. struct drm_connector **connector_list,
  49. int num_connectors)
  50. {
  51. struct drm_device *dev = crtc->dev;
  52. struct drm_connector *connector;
  53. int count = 0;
  54. /*
  55. * Note: Once we change the plane hooks to more fine-grained locking we
  56. * need to grab the connection_mutex here to be able to make these
  57. * checks.
  58. */
  59. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  60. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  61. if (connector->encoder && connector->encoder->crtc == crtc) {
  62. if (connector_list != NULL && count < num_connectors)
  63. *(connector_list++) = connector;
  64. count++;
  65. }
  66. return count;
  67. }
  68. /**
  69. * drm_plane_helper_check_update() - Check plane update for validity
  70. * @plane: plane object to update
  71. * @crtc: owning CRTC of owning plane
  72. * @fb: framebuffer to flip onto plane
  73. * @src: source coordinates in 16.16 fixed point
  74. * @dest: integer destination coordinates
  75. * @clip: integer clipping coordinates
  76. * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
  77. * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
  78. * @can_position: is it legal to position the plane such that it
  79. * doesn't cover the entire crtc? This will generally
  80. * only be false for primary planes.
  81. * @can_update_disabled: can the plane be updated while the crtc
  82. * is disabled?
  83. * @visible: output parameter indicating whether plane is still visible after
  84. * clipping
  85. *
  86. * Checks that a desired plane update is valid. Drivers that provide
  87. * their own plane handling rather than helper-provided implementations may
  88. * still wish to call this function to avoid duplication of error checking
  89. * code.
  90. *
  91. * RETURNS:
  92. * Zero if update appears valid, error code on failure
  93. */
  94. int drm_plane_helper_check_update(struct drm_plane *plane,
  95. struct drm_crtc *crtc,
  96. struct drm_framebuffer *fb,
  97. struct drm_rect *src,
  98. struct drm_rect *dest,
  99. const struct drm_rect *clip,
  100. int min_scale,
  101. int max_scale,
  102. bool can_position,
  103. bool can_update_disabled,
  104. bool *visible)
  105. {
  106. int hscale, vscale;
  107. if (!crtc->enabled && !can_update_disabled) {
  108. DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
  109. return -EINVAL;
  110. }
  111. /* Check scaling */
  112. hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
  113. vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
  114. if (hscale < 0 || vscale < 0) {
  115. DRM_DEBUG_KMS("Invalid scaling of plane\n");
  116. return -ERANGE;
  117. }
  118. *visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
  119. if (!*visible)
  120. /*
  121. * Plane isn't visible; some drivers can handle this
  122. * so we just return success here. Drivers that can't
  123. * (including those that use the primary plane helper's
  124. * update function) will return an error from their
  125. * update_plane handler.
  126. */
  127. return 0;
  128. if (!can_position && !drm_rect_equals(dest, clip)) {
  129. DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
  130. return -EINVAL;
  131. }
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(drm_plane_helper_check_update);
  135. /**
  136. * drm_primary_helper_update() - Helper for primary plane update
  137. * @plane: plane object to update
  138. * @crtc: owning CRTC of owning plane
  139. * @fb: framebuffer to flip onto plane
  140. * @crtc_x: x offset of primary plane on crtc
  141. * @crtc_y: y offset of primary plane on crtc
  142. * @crtc_w: width of primary plane rectangle on crtc
  143. * @crtc_h: height of primary plane rectangle on crtc
  144. * @src_x: x offset of @fb for panning
  145. * @src_y: y offset of @fb for panning
  146. * @src_w: width of source rectangle in @fb
  147. * @src_h: height of source rectangle in @fb
  148. *
  149. * Provides a default plane update handler for primary planes. This is handler
  150. * is called in response to a userspace SetPlane operation on the plane with a
  151. * non-NULL framebuffer. We call the driver's modeset handler to update the
  152. * framebuffer.
  153. *
  154. * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
  155. * return an error.
  156. *
  157. * Note that we make some assumptions about hardware limitations that may not be
  158. * true for all hardware --
  159. * 1) Primary plane cannot be repositioned.
  160. * 2) Primary plane cannot be scaled.
  161. * 3) Primary plane must cover the entire CRTC.
  162. * 4) Subpixel positioning is not supported.
  163. * Drivers for hardware that don't have these restrictions can provide their
  164. * own implementation rather than using this helper.
  165. *
  166. * RETURNS:
  167. * Zero on success, error code on failure
  168. */
  169. int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  170. struct drm_framebuffer *fb,
  171. int crtc_x, int crtc_y,
  172. unsigned int crtc_w, unsigned int crtc_h,
  173. uint32_t src_x, uint32_t src_y,
  174. uint32_t src_w, uint32_t src_h)
  175. {
  176. struct drm_mode_set set = {
  177. .crtc = crtc,
  178. .fb = fb,
  179. .mode = &crtc->mode,
  180. .x = src_x >> 16,
  181. .y = src_y >> 16,
  182. };
  183. struct drm_rect src = {
  184. .x1 = src_x,
  185. .y1 = src_y,
  186. .x2 = src_x + src_w,
  187. .y2 = src_y + src_h,
  188. };
  189. struct drm_rect dest = {
  190. .x1 = crtc_x,
  191. .y1 = crtc_y,
  192. .x2 = crtc_x + crtc_w,
  193. .y2 = crtc_y + crtc_h,
  194. };
  195. const struct drm_rect clip = {
  196. .x2 = crtc->mode.hdisplay,
  197. .y2 = crtc->mode.vdisplay,
  198. };
  199. struct drm_connector **connector_list;
  200. int num_connectors, ret;
  201. bool visible;
  202. ret = drm_plane_helper_check_update(plane, crtc, fb,
  203. &src, &dest, &clip,
  204. DRM_PLANE_HELPER_NO_SCALING,
  205. DRM_PLANE_HELPER_NO_SCALING,
  206. false, false, &visible);
  207. if (ret)
  208. return ret;
  209. if (!visible)
  210. /*
  211. * Primary plane isn't visible. Note that unless a driver
  212. * provides their own disable function, this will just
  213. * wind up returning -EINVAL to userspace.
  214. */
  215. return plane->funcs->disable_plane(plane);
  216. /* Find current connectors for CRTC */
  217. num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
  218. BUG_ON(num_connectors == 0);
  219. connector_list = kzalloc(num_connectors * sizeof(*connector_list),
  220. GFP_KERNEL);
  221. if (!connector_list)
  222. return -ENOMEM;
  223. get_connectors_for_crtc(crtc, connector_list, num_connectors);
  224. set.connectors = connector_list;
  225. set.num_connectors = num_connectors;
  226. /*
  227. * We call set_config() directly here rather than using
  228. * drm_mode_set_config_internal. We're reprogramming the same
  229. * connectors that were already in use, so we shouldn't need the extra
  230. * cross-CRTC fb refcounting to accomodate stealing connectors.
  231. * drm_mode_setplane() already handles the basic refcounting for the
  232. * framebuffers involved in this operation.
  233. */
  234. ret = crtc->funcs->set_config(&set);
  235. kfree(connector_list);
  236. return ret;
  237. }
  238. EXPORT_SYMBOL(drm_primary_helper_update);
  239. /**
  240. * drm_primary_helper_disable() - Helper for primary plane disable
  241. * @plane: plane to disable
  242. *
  243. * Provides a default plane disable handler for primary planes. This is handler
  244. * is called in response to a userspace SetPlane operation on the plane with a
  245. * NULL framebuffer parameter. It unconditionally fails the disable call with
  246. * -EINVAL the only way to disable the primary plane without driver support is
  247. * to disable the entier CRTC. Which does not match the plane ->disable hook.
  248. *
  249. * Note that some hardware may be able to disable the primary plane without
  250. * disabling the whole CRTC. Drivers for such hardware should provide their
  251. * own disable handler that disables just the primary plane (and they'll likely
  252. * need to provide their own update handler as well to properly re-enable a
  253. * disabled primary plane).
  254. *
  255. * RETURNS:
  256. * Unconditionally returns -EINVAL.
  257. */
  258. int drm_primary_helper_disable(struct drm_plane *plane)
  259. {
  260. return -EINVAL;
  261. }
  262. EXPORT_SYMBOL(drm_primary_helper_disable);
  263. /**
  264. * drm_primary_helper_destroy() - Helper for primary plane destruction
  265. * @plane: plane to destroy
  266. *
  267. * Provides a default plane destroy handler for primary planes. This handler
  268. * is called during CRTC destruction. We disable the primary plane, remove
  269. * it from the DRM plane list, and deallocate the plane structure.
  270. */
  271. void drm_primary_helper_destroy(struct drm_plane *plane)
  272. {
  273. drm_plane_cleanup(plane);
  274. kfree(plane);
  275. }
  276. EXPORT_SYMBOL(drm_primary_helper_destroy);
  277. const struct drm_plane_funcs drm_primary_helper_funcs = {
  278. .update_plane = drm_primary_helper_update,
  279. .disable_plane = drm_primary_helper_disable,
  280. .destroy = drm_primary_helper_destroy,
  281. };
  282. EXPORT_SYMBOL(drm_primary_helper_funcs);
  283. /**
  284. * drm_primary_helper_create_plane() - Create a generic primary plane
  285. * @dev: drm device
  286. * @formats: pixel formats supported, or NULL for a default safe list
  287. * @num_formats: size of @formats; ignored if @formats is NULL
  288. *
  289. * Allocates and initializes a primary plane that can be used with the primary
  290. * plane helpers. Drivers that wish to use driver-specific plane structures or
  291. * provide custom handler functions may perform their own allocation and
  292. * initialization rather than calling this function.
  293. */
  294. struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
  295. const uint32_t *formats,
  296. int num_formats)
  297. {
  298. struct drm_plane *primary;
  299. int ret;
  300. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  301. if (primary == NULL) {
  302. DRM_DEBUG_KMS("Failed to allocate primary plane\n");
  303. return NULL;
  304. }
  305. if (formats == NULL) {
  306. formats = safe_modeset_formats;
  307. num_formats = ARRAY_SIZE(safe_modeset_formats);
  308. }
  309. /* possible_crtc's will be filled in later by crtc_init */
  310. ret = drm_plane_init(dev, primary, 0, &drm_primary_helper_funcs,
  311. formats, num_formats,
  312. DRM_PLANE_TYPE_PRIMARY);
  313. if (ret) {
  314. kfree(primary);
  315. primary = NULL;
  316. }
  317. return primary;
  318. }
  319. EXPORT_SYMBOL(drm_primary_helper_create_plane);
  320. /**
  321. * drm_crtc_init - Legacy CRTC initialization function
  322. * @dev: DRM device
  323. * @crtc: CRTC object to init
  324. * @funcs: callbacks for the new CRTC
  325. *
  326. * Initialize a CRTC object with a default helper-provided primary plane and no
  327. * cursor plane.
  328. *
  329. * Returns:
  330. * Zero on success, error code on failure.
  331. */
  332. int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  333. const struct drm_crtc_funcs *funcs)
  334. {
  335. struct drm_plane *primary;
  336. primary = drm_primary_helper_create_plane(dev, NULL, 0);
  337. return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
  338. }
  339. EXPORT_SYMBOL(drm_crtc_init);