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