drm_plane.h 19 KB

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