drm_plane_helper.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 (!crtc->enabled && !can_update_disabled) {
  135. DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
  136. return -EINVAL;
  137. }
  138. /* Check scaling */
  139. hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
  140. vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
  141. if (hscale < 0 || vscale < 0) {
  142. DRM_DEBUG_KMS("Invalid scaling of plane\n");
  143. return -ERANGE;
  144. }
  145. if (!fb) {
  146. *visible = false;
  147. return 0;
  148. }
  149. *visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
  150. if (!*visible)
  151. /*
  152. * Plane isn't visible; some drivers can handle this
  153. * so we just return success here. Drivers that can't
  154. * (including those that use the primary plane helper's
  155. * update function) will return an error from their
  156. * update_plane handler.
  157. */
  158. return 0;
  159. if (!can_position && !drm_rect_equals(dest, clip)) {
  160. DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(drm_plane_helper_check_update);
  166. /**
  167. * drm_primary_helper_update() - Helper for primary plane update
  168. * @plane: plane object to update
  169. * @crtc: owning CRTC of owning plane
  170. * @fb: framebuffer to flip onto plane
  171. * @crtc_x: x offset of primary plane on crtc
  172. * @crtc_y: y offset of primary plane on crtc
  173. * @crtc_w: width of primary plane rectangle on crtc
  174. * @crtc_h: height of primary plane rectangle on crtc
  175. * @src_x: x offset of @fb for panning
  176. * @src_y: y offset of @fb for panning
  177. * @src_w: width of source rectangle in @fb
  178. * @src_h: height of source rectangle in @fb
  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. * 1) Primary plane cannot be repositioned.
  191. * 2) Primary plane cannot be scaled.
  192. * 3) Primary plane must cover the entire CRTC.
  193. * 4) Subpixel positioning is not supported.
  194. * Drivers for hardware that don't have these restrictions can provide their
  195. * own implementation rather than using this helper.
  196. *
  197. * RETURNS:
  198. * Zero on success, error code on failure
  199. */
  200. int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  201. struct drm_framebuffer *fb,
  202. int crtc_x, int crtc_y,
  203. unsigned int crtc_w, unsigned int crtc_h,
  204. uint32_t src_x, uint32_t src_y,
  205. uint32_t src_w, uint32_t src_h)
  206. {
  207. struct drm_mode_set set = {
  208. .crtc = crtc,
  209. .fb = fb,
  210. .mode = &crtc->mode,
  211. .x = src_x >> 16,
  212. .y = src_y >> 16,
  213. };
  214. struct drm_rect src = {
  215. .x1 = src_x,
  216. .y1 = src_y,
  217. .x2 = src_x + src_w,
  218. .y2 = src_y + src_h,
  219. };
  220. struct drm_rect dest = {
  221. .x1 = crtc_x,
  222. .y1 = crtc_y,
  223. .x2 = crtc_x + crtc_w,
  224. .y2 = crtc_y + crtc_h,
  225. };
  226. const struct drm_rect clip = {
  227. .x2 = crtc->mode.hdisplay,
  228. .y2 = crtc->mode.vdisplay,
  229. };
  230. struct drm_connector **connector_list;
  231. int num_connectors, ret;
  232. bool visible;
  233. ret = drm_plane_helper_check_update(plane, crtc, fb,
  234. &src, &dest, &clip,
  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);
  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 = kzalloc(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);
  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. *
  274. * Provides a default plane disable handler for primary planes. This is handler
  275. * is called in response to a userspace SetPlane operation on the plane with a
  276. * NULL framebuffer parameter. It unconditionally fails the disable call with
  277. * -EINVAL the only way to disable the primary plane without driver support is
  278. * to disable the entier CRTC. Which does not match the plane ->disable hook.
  279. *
  280. * Note that some hardware may be able to disable the primary plane without
  281. * disabling the whole CRTC. Drivers for such hardware should provide their
  282. * own disable handler that disables just the primary plane (and they'll likely
  283. * need to provide their own update handler as well to properly re-enable a
  284. * disabled primary plane).
  285. *
  286. * RETURNS:
  287. * Unconditionally returns -EINVAL.
  288. */
  289. int drm_primary_helper_disable(struct drm_plane *plane)
  290. {
  291. return -EINVAL;
  292. }
  293. EXPORT_SYMBOL(drm_primary_helper_disable);
  294. /**
  295. * drm_primary_helper_destroy() - Helper for primary plane destruction
  296. * @plane: plane to destroy
  297. *
  298. * Provides a default plane destroy handler for primary planes. This handler
  299. * is called during CRTC destruction. We disable the primary plane, remove
  300. * it from the DRM plane list, and deallocate the plane structure.
  301. */
  302. void drm_primary_helper_destroy(struct drm_plane *plane)
  303. {
  304. drm_plane_cleanup(plane);
  305. kfree(plane);
  306. }
  307. EXPORT_SYMBOL(drm_primary_helper_destroy);
  308. const struct drm_plane_funcs drm_primary_helper_funcs = {
  309. .update_plane = drm_primary_helper_update,
  310. .disable_plane = drm_primary_helper_disable,
  311. .destroy = drm_primary_helper_destroy,
  312. };
  313. EXPORT_SYMBOL(drm_primary_helper_funcs);
  314. /**
  315. * drm_primary_helper_create_plane() - Create a generic primary plane
  316. * @dev: drm device
  317. * @formats: pixel formats supported, or NULL for a default safe list
  318. * @num_formats: size of @formats; ignored if @formats is NULL
  319. *
  320. * Allocates and initializes a primary plane that can be used with the primary
  321. * plane helpers. Drivers that wish to use driver-specific plane structures or
  322. * provide custom handler functions may perform their own allocation and
  323. * initialization rather than calling this function.
  324. */
  325. struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
  326. const uint32_t *formats,
  327. int num_formats)
  328. {
  329. struct drm_plane *primary;
  330. int ret;
  331. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  332. if (primary == NULL) {
  333. DRM_DEBUG_KMS("Failed to allocate primary plane\n");
  334. return NULL;
  335. }
  336. if (formats == NULL) {
  337. formats = safe_modeset_formats;
  338. num_formats = ARRAY_SIZE(safe_modeset_formats);
  339. }
  340. /* possible_crtc's will be filled in later by crtc_init */
  341. ret = drm_universal_plane_init(dev, primary, 0,
  342. &drm_primary_helper_funcs,
  343. formats, num_formats,
  344. DRM_PLANE_TYPE_PRIMARY);
  345. if (ret) {
  346. kfree(primary);
  347. primary = NULL;
  348. }
  349. return primary;
  350. }
  351. EXPORT_SYMBOL(drm_primary_helper_create_plane);
  352. /**
  353. * drm_crtc_init - Legacy CRTC initialization function
  354. * @dev: DRM device
  355. * @crtc: CRTC object to init
  356. * @funcs: callbacks for the new CRTC
  357. *
  358. * Initialize a CRTC object with a default helper-provided primary plane and no
  359. * cursor plane.
  360. *
  361. * Returns:
  362. * Zero on success, error code on failure.
  363. */
  364. int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  365. const struct drm_crtc_funcs *funcs)
  366. {
  367. struct drm_plane *primary;
  368. primary = drm_primary_helper_create_plane(dev, NULL, 0);
  369. return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
  370. }
  371. EXPORT_SYMBOL(drm_crtc_init);
  372. int drm_plane_helper_commit(struct drm_plane *plane,
  373. struct drm_plane_state *plane_state,
  374. struct drm_framebuffer *old_fb)
  375. {
  376. struct drm_plane_helper_funcs *plane_funcs;
  377. struct drm_crtc *crtc[2];
  378. struct drm_crtc_helper_funcs *crtc_funcs[2];
  379. int i, ret = 0;
  380. plane_funcs = plane->helper_private;
  381. /* Since this is a transitional helper we can't assume that plane->state
  382. * is always valid. Hence we need to use plane->crtc instead of
  383. * plane->state->crtc as the old crtc. */
  384. crtc[0] = plane->crtc;
  385. crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
  386. for (i = 0; i < 2; i++)
  387. crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
  388. if (plane_funcs->atomic_check) {
  389. ret = plane_funcs->atomic_check(plane, plane_state);
  390. if (ret)
  391. goto out;
  392. }
  393. if (plane_funcs->prepare_fb && plane_state->fb) {
  394. ret = plane_funcs->prepare_fb(plane, plane_state->fb);
  395. if (ret)
  396. goto out;
  397. }
  398. /* Point of no return, commit sw state. */
  399. swap(plane->state, plane_state);
  400. for (i = 0; i < 2; i++) {
  401. if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
  402. crtc_funcs[i]->atomic_begin(crtc[i]);
  403. }
  404. plane_funcs->atomic_update(plane, plane_state);
  405. for (i = 0; i < 2; i++) {
  406. if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
  407. crtc_funcs[i]->atomic_flush(crtc[i]);
  408. }
  409. for (i = 0; i < 2; i++) {
  410. if (!crtc[i])
  411. continue;
  412. /* There's no other way to figure out whether the crtc is running. */
  413. ret = drm_crtc_vblank_get(crtc[i]);
  414. if (ret == 0) {
  415. drm_crtc_wait_one_vblank(crtc[i]);
  416. drm_crtc_vblank_put(crtc[i]);
  417. }
  418. ret = 0;
  419. }
  420. if (plane_funcs->cleanup_fb && old_fb)
  421. plane_funcs->cleanup_fb(plane, old_fb);
  422. out:
  423. if (plane_state) {
  424. if (plane->funcs->atomic_destroy_state)
  425. plane->funcs->atomic_destroy_state(plane, plane_state);
  426. else
  427. drm_atomic_helper_plane_destroy_state(plane, plane_state);
  428. }
  429. return ret;
  430. }
  431. /**
  432. * drm_plane_helper_update() - Helper for primary plane update
  433. * @plane: plane object to update
  434. * @crtc: owning CRTC of owning plane
  435. * @fb: framebuffer to flip onto plane
  436. * @crtc_x: x offset of primary plane on crtc
  437. * @crtc_y: y offset of primary plane on crtc
  438. * @crtc_w: width of primary plane rectangle on crtc
  439. * @crtc_h: height of primary plane rectangle on crtc
  440. * @src_x: x offset of @fb for panning
  441. * @src_y: y offset of @fb for panning
  442. * @src_w: width of source rectangle in @fb
  443. * @src_h: height of source rectangle in @fb
  444. *
  445. * Provides a default plane update handler using the atomic plane update
  446. * functions. It is fully left to the driver to check plane constraints and
  447. * handle corner-cases like a fully occluded or otherwise invisible plane.
  448. *
  449. * This is useful for piecewise transitioning of a driver to the atomic helpers.
  450. *
  451. * RETURNS:
  452. * Zero on success, error code on failure
  453. */
  454. int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
  455. struct drm_framebuffer *fb,
  456. int crtc_x, int crtc_y,
  457. unsigned int crtc_w, unsigned int crtc_h,
  458. uint32_t src_x, uint32_t src_y,
  459. uint32_t src_w, uint32_t src_h)
  460. {
  461. struct drm_plane_state *plane_state;
  462. if (plane->funcs->atomic_duplicate_state)
  463. plane_state = plane->funcs->atomic_duplicate_state(plane);
  464. else if (plane->state)
  465. plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  466. else
  467. plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
  468. if (!plane_state)
  469. return -ENOMEM;
  470. plane_state->crtc = crtc;
  471. drm_atomic_set_fb_for_plane(plane_state, fb);
  472. plane_state->crtc_x = crtc_x;
  473. plane_state->crtc_y = crtc_y;
  474. plane_state->crtc_h = crtc_h;
  475. plane_state->crtc_w = crtc_w;
  476. plane_state->src_x = src_x;
  477. plane_state->src_y = src_y;
  478. plane_state->src_h = src_h;
  479. plane_state->src_w = src_w;
  480. return drm_plane_helper_commit(plane, plane_state, plane->fb);
  481. }
  482. EXPORT_SYMBOL(drm_plane_helper_update);
  483. /**
  484. * drm_plane_helper_disable() - Helper for primary plane disable
  485. * @plane: plane to disable
  486. *
  487. * Provides a default plane disable handler using the atomic plane update
  488. * functions. It is fully left to the driver to check plane constraints and
  489. * handle corner-cases like a fully occluded or otherwise invisible plane.
  490. *
  491. * This is useful for piecewise transitioning of a driver to the atomic helpers.
  492. *
  493. * RETURNS:
  494. * Zero on success, error code on failure
  495. */
  496. int drm_plane_helper_disable(struct drm_plane *plane)
  497. {
  498. struct drm_plane_state *plane_state;
  499. /* crtc helpers love to call disable functions for already disabled hw
  500. * functions. So cope with that. */
  501. if (!plane->crtc)
  502. return 0;
  503. if (plane->funcs->atomic_duplicate_state)
  504. plane_state = plane->funcs->atomic_duplicate_state(plane);
  505. else if (plane->state)
  506. plane_state = drm_atomic_helper_plane_duplicate_state(plane);
  507. else
  508. plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
  509. if (!plane_state)
  510. return -ENOMEM;
  511. plane_state->crtc = NULL;
  512. drm_atomic_set_fb_for_plane(plane_state, NULL);
  513. return drm_plane_helper_commit(plane, plane_state, plane->fb);
  514. }
  515. EXPORT_SYMBOL(drm_plane_helper_disable);