drm_plane_helper.c 18 KB

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