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 
23 #ifndef __DRM_PLANE_H__
24 #define __DRM_PLANE_H__
25 
26 #include <linux/list.h>
27 #include <linux/ctype.h>
28 #include <drm/drm_mode_object.h>
29 #include <drm/drm_color_mgmt.h>
30 #include <drm/drm_rect.h>
31 #include <drm/drm_modeset_lock.h>
32 #include <drm/drm_util.h>
33 
34 struct drm_crtc;
35 struct drm_printer;
36 struct drm_modeset_acquire_ctx;
37 
38 enum drm_scaling_filter {
39 	DRM_SCALING_FILTER_DEFAULT,
40 	DRM_SCALING_FILTER_NEAREST_NEIGHBOR,
41 };
42 
43 /**
44  * struct drm_plane_state - mutable plane state
45  *
46  * Please note that the destination coordinates @crtc_x, @crtc_y, @crtc_h and
47  * @crtc_w and the source coordinates @src_x, @src_y, @src_h and @src_w are the
48  * raw coordinates provided by userspace. Drivers should use
49  * drm_atomic_helper_check_plane_state() and only use the derived rectangles in
50  * @src and @dst to program the hardware.
51  */
52 struct drm_plane_state {
53 	/** @plane: backpointer to the plane */
54 	struct drm_plane *plane;
55 
56 	/**
57 	 * @crtc:
58 	 *
59 	 * Currently bound CRTC, NULL if disabled. Do not write this directly,
60 	 * use drm_atomic_set_crtc_for_plane()
61 	 */
62 	struct drm_crtc *crtc;
63 
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 	/**
73 	 * @fence:
74 	 *
75 	 * Optional fence to wait for before scanning out @fb. The core atomic
76 	 * code will set this when userspace is using explicit fencing. Do not
77 	 * write this field directly for a driver's implicit fence.
78 	 *
79 	 * Drivers should store any implicit fence in this from their
80 	 * &drm_plane_helper_funcs.prepare_fb callback. See
81 	 * drm_gem_plane_helper_prepare_fb() for a suitable helper.
82 	 */
83 	struct dma_fence *fence;
84 
85 	/**
86 	 * @crtc_x:
87 	 *
88 	 * Left position of visible portion of plane on crtc, signed dest
89 	 * location allows it to be partially off screen.
90 	 */
91 
92 	int32_t crtc_x;
93 	/**
94 	 * @crtc_y:
95 	 *
96 	 * Upper position of visible portion of plane on crtc, signed dest
97 	 * location allows it to be partially off screen.
98 	 */
99 	int32_t crtc_y;
100 
101 	/** @crtc_w: width of visible portion of plane on crtc */
102 	/** @crtc_h: height of visible portion of plane on crtc */
103 	uint32_t crtc_w, crtc_h;
104 
105 	/**
106 	 * @src_x: left position of visible portion of plane within plane (in
107 	 * 16.16 fixed point).
108 	 */
109 	uint32_t src_x;
110 	/**
111 	 * @src_y: upper position of visible portion of plane within plane (in
112 	 * 16.16 fixed point).
113 	 */
114 	uint32_t src_y;
115 	/** @src_w: width of visible portion of plane (in 16.16) */
116 	/** @src_h: height of visible portion of plane (in 16.16) */
117 	uint32_t src_h, src_w;
118 
119 	/**
120 	 * @alpha:
121 	 * Opacity of the plane with 0 as completely transparent and 0xffff as
122 	 * completely opaque. See drm_plane_create_alpha_property() for more
123 	 * details.
124 	 */
125 	u16 alpha;
126 
127 	/**
128 	 * @pixel_blend_mode:
129 	 * The alpha blending equation selection, describing how the pixels from
130 	 * the current plane are composited with the background. Value can be
131 	 * one of DRM_MODE_BLEND_*
132 	 */
133 	uint16_t pixel_blend_mode;
134 
135 	/**
136 	 * @rotation:
137 	 * Rotation of the plane. See drm_plane_create_rotation_property() for
138 	 * more details.
139 	 */
140 	unsigned int rotation;
141 
142 	/**
143 	 * @zpos:
144 	 * Priority of the given plane on crtc (optional).
145 	 *
146 	 * User-space may set mutable zpos properties so that multiple active
147 	 * planes on the same CRTC have identical zpos values. This is a
148 	 * user-space bug, but drivers can solve the conflict by comparing the
149 	 * plane object IDs; the plane with a higher ID is stacked on top of a
150 	 * plane with a lower ID.
151 	 *
152 	 * See drm_plane_create_zpos_property() and
153 	 * drm_plane_create_zpos_immutable_property() for more details.
154 	 */
155 	unsigned int zpos;
156 
157 	/**
158 	 * @normalized_zpos:
159 	 * Normalized value of zpos: unique, range from 0 to N-1 where N is the
160 	 * number of active planes for given crtc. Note that the driver must set
161 	 * &drm_mode_config.normalize_zpos or call drm_atomic_normalize_zpos() to
162 	 * update this before it can be trusted.
163 	 */
164 	unsigned int normalized_zpos;
165 
166 	/**
167 	 * @color_encoding:
168 	 *
169 	 * Color encoding for non RGB formats
170 	 */
171 	enum drm_color_encoding color_encoding;
172 
173 	/**
174 	 * @color_range:
175 	 *
176 	 * Color range for non RGB formats
177 	 */
178 	enum drm_color_range color_range;
179 
180 	/**
181 	 * @fb_damage_clips:
182 	 *
183 	 * Blob representing damage (area in plane framebuffer that changed
184 	 * since last plane update) as an array of &drm_mode_rect in framebuffer
185 	 * coodinates of the attached framebuffer. Note that unlike plane src,
186 	 * damage clips are not in 16.16 fixed point.
187 	 *
188 	 * See drm_plane_get_damage_clips() and
189 	 * drm_plane_get_damage_clips_count() for accessing these.
190 	 */
191 	struct drm_property_blob *fb_damage_clips;
192 
193 	/**
194 	 * @ignore_damage_clips:
195 	 *
196 	 * Set by drivers to indicate the drm_atomic_helper_damage_iter_init()
197 	 * helper that the @fb_damage_clips blob property should be ignored.
198 	 *
199 	 * See :ref:`damage_tracking_properties` for more information.
200 	 */
201 	bool ignore_damage_clips;
202 
203 	/**
204 	 * @src:
205 	 *
206 	 * source coordinates of the plane (in 16.16).
207 	 *
208 	 * When using drm_atomic_helper_check_plane_state(),
209 	 * the coordinates are clipped, but the driver may choose
210 	 * to use unclipped coordinates instead when the hardware
211 	 * performs the clipping automatically.
212 	 */
213 	/**
214 	 * @dst:
215 	 *
216 	 * clipped destination coordinates of the plane.
217 	 *
218 	 * When using drm_atomic_helper_check_plane_state(),
219 	 * the coordinates are clipped, but the driver may choose
220 	 * to use unclipped coordinates instead when the hardware
221 	 * performs the clipping automatically.
222 	 */
223 	struct drm_rect src, dst;
224 
225 	/**
226 	 * @visible:
227 	 *
228 	 * Visibility of the plane. This can be false even if fb!=NULL and
229 	 * crtc!=NULL, due to clipping.
230 	 */
231 	bool visible;
232 
233 	/**
234 	 * @scaling_filter:
235 	 *
236 	 * Scaling filter to be applied
237 	 */
238 	enum drm_scaling_filter scaling_filter;
239 
240 	/**
241 	 * @commit: Tracks the pending commit to prevent use-after-free conditions,
242 	 * and for async plane updates.
243 	 *
244 	 * May be NULL.
245 	 */
246 	struct drm_crtc_commit *commit;
247 
248 	/** @state: backpointer to global drm_atomic_state */
249 	struct drm_atomic_state *state;
250 };
251 
252 static inline struct drm_rect
drm_plane_state_src(const struct drm_plane_state * state)253 drm_plane_state_src(const struct drm_plane_state *state)
254 {
255 	struct drm_rect src = {
256 		.x1 = state->src_x,
257 		.y1 = state->src_y,
258 		.x2 = state->src_x + state->src_w,
259 		.y2 = state->src_y + state->src_h,
260 	};
261 	return src;
262 }
263 
264 static inline struct drm_rect
drm_plane_state_dest(const struct drm_plane_state * state)265 drm_plane_state_dest(const struct drm_plane_state *state)
266 {
267 	struct drm_rect dest = {
268 		.x1 = state->crtc_x,
269 		.y1 = state->crtc_y,
270 		.x2 = state->crtc_x + state->crtc_w,
271 		.y2 = state->crtc_y + state->crtc_h,
272 	};
273 	return dest;
274 }
275 
276 /**
277  * struct drm_plane_funcs - driver plane control functions
278  */
279 struct drm_plane_funcs {
280 	/**
281 	 * @update_plane:
282 	 *
283 	 * This is the legacy entry point to enable and configure the plane for
284 	 * the given CRTC and framebuffer. It is never called to disable the
285 	 * plane, i.e. the passed-in crtc and fb paramters are never NULL.
286 	 *
287 	 * The source rectangle in frame buffer memory coordinates is given by
288 	 * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
289 	 * values). Devices that don't support subpixel plane coordinates can
290 	 * ignore the fractional part.
291 	 *
292 	 * The destination rectangle in CRTC coordinates is given by the
293 	 * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
294 	 * Devices scale the source rectangle to the destination rectangle. If
295 	 * scaling is not supported, and the source rectangle size doesn't match
296 	 * the destination rectangle size, the driver must return a
297 	 * -<errorname>EINVAL</errorname> error.
298 	 *
299 	 * Drivers implementing atomic modeset should use
300 	 * drm_atomic_helper_update_plane() to implement this hook.
301 	 *
302 	 * RETURNS:
303 	 *
304 	 * 0 on success or a negative error code on failure.
305 	 */
306 	int (*update_plane)(struct drm_plane *plane,
307 			    struct drm_crtc *crtc, struct drm_framebuffer *fb,
308 			    int crtc_x, int crtc_y,
309 			    unsigned int crtc_w, unsigned int crtc_h,
310 			    uint32_t src_x, uint32_t src_y,
311 			    uint32_t src_w, uint32_t src_h,
312 			    struct drm_modeset_acquire_ctx *ctx);
313 
314 	/**
315 	 * @disable_plane:
316 	 *
317 	 * This is the legacy entry point to disable the plane. The DRM core
318 	 * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
319 	 * with the frame buffer ID set to 0.  Disabled planes must not be
320 	 * processed by the CRTC.
321 	 *
322 	 * Drivers implementing atomic modeset should use
323 	 * drm_atomic_helper_disable_plane() to implement this hook.
324 	 *
325 	 * RETURNS:
326 	 *
327 	 * 0 on success or a negative error code on failure.
328 	 */
329 	int (*disable_plane)(struct drm_plane *plane,
330 			     struct drm_modeset_acquire_ctx *ctx);
331 
332 	/**
333 	 * @destroy:
334 	 *
335 	 * Clean up plane resources. This is only called at driver unload time
336 	 * through drm_mode_config_cleanup() since a plane cannot be hotplugged
337 	 * in DRM.
338 	 */
339 	void (*destroy)(struct drm_plane *plane);
340 
341 	/**
342 	 * @reset:
343 	 *
344 	 * Reset plane hardware and software state to off. This function isn't
345 	 * called by the core directly, only through drm_mode_config_reset().
346 	 * It's not a helper hook only for historical reasons.
347 	 *
348 	 * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
349 	 * atomic state using this hook.
350 	 */
351 	void (*reset)(struct drm_plane *plane);
352 
353 	/**
354 	 * @set_property:
355 	 *
356 	 * This is the legacy entry point to update a property attached to the
357 	 * plane.
358 	 *
359 	 * This callback is optional if the driver does not support any legacy
360 	 * driver-private properties. For atomic drivers it is not used because
361 	 * property handling is done entirely in the DRM core.
362 	 *
363 	 * RETURNS:
364 	 *
365 	 * 0 on success or a negative error code on failure.
366 	 */
367 	int (*set_property)(struct drm_plane *plane,
368 			    struct drm_property *property, uint64_t val);
369 
370 	/**
371 	 * @atomic_duplicate_state:
372 	 *
373 	 * Duplicate the current atomic state for this plane and return it.
374 	 * The core and helpers guarantee that any atomic state duplicated with
375 	 * this hook and still owned by the caller (i.e. not transferred to the
376 	 * driver by calling &drm_mode_config_funcs.atomic_commit) will be
377 	 * cleaned up by calling the @atomic_destroy_state hook in this
378 	 * structure.
379 	 *
380 	 * This callback is mandatory for atomic drivers.
381 	 *
382 	 * Atomic drivers which don't subclass &struct drm_plane_state should use
383 	 * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
384 	 * state structure to extend it with driver-private state should use
385 	 * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
386 	 * duplicated in a consistent fashion across drivers.
387 	 *
388 	 * It is an error to call this hook before &drm_plane.state has been
389 	 * initialized correctly.
390 	 *
391 	 * NOTE:
392 	 *
393 	 * If the duplicate state references refcounted resources this hook must
394 	 * acquire a reference for each of them. The driver must release these
395 	 * references again in @atomic_destroy_state.
396 	 *
397 	 * RETURNS:
398 	 *
399 	 * Duplicated atomic state or NULL when the allocation failed.
400 	 */
401 	struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
402 
403 	/**
404 	 * @atomic_destroy_state:
405 	 *
406 	 * Destroy a state duplicated with @atomic_duplicate_state and release
407 	 * or unreference all resources it references
408 	 *
409 	 * This callback is mandatory for atomic drivers.
410 	 */
411 	void (*atomic_destroy_state)(struct drm_plane *plane,
412 				     struct drm_plane_state *state);
413 
414 	/**
415 	 * @atomic_set_property:
416 	 *
417 	 * Decode a driver-private property value and store the decoded value
418 	 * into the passed-in state structure. Since the atomic core decodes all
419 	 * standardized properties (even for extensions beyond the core set of
420 	 * properties which might not be implemented by all drivers) this
421 	 * requires drivers to subclass the state structure.
422 	 *
423 	 * Such driver-private properties should really only be implemented for
424 	 * truly hardware/vendor specific state. Instead it is preferred to
425 	 * standardize atomic extension and decode the properties used to expose
426 	 * such an extension in the core.
427 	 *
428 	 * Do not call this function directly, use
429 	 * drm_atomic_plane_set_property() instead.
430 	 *
431 	 * This callback is optional if the driver does not support any
432 	 * driver-private atomic properties.
433 	 *
434 	 * NOTE:
435 	 *
436 	 * This function is called in the state assembly phase of atomic
437 	 * modesets, which can be aborted for any reason (including on
438 	 * userspace's request to just check whether a configuration would be
439 	 * possible). Drivers MUST NOT touch any persistent state (hardware or
440 	 * software) or data structures except the passed in @state parameter.
441 	 *
442 	 * Also since userspace controls in which order properties are set this
443 	 * function must not do any input validation (since the state update is
444 	 * incomplete and hence likely inconsistent). Instead any such input
445 	 * validation must be done in the various atomic_check callbacks.
446 	 *
447 	 * RETURNS:
448 	 *
449 	 * 0 if the property has been found, -EINVAL if the property isn't
450 	 * implemented by the driver (which shouldn't ever happen, the core only
451 	 * asks for properties attached to this plane). No other validation is
452 	 * allowed by the driver. The core already checks that the property
453 	 * value is within the range (integer, valid enum value, ...) the driver
454 	 * set when registering the property.
455 	 */
456 	int (*atomic_set_property)(struct drm_plane *plane,
457 				   struct drm_plane_state *state,
458 				   struct drm_property *property,
459 				   uint64_t val);
460 
461 	/**
462 	 * @atomic_get_property:
463 	 *
464 	 * Reads out the decoded driver-private property. This is used to
465 	 * implement the GETPLANE IOCTL.
466 	 *
467 	 * Do not call this function directly, use
468 	 * drm_atomic_plane_get_property() instead.
469 	 *
470 	 * This callback is optional if the driver does not support any
471 	 * driver-private atomic properties.
472 	 *
473 	 * RETURNS:
474 	 *
475 	 * 0 on success, -EINVAL if the property isn't implemented by the
476 	 * driver (which should never happen, the core only asks for
477 	 * properties attached to this plane).
478 	 */
479 	int (*atomic_get_property)(struct drm_plane *plane,
480 				   const struct drm_plane_state *state,
481 				   struct drm_property *property,
482 				   uint64_t *val);
483 	/**
484 	 * @late_register:
485 	 *
486 	 * This optional hook can be used to register additional userspace
487 	 * interfaces attached to the plane like debugfs interfaces.
488 	 * It is called late in the driver load sequence from drm_dev_register().
489 	 * Everything added from this callback should be unregistered in
490 	 * the early_unregister callback.
491 	 *
492 	 * Returns:
493 	 *
494 	 * 0 on success, or a negative error code on failure.
495 	 */
496 	int (*late_register)(struct drm_plane *plane);
497 
498 	/**
499 	 * @early_unregister:
500 	 *
501 	 * This optional hook should be used to unregister the additional
502 	 * userspace interfaces attached to the plane from
503 	 * @late_register. It is called from drm_dev_unregister(),
504 	 * early in the driver unload sequence to disable userspace access
505 	 * before data structures are torndown.
506 	 */
507 	void (*early_unregister)(struct drm_plane *plane);
508 
509 	/**
510 	 * @atomic_print_state:
511 	 *
512 	 * If driver subclasses &struct drm_plane_state, it should implement
513 	 * this optional hook for printing additional driver specific state.
514 	 *
515 	 * Do not call this directly, use drm_atomic_plane_print_state()
516 	 * instead.
517 	 */
518 	void (*atomic_print_state)(struct drm_printer *p,
519 				   const struct drm_plane_state *state);
520 
521 	/**
522 	 * @format_mod_supported:
523 	 *
524 	 * This optional hook is used for the DRM to determine if the given
525 	 * format/modifier combination is valid for the plane. This allows the
526 	 * DRM to generate the correct format bitmask (which formats apply to
527 	 * which modifier), and to validate modifiers at atomic_check time.
528 	 *
529 	 * If not present, then any modifier in the plane's modifier
530 	 * list is allowed with any of the plane's formats.
531 	 *
532 	 * Returns:
533 	 *
534 	 * True if the given modifier is valid for that format on the plane.
535 	 * False otherwise.
536 	 */
537 	bool (*format_mod_supported)(struct drm_plane *plane, uint32_t format,
538 				     uint64_t modifier);
539 };
540 
541 /**
542  * enum drm_plane_type - uapi plane type enumeration
543  *
544  * For historical reasons not all planes are made the same. This enumeration is
545  * used to tell the different types of planes apart to implement the different
546  * uapi semantics for them. For userspace which is universal plane aware and
547  * which is using that atomic IOCTL there's no difference between these planes
548  * (beyong what the driver and hardware can support of course).
549  *
550  * For compatibility with legacy userspace, only overlay planes are made
551  * available to userspace by default. Userspace clients may set the
552  * &DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they
553  * wish to receive a universal plane list containing all plane types. See also
554  * drm_for_each_legacy_plane().
555  *
556  * In addition to setting each plane's type, drivers need to setup the
557  * &drm_crtc.primary and optionally &drm_crtc.cursor pointers for legacy
558  * IOCTLs. See drm_crtc_init_with_planes().
559  *
560  * WARNING: The values of this enum is UABI since they're exposed in the "type"
561  * property.
562  */
563 enum drm_plane_type {
564 	/**
565 	 * @DRM_PLANE_TYPE_OVERLAY:
566 	 *
567 	 * Overlay planes represent all non-primary, non-cursor planes. Some
568 	 * drivers refer to these types of planes as "sprites" internally.
569 	 */
570 	DRM_PLANE_TYPE_OVERLAY,
571 
572 	/**
573 	 * @DRM_PLANE_TYPE_PRIMARY:
574 	 *
575 	 * A primary plane attached to a CRTC is the most likely to be able to
576 	 * light up the CRTC when no scaling/cropping is used and the plane
577 	 * covers the whole CRTC.
578 	 */
579 	DRM_PLANE_TYPE_PRIMARY,
580 
581 	/**
582 	 * @DRM_PLANE_TYPE_CURSOR:
583 	 *
584 	 * A cursor plane attached to a CRTC is more likely to be able to be
585 	 * enabled when no scaling/cropping is used and the framebuffer has the
586 	 * size indicated by &drm_mode_config.cursor_width and
587 	 * &drm_mode_config.cursor_height. Additionally, if the driver doesn't
588 	 * support modifiers, the framebuffer should have a linear layout.
589 	 */
590 	DRM_PLANE_TYPE_CURSOR,
591 };
592 
593 
594 /**
595  * struct drm_plane - central DRM plane control structure
596  *
597  * Planes represent the scanout hardware of a display block. They receive their
598  * input data from a &drm_framebuffer and feed it to a &drm_crtc. Planes control
599  * the color conversion, see `Plane Composition Properties`_ for more details,
600  * and are also involved in the color conversion of input pixels, see `Color
601  * Management Properties`_ for details on that.
602  */
603 struct drm_plane {
604 	/** @dev: DRM device this plane belongs to */
605 	struct drm_device *dev;
606 
607 	/**
608 	 * @head:
609 	 *
610 	 * List of all planes on @dev, linked from &drm_mode_config.plane_list.
611 	 * Invariant over the lifetime of @dev and therefore does not need
612 	 * locking.
613 	 */
614 	struct list_head head;
615 
616 	/** @name: human readable name, can be overwritten by the driver */
617 	char *name;
618 
619 	/**
620 	 * @mutex:
621 	 *
622 	 * Protects modeset plane state, together with the &drm_crtc.mutex of
623 	 * CRTC this plane is linked to (when active, getting activated or
624 	 * getting disabled).
625 	 *
626 	 * For atomic drivers specifically this protects @state.
627 	 */
628 	struct drm_modeset_lock mutex;
629 
630 	/** @base: base mode object */
631 	struct drm_mode_object base;
632 
633 	/**
634 	 * @possible_crtcs: pipes this plane can be bound to constructed from
635 	 * drm_crtc_mask()
636 	 */
637 	uint32_t possible_crtcs;
638 	/** @format_types: array of formats supported by this plane */
639 	uint32_t *format_types;
640 	/** @format_count: Size of the array pointed at by @format_types. */
641 	unsigned int format_count;
642 	/**
643 	 * @format_default: driver hasn't supplied supported formats for the
644 	 * plane. Used by the non-atomic driver compatibility wrapper only.
645 	 */
646 	bool format_default;
647 
648 	/** @modifiers: array of modifiers supported by this plane */
649 	uint64_t *modifiers;
650 	/** @modifier_count: Size of the array pointed at by @modifier_count. */
651 	unsigned int modifier_count;
652 
653 	/**
654 	 * @crtc:
655 	 *
656 	 * Currently bound CRTC, only meaningful for non-atomic drivers. For
657 	 * atomic drivers this is forced to be NULL, atomic drivers should
658 	 * instead check &drm_plane_state.crtc.
659 	 */
660 	struct drm_crtc *crtc;
661 
662 	/**
663 	 * @fb:
664 	 *
665 	 * Currently bound framebuffer, only meaningful for non-atomic drivers.
666 	 * For atomic drivers this is forced to be NULL, atomic drivers should
667 	 * instead check &drm_plane_state.fb.
668 	 */
669 	struct drm_framebuffer *fb;
670 
671 	/**
672 	 * @old_fb:
673 	 *
674 	 * Temporary tracking of the old fb while a modeset is ongoing. Only
675 	 * used by non-atomic drivers, forced to be NULL for atomic drivers.
676 	 */
677 	struct drm_framebuffer *old_fb;
678 
679 	/** @funcs: plane control functions */
680 	const struct drm_plane_funcs *funcs;
681 
682 	/** @properties: property tracking for this plane */
683 	struct drm_object_properties properties;
684 
685 	/** @type: Type of plane, see &enum drm_plane_type for details. */
686 	enum drm_plane_type type;
687 
688 	/**
689 	 * @index: Position inside the mode_config.list, can be used as an array
690 	 * index. It is invariant over the lifetime of the plane.
691 	 */
692 	unsigned index;
693 
694 	/** @helper_private: mid-layer private data */
695 	const struct drm_plane_helper_funcs *helper_private;
696 
697 	/**
698 	 * @state:
699 	 *
700 	 * Current atomic state for this plane.
701 	 *
702 	 * This is protected by @mutex. Note that nonblocking atomic commits
703 	 * access the current plane state without taking locks. Either by going
704 	 * through the &struct drm_atomic_state pointers, see
705 	 * for_each_oldnew_plane_in_state(), for_each_old_plane_in_state() and
706 	 * for_each_new_plane_in_state(). Or through careful ordering of atomic
707 	 * commit operations as implemented in the atomic helpers, see
708 	 * &struct drm_crtc_commit.
709 	 */
710 	struct drm_plane_state *state;
711 
712 	/**
713 	 * @alpha_property:
714 	 * Optional alpha property for this plane. See
715 	 * drm_plane_create_alpha_property().
716 	 */
717 	struct drm_property *alpha_property;
718 	/**
719 	 * @zpos_property:
720 	 * Optional zpos property for this plane. See
721 	 * drm_plane_create_zpos_property().
722 	 */
723 	struct drm_property *zpos_property;
724 	/**
725 	 * @rotation_property:
726 	 * Optional rotation property for this plane. See
727 	 * drm_plane_create_rotation_property().
728 	 */
729 	struct drm_property *rotation_property;
730 	/**
731 	 * @blend_mode_property:
732 	 * Optional "pixel blend mode" enum property for this plane.
733 	 * Blend mode property represents the alpha blending equation selection,
734 	 * describing how the pixels from the current plane are composited with
735 	 * the background.
736 	 */
737 	struct drm_property *blend_mode_property;
738 
739 	/**
740 	 * @color_encoding_property:
741 	 *
742 	 * Optional "COLOR_ENCODING" enum property for specifying
743 	 * color encoding for non RGB formats.
744 	 * See drm_plane_create_color_properties().
745 	 */
746 	struct drm_property *color_encoding_property;
747 	/**
748 	 * @color_range_property:
749 	 *
750 	 * Optional "COLOR_RANGE" enum property for specifying
751 	 * color range for non RGB formats.
752 	 * See drm_plane_create_color_properties().
753 	 */
754 	struct drm_property *color_range_property;
755 
756 	/**
757 	 * @scaling_filter_property: property to apply a particular filter while
758 	 * scaling.
759 	 */
760 	struct drm_property *scaling_filter_property;
761 };
762 
763 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
764 
765 __printf(9, 10)
766 int drm_universal_plane_init(struct drm_device *dev,
767 			     struct drm_plane *plane,
768 			     uint32_t possible_crtcs,
769 			     const struct drm_plane_funcs *funcs,
770 			     const uint32_t *formats,
771 			     unsigned int format_count,
772 			     const uint64_t *format_modifiers,
773 			     enum drm_plane_type type,
774 			     const char *name, ...);
775 void drm_plane_cleanup(struct drm_plane *plane);
776 
777 __printf(10, 11)
778 void *__drmm_universal_plane_alloc(struct drm_device *dev,
779 				   size_t size, size_t offset,
780 				   uint32_t possible_crtcs,
781 				   const struct drm_plane_funcs *funcs,
782 				   const uint32_t *formats,
783 				   unsigned int format_count,
784 				   const uint64_t *format_modifiers,
785 				   enum drm_plane_type plane_type,
786 				   const char *name, ...);
787 
788 /**
789  * drmm_universal_plane_alloc - Allocate and initialize an universal plane object
790  * @dev: DRM device
791  * @type: the type of the struct which contains struct &drm_plane
792  * @member: the name of the &drm_plane within @type
793  * @possible_crtcs: bitmask of possible CRTCs
794  * @funcs: callbacks for the new plane
795  * @formats: array of supported formats (DRM_FORMAT\_\*)
796  * @format_count: number of elements in @formats
797  * @format_modifiers: array of struct drm_format modifiers terminated by
798  *                    DRM_FORMAT_MOD_INVALID
799  * @plane_type: type of plane (overlay, primary, cursor)
800  * @name: printf style format string for the plane name, or NULL for default name
801  *
802  * Allocates and initializes a plane object of type @type. Cleanup is
803  * automatically handled through registering drm_plane_cleanup() with
804  * drmm_add_action().
805  *
806  * The @drm_plane_funcs.destroy hook must be NULL.
807  *
808  * Drivers that only support the DRM_FORMAT_MOD_LINEAR modifier support may set
809  * @format_modifiers to NULL. The plane will advertise the linear modifier.
810  *
811  * Returns:
812  * Pointer to new plane, or ERR_PTR on failure.
813  */
814 #define drmm_universal_plane_alloc(dev, type, member, possible_crtcs, funcs, formats, \
815 				   format_count, format_modifiers, plane_type, name, ...) \
816 	((type *)__drmm_universal_plane_alloc(dev, sizeof(type), \
817 					      offsetof(type, member), \
818 					      possible_crtcs, funcs, formats, \
819 					      format_count, format_modifiers, \
820 					      plane_type, name, ##__VA_ARGS__))
821 
822 __printf(10, 11)
823 void *__drm_universal_plane_alloc(struct drm_device *dev,
824 				  size_t size, size_t offset,
825 				  uint32_t possible_crtcs,
826 				  const struct drm_plane_funcs *funcs,
827 				  const uint32_t *formats,
828 				  unsigned int format_count,
829 				  const uint64_t *format_modifiers,
830 				  enum drm_plane_type plane_type,
831 				  const char *name, ...);
832 
833 /**
834  * drm_universal_plane_alloc() - Allocate and initialize an universal plane object
835  * @dev: DRM device
836  * @type: the type of the struct which contains struct &drm_plane
837  * @member: the name of the &drm_plane within @type
838  * @possible_crtcs: bitmask of possible CRTCs
839  * @funcs: callbacks for the new plane
840  * @formats: array of supported formats (DRM_FORMAT\_\*)
841  * @format_count: number of elements in @formats
842  * @format_modifiers: array of struct drm_format modifiers terminated by
843  *                    DRM_FORMAT_MOD_INVALID
844  * @plane_type: type of plane (overlay, primary, cursor)
845  * @name: printf style format string for the plane name, or NULL for default name
846  *
847  * Allocates and initializes a plane object of type @type. The caller
848  * is responsible for releasing the allocated memory with kfree().
849  *
850  * Drivers are encouraged to use drmm_universal_plane_alloc() instead.
851  *
852  * Drivers that only support the DRM_FORMAT_MOD_LINEAR modifier support may set
853  * @format_modifiers to NULL. The plane will advertise the linear modifier.
854  *
855  * Returns:
856  * Pointer to new plane, or ERR_PTR on failure.
857  */
858 #define drm_universal_plane_alloc(dev, type, member, possible_crtcs, funcs, formats, \
859 				   format_count, format_modifiers, plane_type, name, ...) \
860 	((type *)__drm_universal_plane_alloc(dev, sizeof(type), \
861 					     offsetof(type, member), \
862 					     possible_crtcs, funcs, formats, \
863 					     format_count, format_modifiers, \
864 					     plane_type, name, ##__VA_ARGS__))
865 
866 /**
867  * drm_plane_index - find the index of a registered plane
868  * @plane: plane to find index for
869  *
870  * Given a registered plane, return the index of that plane within a DRM
871  * device's list of planes.
872  */
drm_plane_index(const struct drm_plane * plane)873 static inline unsigned int drm_plane_index(const struct drm_plane *plane)
874 {
875 	return plane->index;
876 }
877 
878 /**
879  * drm_plane_mask - find the mask of a registered plane
880  * @plane: plane to find mask for
881  */
drm_plane_mask(const struct drm_plane * plane)882 static inline u32 drm_plane_mask(const struct drm_plane *plane)
883 {
884 	return 1 << drm_plane_index(plane);
885 }
886 
887 struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
888 void drm_plane_force_disable(struct drm_plane *plane);
889 
890 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
891 				       struct drm_property *property,
892 				       uint64_t value);
893 
894 /**
895  * drm_plane_find - find a &drm_plane
896  * @dev: DRM device
897  * @file_priv: drm file to check for lease against.
898  * @id: plane id
899  *
900  * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
901  * drm_mode_object_find().
902  */
drm_plane_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id)903 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
904 		struct drm_file *file_priv,
905 		uint32_t id)
906 {
907 	struct drm_mode_object *mo;
908 	mo = drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_PLANE);
909 	return mo ? obj_to_plane(mo) : NULL;
910 }
911 
912 /**
913  * drm_for_each_plane_mask - iterate over planes specified by bitmask
914  * @plane: the loop cursor
915  * @dev: the DRM device
916  * @plane_mask: bitmask of plane indices
917  *
918  * Iterate over all planes specified by bitmask.
919  */
920 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
921 	list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
922 		for_each_if ((plane_mask) & drm_plane_mask(plane))
923 
924 /**
925  * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
926  * @plane: the loop cursor
927  * @dev: the DRM device
928  *
929  * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
930  * This is useful for implementing userspace apis when userspace is not
931  * universal plane aware. See also &enum drm_plane_type.
932  */
933 #define drm_for_each_legacy_plane(plane, dev) \
934 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
935 		for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
936 
937 /**
938  * drm_for_each_plane - iterate over all planes
939  * @plane: the loop cursor
940  * @dev: the DRM device
941  *
942  * Iterate over all planes of @dev, include primary and cursor planes.
943  */
944 #define drm_for_each_plane(plane, dev) \
945 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
946 
947 bool drm_any_plane_has_format(struct drm_device *dev,
948 			      u32 format, u64 modifier);
949 
950 void drm_plane_enable_fb_damage_clips(struct drm_plane *plane);
951 unsigned int
952 drm_plane_get_damage_clips_count(const struct drm_plane_state *state);
953 struct drm_mode_rect *
954 drm_plane_get_damage_clips(const struct drm_plane_state *state);
955 
956 int drm_plane_create_scaling_filter_property(struct drm_plane *plane,
957 					     unsigned int supported_filters);
958 
959 #endif
960