drm_plane_helper.c 18 KB

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