drm_plane_helper.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_atomic.h>
  30. #include <drm/drm_atomic_uapi.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/drm_encoder.h>
  33. #include <drm/drm_atomic_helper.h>
  34. #define SUBPIXEL_MASK 0xffff
  35. /**
  36. * DOC: overview
  37. *
  38. * This helper library has two parts. The first part has support to implement
  39. * primary plane support on top of the normal CRTC configuration interface.
  40. * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
  41. * plane together with the CRTC state this does not allow userspace to disable
  42. * the primary plane itself. To avoid too much duplicated code use
  43. * drm_plane_helper_check_update() which can be used to enforce the same
  44. * restrictions as primary planes had thus. The default primary plane only
  45. * expose XRBG8888 and ARGB8888 as valid pixel formats for the attached
  46. * framebuffer.
  47. *
  48. * Drivers are highly recommended to implement proper support for primary
  49. * planes, and newly merged drivers must not rely upon these transitional
  50. * helpers.
  51. *
  52. * The second part also implements transitional helpers which allow drivers to
  53. * gradually switch to the atomic helper infrastructure for plane updates. Once
  54. * that switch is complete drivers shouldn't use these any longer, instead using
  55. * the proper legacy implementations for update and disable plane hooks provided
  56. * by the atomic helpers.
  57. *
  58. * Again drivers are strongly urged to switch to the new interfaces.
  59. *
  60. * The plane helpers share the function table structures with other helpers,
  61. * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
  62. * the details.
  63. */
  64. /*
  65. * Returns the connectors currently associated with a CRTC. This function
  66. * should be called twice: once with a NULL connector list to retrieve
  67. * the list size, and once with the properly allocated list to be filled in.
  68. */
  69. static int get_connectors_for_crtc(struct drm_crtc *crtc,
  70. struct drm_connector **connector_list,
  71. int num_connectors)
  72. {
  73. struct drm_device *dev = crtc->dev;
  74. struct drm_connector *connector;
  75. struct drm_connector_list_iter conn_iter;
  76. int count = 0;
  77. /*
  78. * Note: Once we change the plane hooks to more fine-grained locking we
  79. * need to grab the connection_mutex here to be able to make these
  80. * checks.
  81. */
  82. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  83. drm_connector_list_iter_begin(dev, &conn_iter);
  84. drm_for_each_connector_iter(connector, &conn_iter) {
  85. if (connector->encoder && connector->encoder->crtc == crtc) {
  86. if (connector_list != NULL && count < num_connectors)
  87. *(connector_list++) = connector;
  88. count++;
  89. }
  90. }
  91. drm_connector_list_iter_end(&conn_iter);
  92. return count;
  93. }
  94. /**
  95. * drm_plane_helper_check_update() - Check plane update for validity
  96. * @plane: plane object to update
  97. * @crtc: owning CRTC of owning plane
  98. * @fb: framebuffer to flip onto plane
  99. * @src: source coordinates in 16.16 fixed point
  100. * @dst: integer destination coordinates
  101. * @rotation: plane rotation
  102. * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
  103. * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
  104. * @can_position: is it legal to position the plane such that it
  105. * doesn't cover the entire crtc? This will generally
  106. * only be false for primary planes.
  107. * @can_update_disabled: can the plane be updated while the crtc
  108. * is disabled?
  109. * @visible: output parameter indicating whether plane is still visible after
  110. * clipping
  111. *
  112. * Checks that a desired plane update is valid. Drivers that provide
  113. * their own plane handling rather than helper-provided implementations may
  114. * still wish to call this function to avoid duplication of error checking
  115. * code.
  116. *
  117. * RETURNS:
  118. * Zero if update appears valid, error code on failure
  119. */
  120. int drm_plane_helper_check_update(struct drm_plane *plane,
  121. struct drm_crtc *crtc,
  122. struct drm_framebuffer *fb,
  123. struct drm_rect *src,
  124. struct drm_rect *dst,
  125. unsigned int rotation,
  126. int min_scale,
  127. int max_scale,
  128. bool can_position,
  129. bool can_update_disabled,
  130. bool *visible)
  131. {
  132. struct drm_plane_state plane_state = {
  133. .plane = plane,
  134. .crtc = crtc,
  135. .fb = fb,
  136. .src_x = src->x1,
  137. .src_y = src->y1,
  138. .src_w = drm_rect_width(src),
  139. .src_h = drm_rect_height(src),
  140. .crtc_x = dst->x1,
  141. .crtc_y = dst->y1,
  142. .crtc_w = drm_rect_width(dst),
  143. .crtc_h = drm_rect_height(dst),
  144. .rotation = rotation,
  145. .visible = *visible,
  146. };
  147. struct drm_crtc_state crtc_state = {
  148. .crtc = crtc,
  149. .enable = crtc->enabled,
  150. .mode = crtc->mode,
  151. };
  152. int ret;
  153. ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
  154. min_scale, max_scale,
  155. can_position,
  156. can_update_disabled);
  157. if (ret)
  158. return ret;
  159. *src = plane_state.src;
  160. *dst = plane_state.dst;
  161. *visible = plane_state.visible;
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(drm_plane_helper_check_update);
  165. /**
  166. * drm_primary_helper_update() - Helper for primary plane update
  167. * @plane: plane object to update
  168. * @crtc: owning CRTC of owning plane
  169. * @fb: framebuffer to flip onto plane
  170. * @crtc_x: x offset of primary plane on crtc
  171. * @crtc_y: y offset of primary plane on crtc
  172. * @crtc_w: width of primary plane rectangle on crtc
  173. * @crtc_h: height of primary plane rectangle on crtc
  174. * @src_x: x offset of @fb for panning
  175. * @src_y: y offset of @fb for panning
  176. * @src_w: width of source rectangle in @fb
  177. * @src_h: height of source rectangle in @fb
  178. * @ctx: lock acquire context, not used here
  179. *
  180. * Provides a default plane update handler for primary planes. This is handler
  181. * is called in response to a userspace SetPlane operation on the plane with a
  182. * non-NULL framebuffer. We call the driver's modeset handler to update the
  183. * framebuffer.
  184. *
  185. * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
  186. * return an error.
  187. *
  188. * Note that we make some assumptions about hardware limitations that may not be
  189. * true for all hardware --
  190. *
  191. * 1. Primary plane cannot be repositioned.
  192. * 2. Primary plane cannot be scaled.
  193. * 3. Primary plane must cover the entire CRTC.
  194. * 4. Subpixel positioning is not supported.
  195. *
  196. * Drivers for hardware that don't have these restrictions can provide their
  197. * own implementation rather than using this helper.
  198. *
  199. * RETURNS:
  200. * Zero on success, error code on failure
  201. */
  202. int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  203. struct drm_framebuffer *fb,
  204. int crtc_x, int crtc_y,
  205. unsigned int crtc_w, unsigned int crtc_h,
  206. uint32_t src_x, uint32_t src_y,
  207. uint32_t src_w, uint32_t src_h,
  208. struct drm_modeset_acquire_ctx *ctx)
  209. {
  210. struct drm_mode_set set = {
  211. .crtc = crtc,
  212. .fb = fb,
  213. .mode = &crtc->mode,
  214. .x = src_x >> 16,
  215. .y = src_y >> 16,
  216. };
  217. struct drm_rect src = {
  218. .x1 = src_x,
  219. .y1 = src_y,
  220. .x2 = src_x + src_w,
  221. .y2 = src_y + src_h,
  222. };
  223. struct drm_rect dest = {
  224. .x1 = crtc_x,
  225. .y1 = crtc_y,
  226. .x2 = crtc_x + crtc_w,
  227. .y2 = crtc_y + crtc_h,
  228. };
  229. struct drm_connector **connector_list;
  230. int num_connectors, ret;
  231. bool visible;
  232. ret = drm_plane_helper_check_update(plane, crtc, fb,
  233. &src, &dest,
  234. DRM_MODE_ROTATE_0,
  235. DRM_PLANE_HELPER_NO_SCALING,
  236. DRM_PLANE_HELPER_NO_SCALING,
  237. false, false, &visible);
  238. if (ret)
  239. return ret;
  240. if (!visible)
  241. /*
  242. * Primary plane isn't visible. Note that unless a driver
  243. * provides their own disable function, this will just
  244. * wind up returning -EINVAL to userspace.
  245. */
  246. return plane->funcs->disable_plane(plane, ctx);
  247. /* Find current connectors for CRTC */
  248. num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
  249. BUG_ON(num_connectors == 0);
  250. connector_list = kcalloc(num_connectors, sizeof(*connector_list),
  251. GFP_KERNEL);
  252. if (!connector_list)
  253. return -ENOMEM;
  254. get_connectors_for_crtc(crtc, connector_list, num_connectors);
  255. set.connectors = connector_list;
  256. set.num_connectors = num_connectors;
  257. /*
  258. * We call set_config() directly here rather than using
  259. * drm_mode_set_config_internal. We're reprogramming the same
  260. * connectors that were already in use, so we shouldn't need the extra
  261. * cross-CRTC fb refcounting to accomodate stealing connectors.
  262. * drm_mode_setplane() already handles the basic refcounting for the
  263. * framebuffers involved in this operation.
  264. */
  265. ret = crtc->funcs->set_config(&set, ctx);
  266. kfree(connector_list);
  267. return ret;
  268. }
  269. EXPORT_SYMBOL(drm_primary_helper_update);
  270. /**
  271. * drm_primary_helper_disable() - Helper for primary plane disable
  272. * @plane: plane to disable
  273. * @ctx: lock acquire context, not used here
  274. *
  275. * Provides a default plane disable handler for primary planes. This is handler
  276. * is called in response to a userspace SetPlane operation on the plane with a
  277. * NULL framebuffer parameter. It unconditionally fails the disable call with
  278. * -EINVAL the only way to disable the primary plane without driver support is
  279. * to disable the entire CRTC. Which does not match the plane
  280. * &drm_plane_funcs.disable_plane hook.
  281. *
  282. * Note that some hardware may be able to disable the primary plane without
  283. * disabling the whole CRTC. Drivers for such hardware should provide their
  284. * own disable handler that disables just the primary plane (and they'll likely
  285. * need to provide their own update handler as well to properly re-enable a
  286. * disabled primary plane).
  287. *
  288. * RETURNS:
  289. * Unconditionally returns -EINVAL.
  290. */
  291. int drm_primary_helper_disable(struct drm_plane *plane,
  292. struct drm_modeset_acquire_ctx *ctx)
  293. {
  294. return -EINVAL;
  295. }
  296. EXPORT_SYMBOL(drm_primary_helper_disable);
  297. /**
  298. * drm_primary_helper_destroy() - Helper for primary plane destruction
  299. * @plane: plane to destroy
  300. *
  301. * Provides a default plane destroy handler for primary planes. This handler
  302. * is called during CRTC destruction. We disable the primary plane, remove
  303. * it from the DRM plane list, and deallocate the plane structure.
  304. */
  305. void drm_primary_helper_destroy(struct drm_plane *plane)
  306. {
  307. drm_plane_cleanup(plane);
  308. kfree(plane);
  309. }
  310. EXPORT_SYMBOL(drm_primary_helper_destroy);
  311. const struct drm_plane_funcs drm_primary_helper_funcs = {
  312. .update_plane = drm_primary_helper_update,
  313. .disable_plane = drm_primary_helper_disable,
  314. .destroy = drm_primary_helper_destroy,
  315. };
  316. EXPORT_SYMBOL(drm_primary_helper_funcs);
  317. int drm_plane_helper_commit(struct drm_plane *plane,
  318. struct drm_plane_state *plane_state,
  319. struct drm_framebuffer *old_fb)
  320. {
  321. const struct drm_plane_helper_funcs *plane_funcs;
  322. struct drm_crtc *crtc[2];
  323. const struct drm_crtc_helper_funcs *crtc_funcs[2];
  324. int i, ret = 0;
  325. plane_funcs = plane->helper_private;
  326. /* Since this is a transitional helper we can't assume that plane->state
  327. * is always valid. Hence we need to use plane->crtc instead of
  328. * plane->state->crtc as the old crtc. */
  329. crtc[0] = plane->crtc;
  330. crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
  331. for (i = 0; i < 2; i++)
  332. crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
  333. if (plane_funcs->atomic_check) {
  334. ret = plane_funcs->atomic_check(plane, plane_state);
  335. if (ret)
  336. goto out;
  337. }
  338. if (plane_funcs->prepare_fb && plane_state->fb != old_fb) {
  339. ret = plane_funcs->prepare_fb(plane,
  340. plane_state);
  341. if (ret)
  342. goto out;
  343. }
  344. /* Point of no return, commit sw state. */
  345. swap(plane->state, plane_state);
  346. for (i = 0; i < 2; i++) {
  347. if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
  348. crtc_funcs[i]->atomic_begin(crtc[i], crtc[i]->state);
  349. }
  350. /*
  351. * Drivers may optionally implement the ->atomic_disable callback, so
  352. * special-case that here.
  353. */
  354. if (drm_atomic_plane_disabling(plane_state, plane->state) &&
  355. plane_funcs->atomic_disable)
  356. plane_funcs->atomic_disable(plane, plane_state);
  357. else
  358. plane_funcs->atomic_update(plane, plane_state);
  359. for (i = 0; i < 2; i++) {
  360. if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
  361. crtc_funcs[i]->atomic_flush(crtc[i], crtc[i]->state);
  362. }
  363. /*
  364. * If we only moved the plane and didn't change fb's, there's no need to
  365. * wait for vblank.
  366. */
  367. if (plane->state->fb == old_fb)
  368. goto out;
  369. for (i = 0; i < 2; i++) {
  370. if (!crtc[i])
  371. continue;
  372. if (crtc[i]->cursor == plane)
  373. continue;
  374. /* There's no other way to figure out whether the crtc is running. */
  375. ret = drm_crtc_vblank_get(crtc[i]);
  376. if (ret == 0) {
  377. drm_crtc_wait_one_vblank(crtc[i]);
  378. drm_crtc_vblank_put(crtc[i]);
  379. }
  380. ret = 0;
  381. }
  382. if (plane_funcs->cleanup_fb)
  383. plane_funcs->cleanup_fb(plane, plane_state);
  384. out:
  385. if (plane->funcs->atomic_destroy_state)
  386. plane->funcs->atomic_destroy_state(plane, plane_state);
  387. else
  388. drm_atomic_helper_plane_destroy_state(plane, plane_state);
  389. return ret;
  390. }
  391. /**
  392. * drm_plane_helper_update() - Transitional helper for plane update
  393. * @plane: plane object to update
  394. * @crtc: owning CRTC of owning plane
  395. * @fb: framebuffer to flip onto plane
  396. * @crtc_x: x offset of primary plane on crtc
  397. * @crtc_y: y offset of primary plane on crtc
  398. * @crtc_w: width of primary plane rectangle on crtc
  399. * @crtc_h: height of primary plane rectangle on crtc
  400. * @src_x: x offset of @fb for panning
  401. * @src_y: y offset of @fb for panning
  402. * @src_w: width of source rectangle in @fb
  403. * @src_h: height of source rectangle in @fb
  404. * @ctx: lock acquire context, not used here
  405. *
  406. * Provides a default plane update handler using the atomic plane update
  407. * functions. It is fully left to the driver to check plane constraints and
  408. * handle corner-cases like a fully occluded or otherwise invisible plane.
  409. *
  410. * This is useful for piecewise transitioning of a driver to the atomic helpers.
  411. *
  412. * RETURNS:
  413. * Zero on success, error code on failure
  414. */
  415. int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  416. struct drm_framebuffer *fb,
  417. int crtc_x, int crtc_y,
  418. unsigned int crtc_w, unsigned int crtc_h,
  419. uint32_t src_x, uint32_t src_y,
  420. uint32_t src_w, uint32_t src_h,
  421. struct drm_modeset_acquire_ctx *ctx)
  422. {
  423. struct drm_plane_state *plane_state;
  424. if (plane->funcs->atomic_duplicate_state)
  425. plane_state = plane->funcs->atomic_duplicate_state(plane);
  426. else {
  427. if (!plane->state)
  428. drm_atomic_helper_plane_reset(plane);
  429. plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  430. }
  431. if (!plane_state)
  432. return -ENOMEM;
  433. plane_state->plane = plane;
  434. plane_state->crtc = crtc;
  435. drm_atomic_set_fb_for_plane(plane_state, fb);
  436. plane_state->crtc_x = crtc_x;
  437. plane_state->crtc_y = crtc_y;
  438. plane_state->crtc_h = crtc_h;
  439. plane_state->crtc_w = crtc_w;
  440. plane_state->src_x = src_x;
  441. plane_state->src_y = src_y;
  442. plane_state->src_h = src_h;
  443. plane_state->src_w = src_w;
  444. return drm_plane_helper_commit(plane, plane_state, plane->fb);
  445. }
  446. EXPORT_SYMBOL(drm_plane_helper_update);
  447. /**
  448. * drm_plane_helper_disable() - Transitional helper for plane disable
  449. * @plane: plane to disable
  450. * @ctx: lock acquire context, not used here
  451. *
  452. * Provides a default plane disable handler using the atomic plane update
  453. * functions. It is fully left to the driver to check plane constraints and
  454. * handle corner-cases like a fully occluded or otherwise invisible plane.
  455. *
  456. * This is useful for piecewise transitioning of a driver to the atomic helpers.
  457. *
  458. * RETURNS:
  459. * Zero on success, error code on failure
  460. */
  461. int drm_plane_helper_disable(struct drm_plane *plane,
  462. struct drm_modeset_acquire_ctx *ctx)
  463. {
  464. struct drm_plane_state *plane_state;
  465. struct drm_framebuffer *old_fb;
  466. /* crtc helpers love to call disable functions for already disabled hw
  467. * functions. So cope with that. */
  468. if (!plane->crtc)
  469. return 0;
  470. if (plane->funcs->atomic_duplicate_state)
  471. plane_state = plane->funcs->atomic_duplicate_state(plane);
  472. else {
  473. if (!plane->state)
  474. drm_atomic_helper_plane_reset(plane);
  475. plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  476. }
  477. if (!plane_state)
  478. return -ENOMEM;
  479. plane_state->plane = plane;
  480. plane_state->crtc = NULL;
  481. old_fb = plane_state->fb;
  482. drm_atomic_set_fb_for_plane(plane_state, NULL);
  483. return drm_plane_helper_commit(plane, plane_state, old_fb);
  484. }
  485. EXPORT_SYMBOL(drm_plane_helper_disable);