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