drm_plane.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #ifndef __DRM_PLANE_H__
  23. #define __DRM_PLANE_H__
  24. #include <linux/list.h>
  25. #include <linux/ctype.h>
  26. #include <drm/drm_mode_object.h>
  27. struct drm_crtc;
  28. struct drm_printer;
  29. struct drm_modeset_acquire_ctx;
  30. /**
  31. * struct drm_plane_state - mutable plane state
  32. * @plane: backpointer to the plane
  33. * @crtc_w: width of visible portion of plane on crtc
  34. * @crtc_h: height of visible portion of plane on crtc
  35. * @src_x: left position of visible portion of plane within
  36. * plane (in 16.16)
  37. * @src_y: upper position of visible portion of plane within
  38. * plane (in 16.16)
  39. * @src_w: width of visible portion of plane (in 16.16)
  40. * @src_h: height of visible portion of plane (in 16.16)
  41. * @rotation: rotation of the plane
  42. * @zpos: priority of the given plane on crtc (optional)
  43. * Note that multiple active planes on the same crtc can have an identical
  44. * zpos value. The rule to solving the conflict is to compare the plane
  45. * object IDs; the plane with a higher ID must be stacked on top of a
  46. * plane with a lower ID.
  47. * @normalized_zpos: normalized value of zpos: unique, range from 0 to N-1
  48. * where N is the number of active planes for given crtc. Note that
  49. * the driver must call drm_atomic_normalize_zpos() to update this before
  50. * it can be trusted.
  51. * @src: clipped source coordinates of the plane (in 16.16)
  52. * @dst: clipped destination coordinates of the plane
  53. * @state: backpointer to global drm_atomic_state
  54. */
  55. struct drm_plane_state {
  56. struct drm_plane *plane;
  57. /**
  58. * @crtc:
  59. *
  60. * Currently bound CRTC, NULL if disabled. Do not this write directly,
  61. * use drm_atomic_set_crtc_for_plane()
  62. */
  63. struct drm_crtc *crtc;
  64. /**
  65. * @fb:
  66. *
  67. * Currently bound framebuffer. Do not write this directly, use
  68. * drm_atomic_set_fb_for_plane()
  69. */
  70. struct drm_framebuffer *fb;
  71. /**
  72. * @fence:
  73. *
  74. * Optional fence to wait for before scanning out @fb. Do not write this
  75. * directly, use drm_atomic_set_fence_for_plane()
  76. */
  77. struct dma_fence *fence;
  78. /**
  79. * @crtc_x:
  80. *
  81. * Left position of visible portion of plane on crtc, signed dest
  82. * location allows it to be partially off screen.
  83. */
  84. int32_t crtc_x;
  85. /**
  86. * @crtc_y:
  87. *
  88. * Upper position of visible portion of plane on crtc, signed dest
  89. * location allows it to be partially off screen.
  90. */
  91. int32_t crtc_y;
  92. uint32_t crtc_w, crtc_h;
  93. /* Source values are 16.16 fixed point */
  94. uint32_t src_x, src_y;
  95. uint32_t src_h, src_w;
  96. /* Plane rotation */
  97. unsigned int rotation;
  98. /* Plane zpos */
  99. unsigned int zpos;
  100. unsigned int normalized_zpos;
  101. /* Clipped coordinates */
  102. struct drm_rect src, dst;
  103. /**
  104. * @visible:
  105. *
  106. * Visibility of the plane. This can be false even if fb!=NULL and
  107. * crtc!=NULL, due to clipping.
  108. */
  109. bool visible;
  110. struct drm_atomic_state *state;
  111. };
  112. static inline struct drm_rect
  113. drm_plane_state_src(const struct drm_plane_state *state)
  114. {
  115. struct drm_rect src = {
  116. .x1 = state->src_x,
  117. .y1 = state->src_y,
  118. .x2 = state->src_x + state->src_w,
  119. .y2 = state->src_y + state->src_h,
  120. };
  121. return src;
  122. }
  123. static inline struct drm_rect
  124. drm_plane_state_dest(const struct drm_plane_state *state)
  125. {
  126. struct drm_rect dest = {
  127. .x1 = state->crtc_x,
  128. .y1 = state->crtc_y,
  129. .x2 = state->crtc_x + state->crtc_w,
  130. .y2 = state->crtc_y + state->crtc_h,
  131. };
  132. return dest;
  133. }
  134. /**
  135. * struct drm_plane_funcs - driver plane control functions
  136. */
  137. struct drm_plane_funcs {
  138. /**
  139. * @update_plane:
  140. *
  141. * This is the legacy entry point to enable and configure the plane for
  142. * the given CRTC and framebuffer. It is never called to disable the
  143. * plane, i.e. the passed-in crtc and fb paramters are never NULL.
  144. *
  145. * The source rectangle in frame buffer memory coordinates is given by
  146. * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
  147. * values). Devices that don't support subpixel plane coordinates can
  148. * ignore the fractional part.
  149. *
  150. * The destination rectangle in CRTC coordinates is given by the
  151. * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
  152. * Devices scale the source rectangle to the destination rectangle. If
  153. * scaling is not supported, and the source rectangle size doesn't match
  154. * the destination rectangle size, the driver must return a
  155. * -<errorname>EINVAL</errorname> error.
  156. *
  157. * Drivers implementing atomic modeset should use
  158. * drm_atomic_helper_update_plane() to implement this hook.
  159. *
  160. * RETURNS:
  161. *
  162. * 0 on success or a negative error code on failure.
  163. */
  164. int (*update_plane)(struct drm_plane *plane,
  165. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  166. int crtc_x, int crtc_y,
  167. unsigned int crtc_w, unsigned int crtc_h,
  168. uint32_t src_x, uint32_t src_y,
  169. uint32_t src_w, uint32_t src_h,
  170. struct drm_modeset_acquire_ctx *ctx);
  171. /**
  172. * @disable_plane:
  173. *
  174. * This is the legacy entry point to disable the plane. The DRM core
  175. * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
  176. * with the frame buffer ID set to 0. Disabled planes must not be
  177. * processed by the CRTC.
  178. *
  179. * Drivers implementing atomic modeset should use
  180. * drm_atomic_helper_disable_plane() to implement this hook.
  181. *
  182. * RETURNS:
  183. *
  184. * 0 on success or a negative error code on failure.
  185. */
  186. int (*disable_plane)(struct drm_plane *plane,
  187. struct drm_modeset_acquire_ctx *ctx);
  188. /**
  189. * @destroy:
  190. *
  191. * Clean up plane resources. This is only called at driver unload time
  192. * through drm_mode_config_cleanup() since a plane cannot be hotplugged
  193. * in DRM.
  194. */
  195. void (*destroy)(struct drm_plane *plane);
  196. /**
  197. * @reset:
  198. *
  199. * Reset plane hardware and software state to off. This function isn't
  200. * called by the core directly, only through drm_mode_config_reset().
  201. * It's not a helper hook only for historical reasons.
  202. *
  203. * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
  204. * atomic state using this hook.
  205. */
  206. void (*reset)(struct drm_plane *plane);
  207. /**
  208. * @set_property:
  209. *
  210. * This is the legacy entry point to update a property attached to the
  211. * plane.
  212. *
  213. * Drivers implementing atomic modeset should use
  214. * drm_atomic_helper_plane_set_property() to implement this hook.
  215. *
  216. * This callback is optional if the driver does not support any legacy
  217. * driver-private properties.
  218. *
  219. * RETURNS:
  220. *
  221. * 0 on success or a negative error code on failure.
  222. */
  223. int (*set_property)(struct drm_plane *plane,
  224. struct drm_property *property, uint64_t val);
  225. /**
  226. * @atomic_duplicate_state:
  227. *
  228. * Duplicate the current atomic state for this plane and return it.
  229. * The core and helpers guarantee that any atomic state duplicated with
  230. * this hook and still owned by the caller (i.e. not transferred to the
  231. * driver by calling &drm_mode_config_funcs.atomic_commit) will be
  232. * cleaned up by calling the @atomic_destroy_state hook in this
  233. * structure.
  234. *
  235. * Atomic drivers which don't subclass &struct drm_plane_state should use
  236. * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
  237. * state structure to extend it with driver-private state should use
  238. * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
  239. * duplicated in a consistent fashion across drivers.
  240. *
  241. * It is an error to call this hook before &drm_plane.state has been
  242. * initialized correctly.
  243. *
  244. * NOTE:
  245. *
  246. * If the duplicate state references refcounted resources this hook must
  247. * acquire a reference for each of them. The driver must release these
  248. * references again in @atomic_destroy_state.
  249. *
  250. * RETURNS:
  251. *
  252. * Duplicated atomic state or NULL when the allocation failed.
  253. */
  254. struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
  255. /**
  256. * @atomic_destroy_state:
  257. *
  258. * Destroy a state duplicated with @atomic_duplicate_state and release
  259. * or unreference all resources it references
  260. */
  261. void (*atomic_destroy_state)(struct drm_plane *plane,
  262. struct drm_plane_state *state);
  263. /**
  264. * @atomic_set_property:
  265. *
  266. * Decode a driver-private property value and store the decoded value
  267. * into the passed-in state structure. Since the atomic core decodes all
  268. * standardized properties (even for extensions beyond the core set of
  269. * properties which might not be implemented by all drivers) this
  270. * requires drivers to subclass the state structure.
  271. *
  272. * Such driver-private properties should really only be implemented for
  273. * truly hardware/vendor specific state. Instead it is preferred to
  274. * standardize atomic extension and decode the properties used to expose
  275. * such an extension in the core.
  276. *
  277. * Do not call this function directly, use
  278. * drm_atomic_plane_set_property() instead.
  279. *
  280. * This callback is optional if the driver does not support any
  281. * driver-private atomic properties.
  282. *
  283. * NOTE:
  284. *
  285. * This function is called in the state assembly phase of atomic
  286. * modesets, which can be aborted for any reason (including on
  287. * userspace's request to just check whether a configuration would be
  288. * possible). Drivers MUST NOT touch any persistent state (hardware or
  289. * software) or data structures except the passed in @state parameter.
  290. *
  291. * Also since userspace controls in which order properties are set this
  292. * function must not do any input validation (since the state update is
  293. * incomplete and hence likely inconsistent). Instead any such input
  294. * validation must be done in the various atomic_check callbacks.
  295. *
  296. * RETURNS:
  297. *
  298. * 0 if the property has been found, -EINVAL if the property isn't
  299. * implemented by the driver (which shouldn't ever happen, the core only
  300. * asks for properties attached to this plane). No other validation is
  301. * allowed by the driver. The core already checks that the property
  302. * value is within the range (integer, valid enum value, ...) the driver
  303. * set when registering the property.
  304. */
  305. int (*atomic_set_property)(struct drm_plane *plane,
  306. struct drm_plane_state *state,
  307. struct drm_property *property,
  308. uint64_t val);
  309. /**
  310. * @atomic_get_property:
  311. *
  312. * Reads out the decoded driver-private property. This is used to
  313. * implement the GETPLANE IOCTL.
  314. *
  315. * Do not call this function directly, use
  316. * drm_atomic_plane_get_property() instead.
  317. *
  318. * This callback is optional if the driver does not support any
  319. * driver-private atomic properties.
  320. *
  321. * RETURNS:
  322. *
  323. * 0 on success, -EINVAL if the property isn't implemented by the
  324. * driver (which should never happen, the core only asks for
  325. * properties attached to this plane).
  326. */
  327. int (*atomic_get_property)(struct drm_plane *plane,
  328. const struct drm_plane_state *state,
  329. struct drm_property *property,
  330. uint64_t *val);
  331. /**
  332. * @late_register:
  333. *
  334. * This optional hook can be used to register additional userspace
  335. * interfaces attached to the plane like debugfs interfaces.
  336. * It is called late in the driver load sequence from drm_dev_register().
  337. * Everything added from this callback should be unregistered in
  338. * the early_unregister callback.
  339. *
  340. * Returns:
  341. *
  342. * 0 on success, or a negative error code on failure.
  343. */
  344. int (*late_register)(struct drm_plane *plane);
  345. /**
  346. * @early_unregister:
  347. *
  348. * This optional hook should be used to unregister the additional
  349. * userspace interfaces attached to the plane from
  350. * @late_register. It is called from drm_dev_unregister(),
  351. * early in the driver unload sequence to disable userspace access
  352. * before data structures are torndown.
  353. */
  354. void (*early_unregister)(struct drm_plane *plane);
  355. /**
  356. * @atomic_print_state:
  357. *
  358. * If driver subclasses &struct drm_plane_state, it should implement
  359. * this optional hook for printing additional driver specific state.
  360. *
  361. * Do not call this directly, use drm_atomic_plane_print_state()
  362. * instead.
  363. */
  364. void (*atomic_print_state)(struct drm_printer *p,
  365. const struct drm_plane_state *state);
  366. };
  367. /**
  368. * enum drm_plane_type - uapi plane type enumeration
  369. *
  370. * For historical reasons not all planes are made the same. This enumeration is
  371. * used to tell the different types of planes apart to implement the different
  372. * uapi semantics for them. For userspace which is universal plane aware and
  373. * which is using that atomic IOCTL there's no difference between these planes
  374. * (beyong what the driver and hardware can support of course).
  375. *
  376. * For compatibility with legacy userspace, only overlay planes are made
  377. * available to userspace by default. Userspace clients may set the
  378. * DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they
  379. * wish to receive a universal plane list containing all plane types. See also
  380. * drm_for_each_legacy_plane().
  381. *
  382. * WARNING: The values of this enum is UABI since they're exposed in the "type"
  383. * property.
  384. */
  385. enum drm_plane_type {
  386. /**
  387. * @DRM_PLANE_TYPE_OVERLAY:
  388. *
  389. * Overlay planes represent all non-primary, non-cursor planes. Some
  390. * drivers refer to these types of planes as "sprites" internally.
  391. */
  392. DRM_PLANE_TYPE_OVERLAY,
  393. /**
  394. * @DRM_PLANE_TYPE_PRIMARY:
  395. *
  396. * Primary planes represent a "main" plane for a CRTC. Primary planes
  397. * are the planes operated upon by CRTC modesetting and flipping
  398. * operations described in the &drm_crtc_funcs.page_flip and
  399. * &drm_crtc_funcs.set_config hooks.
  400. */
  401. DRM_PLANE_TYPE_PRIMARY,
  402. /**
  403. * @DRM_PLANE_TYPE_CURSOR:
  404. *
  405. * Cursor planes represent a "cursor" plane for a CRTC. Cursor planes
  406. * are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and
  407. * DRM_IOCTL_MODE_CURSOR2 IOCTLs.
  408. */
  409. DRM_PLANE_TYPE_CURSOR,
  410. };
  411. /**
  412. * struct drm_plane - central DRM plane control structure
  413. * @dev: DRM device this plane belongs to
  414. * @head: for list management
  415. * @name: human readable name, can be overwritten by the driver
  416. * @base: base mode object
  417. * @possible_crtcs: pipes this plane can be bound to
  418. * @format_types: array of formats supported by this plane
  419. * @format_count: number of formats supported
  420. * @format_default: driver hasn't supplied supported formats for the plane
  421. * @crtc: currently bound CRTC
  422. * @fb: currently bound fb
  423. * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
  424. * drm_mode_set_config_internal() to implement correct refcounting.
  425. * @funcs: helper functions
  426. * @properties: property tracking for this plane
  427. * @type: type of plane (overlay, primary, cursor)
  428. * @zpos_property: zpos property for this plane
  429. * @rotation_property: rotation property for this plane
  430. * @helper_private: mid-layer private data
  431. */
  432. struct drm_plane {
  433. struct drm_device *dev;
  434. struct list_head head;
  435. char *name;
  436. /**
  437. * @mutex:
  438. *
  439. * Protects modeset plane state, together with the &drm_crtc.mutex of
  440. * CRTC this plane is linked to (when active, getting activated or
  441. * getting disabled).
  442. *
  443. * For atomic drivers specifically this protects @state.
  444. */
  445. struct drm_modeset_lock mutex;
  446. struct drm_mode_object base;
  447. uint32_t possible_crtcs;
  448. uint32_t *format_types;
  449. unsigned int format_count;
  450. bool format_default;
  451. struct drm_crtc *crtc;
  452. struct drm_framebuffer *fb;
  453. struct drm_framebuffer *old_fb;
  454. const struct drm_plane_funcs *funcs;
  455. struct drm_object_properties properties;
  456. enum drm_plane_type type;
  457. /**
  458. * @index: Position inside the mode_config.list, can be used as an array
  459. * index. It is invariant over the lifetime of the plane.
  460. */
  461. unsigned index;
  462. const struct drm_plane_helper_funcs *helper_private;
  463. /**
  464. * @state:
  465. *
  466. * Current atomic state for this plane.
  467. *
  468. * This is protected by @mutex. Note that nonblocking atomic commits
  469. * access the current plane state without taking locks. Either by going
  470. * through the &struct drm_atomic_state pointers, see
  471. * for_each_plane_in_state(), for_each_oldnew_plane_in_state(),
  472. * for_each_old_plane_in_state() and for_each_new_plane_in_state(). Or
  473. * through careful ordering of atomic commit operations as implemented
  474. * in the atomic helpers, see &struct drm_crtc_commit.
  475. */
  476. struct drm_plane_state *state;
  477. struct drm_property *zpos_property;
  478. struct drm_property *rotation_property;
  479. };
  480. #define obj_to_plane(x) container_of(x, struct drm_plane, base)
  481. __printf(8, 9)
  482. int drm_universal_plane_init(struct drm_device *dev,
  483. struct drm_plane *plane,
  484. uint32_t possible_crtcs,
  485. const struct drm_plane_funcs *funcs,
  486. const uint32_t *formats,
  487. unsigned int format_count,
  488. enum drm_plane_type type,
  489. const char *name, ...);
  490. int drm_plane_init(struct drm_device *dev,
  491. struct drm_plane *plane,
  492. uint32_t possible_crtcs,
  493. const struct drm_plane_funcs *funcs,
  494. const uint32_t *formats, unsigned int format_count,
  495. bool is_primary);
  496. void drm_plane_cleanup(struct drm_plane *plane);
  497. /**
  498. * drm_plane_index - find the index of a registered plane
  499. * @plane: plane to find index for
  500. *
  501. * Given a registered plane, return the index of that plane within a DRM
  502. * device's list of planes.
  503. */
  504. static inline unsigned int drm_plane_index(struct drm_plane *plane)
  505. {
  506. return plane->index;
  507. }
  508. struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
  509. void drm_plane_force_disable(struct drm_plane *plane);
  510. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  511. struct drm_property *property,
  512. uint64_t value);
  513. /**
  514. * drm_plane_find - find a &drm_plane
  515. * @dev: DRM device
  516. * @id: plane id
  517. *
  518. * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
  519. * drm_mode_object_find().
  520. */
  521. static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
  522. uint32_t id)
  523. {
  524. struct drm_mode_object *mo;
  525. mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
  526. return mo ? obj_to_plane(mo) : NULL;
  527. }
  528. /**
  529. * drm_for_each_plane_mask - iterate over planes specified by bitmask
  530. * @plane: the loop cursor
  531. * @dev: the DRM device
  532. * @plane_mask: bitmask of plane indices
  533. *
  534. * Iterate over all planes specified by bitmask.
  535. */
  536. #define drm_for_each_plane_mask(plane, dev, plane_mask) \
  537. list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
  538. for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
  539. /**
  540. * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
  541. * @plane: the loop cursor
  542. * @dev: the DRM device
  543. *
  544. * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
  545. * This is useful for implementing userspace apis when userspace is not
  546. * universal plane aware. See also &enum drm_plane_type.
  547. */
  548. #define drm_for_each_legacy_plane(plane, dev) \
  549. list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
  550. for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  551. /**
  552. * drm_for_each_plane - iterate over all planes
  553. * @plane: the loop cursor
  554. * @dev: the DRM device
  555. *
  556. * Iterate over all planes of @dev, include primary and cursor planes.
  557. */
  558. #define drm_for_each_plane(plane, dev) \
  559. list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
  560. #endif