drm_plane.h 18 KB

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