xref: /openbmc/linux/include/drm/drm_plane.h (revision e6fc3b68)
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 	 * Drivers implementing atomic modeset should use
237 	 * drm_atomic_helper_plane_set_property() to implement this hook.
238 	 *
239 	 * This callback is optional if the driver does not support any legacy
240 	 * driver-private properties.
241 	 *
242 	 * RETURNS:
243 	 *
244 	 * 0 on success or a negative error code on failure.
245 	 */
246 	int (*set_property)(struct drm_plane *plane,
247 			    struct drm_property *property, uint64_t val);
248 
249 	/**
250 	 * @atomic_duplicate_state:
251 	 *
252 	 * Duplicate the current atomic state for this plane and return it.
253 	 * The core and helpers guarantee that any atomic state duplicated with
254 	 * this hook and still owned by the caller (i.e. not transferred to the
255 	 * driver by calling &drm_mode_config_funcs.atomic_commit) will be
256 	 * cleaned up by calling the @atomic_destroy_state hook in this
257 	 * structure.
258 	 *
259 	 * Atomic drivers which don't subclass &struct drm_plane_state should use
260 	 * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
261 	 * state structure to extend it with driver-private state should use
262 	 * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
263 	 * duplicated in a consistent fashion across drivers.
264 	 *
265 	 * It is an error to call this hook before &drm_plane.state has been
266 	 * initialized correctly.
267 	 *
268 	 * NOTE:
269 	 *
270 	 * If the duplicate state references refcounted resources this hook must
271 	 * acquire a reference for each of them. The driver must release these
272 	 * references again in @atomic_destroy_state.
273 	 *
274 	 * RETURNS:
275 	 *
276 	 * Duplicated atomic state or NULL when the allocation failed.
277 	 */
278 	struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
279 
280 	/**
281 	 * @atomic_destroy_state:
282 	 *
283 	 * Destroy a state duplicated with @atomic_duplicate_state and release
284 	 * or unreference all resources it references
285 	 */
286 	void (*atomic_destroy_state)(struct drm_plane *plane,
287 				     struct drm_plane_state *state);
288 
289 	/**
290 	 * @atomic_set_property:
291 	 *
292 	 * Decode a driver-private property value and store the decoded value
293 	 * into the passed-in state structure. Since the atomic core decodes all
294 	 * standardized properties (even for extensions beyond the core set of
295 	 * properties which might not be implemented by all drivers) this
296 	 * requires drivers to subclass the state structure.
297 	 *
298 	 * Such driver-private properties should really only be implemented for
299 	 * truly hardware/vendor specific state. Instead it is preferred to
300 	 * standardize atomic extension and decode the properties used to expose
301 	 * such an extension in the core.
302 	 *
303 	 * Do not call this function directly, use
304 	 * drm_atomic_plane_set_property() instead.
305 	 *
306 	 * This callback is optional if the driver does not support any
307 	 * driver-private atomic properties.
308 	 *
309 	 * NOTE:
310 	 *
311 	 * This function is called in the state assembly phase of atomic
312 	 * modesets, which can be aborted for any reason (including on
313 	 * userspace's request to just check whether a configuration would be
314 	 * possible). Drivers MUST NOT touch any persistent state (hardware or
315 	 * software) or data structures except the passed in @state parameter.
316 	 *
317 	 * Also since userspace controls in which order properties are set this
318 	 * function must not do any input validation (since the state update is
319 	 * incomplete and hence likely inconsistent). Instead any such input
320 	 * validation must be done in the various atomic_check callbacks.
321 	 *
322 	 * RETURNS:
323 	 *
324 	 * 0 if the property has been found, -EINVAL if the property isn't
325 	 * implemented by the driver (which shouldn't ever happen, the core only
326 	 * asks for properties attached to this plane). No other validation is
327 	 * allowed by the driver. The core already checks that the property
328 	 * value is within the range (integer, valid enum value, ...) the driver
329 	 * set when registering the property.
330 	 */
331 	int (*atomic_set_property)(struct drm_plane *plane,
332 				   struct drm_plane_state *state,
333 				   struct drm_property *property,
334 				   uint64_t val);
335 
336 	/**
337 	 * @atomic_get_property:
338 	 *
339 	 * Reads out the decoded driver-private property. This is used to
340 	 * implement the GETPLANE IOCTL.
341 	 *
342 	 * Do not call this function directly, use
343 	 * drm_atomic_plane_get_property() instead.
344 	 *
345 	 * This callback is optional if the driver does not support any
346 	 * driver-private atomic properties.
347 	 *
348 	 * RETURNS:
349 	 *
350 	 * 0 on success, -EINVAL if the property isn't implemented by the
351 	 * driver (which should never happen, the core only asks for
352 	 * properties attached to this plane).
353 	 */
354 	int (*atomic_get_property)(struct drm_plane *plane,
355 				   const struct drm_plane_state *state,
356 				   struct drm_property *property,
357 				   uint64_t *val);
358 	/**
359 	 * @late_register:
360 	 *
361 	 * This optional hook can be used to register additional userspace
362 	 * interfaces attached to the plane like debugfs interfaces.
363 	 * It is called late in the driver load sequence from drm_dev_register().
364 	 * Everything added from this callback should be unregistered in
365 	 * the early_unregister callback.
366 	 *
367 	 * Returns:
368 	 *
369 	 * 0 on success, or a negative error code on failure.
370 	 */
371 	int (*late_register)(struct drm_plane *plane);
372 
373 	/**
374 	 * @early_unregister:
375 	 *
376 	 * This optional hook should be used to unregister the additional
377 	 * userspace interfaces attached to the plane from
378 	 * @late_register. It is called from drm_dev_unregister(),
379 	 * early in the driver unload sequence to disable userspace access
380 	 * before data structures are torndown.
381 	 */
382 	void (*early_unregister)(struct drm_plane *plane);
383 
384 	/**
385 	 * @atomic_print_state:
386 	 *
387 	 * If driver subclasses &struct drm_plane_state, it should implement
388 	 * this optional hook for printing additional driver specific state.
389 	 *
390 	 * Do not call this directly, use drm_atomic_plane_print_state()
391 	 * instead.
392 	 */
393 	void (*atomic_print_state)(struct drm_printer *p,
394 				   const struct drm_plane_state *state);
395 
396 	/**
397 	 * @format_mod_supported:
398 	 *
399 	 * This optional hook is used for the DRM to determine if the given
400 	 * format/modifier combination is valid for the plane. This allows the
401 	 * DRM to generate the correct format bitmask (which formats apply to
402 	 * which modifier).
403 	 *
404 	 * Returns:
405 	 *
406 	 * True if the given modifier is valid for that format on the plane.
407 	 * False otherwise.
408 	 */
409 	bool (*format_mod_supported)(struct drm_plane *plane, uint32_t format,
410 				     uint64_t modifier);
411 };
412 
413 /**
414  * enum drm_plane_type - uapi plane type enumeration
415  *
416  * For historical reasons not all planes are made the same. This enumeration is
417  * used to tell the different types of planes apart to implement the different
418  * uapi semantics for them. For userspace which is universal plane aware and
419  * which is using that atomic IOCTL there's no difference between these planes
420  * (beyong what the driver and hardware can support of course).
421  *
422  * For compatibility with legacy userspace, only overlay planes are made
423  * available to userspace by default. Userspace clients may set the
424  * DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they
425  * wish to receive a universal plane list containing all plane types. See also
426  * drm_for_each_legacy_plane().
427  *
428  * WARNING: The values of this enum is UABI since they're exposed in the "type"
429  * property.
430  */
431 enum drm_plane_type {
432 	/**
433 	 * @DRM_PLANE_TYPE_OVERLAY:
434 	 *
435 	 * Overlay planes represent all non-primary, non-cursor planes. Some
436 	 * drivers refer to these types of planes as "sprites" internally.
437 	 */
438 	DRM_PLANE_TYPE_OVERLAY,
439 
440 	/**
441 	 * @DRM_PLANE_TYPE_PRIMARY:
442 	 *
443 	 * Primary planes represent a "main" plane for a CRTC.  Primary planes
444 	 * are the planes operated upon by CRTC modesetting and flipping
445 	 * operations described in the &drm_crtc_funcs.page_flip and
446 	 * &drm_crtc_funcs.set_config hooks.
447 	 */
448 	DRM_PLANE_TYPE_PRIMARY,
449 
450 	/**
451 	 * @DRM_PLANE_TYPE_CURSOR:
452 	 *
453 	 * Cursor planes represent a "cursor" plane for a CRTC.  Cursor planes
454 	 * are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and
455 	 * DRM_IOCTL_MODE_CURSOR2 IOCTLs.
456 	 */
457 	DRM_PLANE_TYPE_CURSOR,
458 };
459 
460 
461 /**
462  * struct drm_plane - central DRM plane control structure
463  * @dev: DRM device this plane belongs to
464  * @head: for list management
465  * @name: human readable name, can be overwritten by the driver
466  * @base: base mode object
467  * @possible_crtcs: pipes this plane can be bound to
468  * @format_types: array of formats supported by this plane
469  * @format_count: number of formats supported
470  * @format_default: driver hasn't supplied supported formats for the plane
471  * @crtc: currently bound CRTC
472  * @fb: currently bound fb
473  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
474  * 	drm_mode_set_config_internal() to implement correct refcounting.
475  * @funcs: helper functions
476  * @properties: property tracking for this plane
477  * @type: type of plane (overlay, primary, cursor)
478  * @zpos_property: zpos property for this plane
479  * @rotation_property: rotation property for this plane
480  * @helper_private: mid-layer private data
481  */
482 struct drm_plane {
483 	struct drm_device *dev;
484 	struct list_head head;
485 
486 	char *name;
487 
488 	/**
489 	 * @mutex:
490 	 *
491 	 * Protects modeset plane state, together with the &drm_crtc.mutex of
492 	 * CRTC this plane is linked to (when active, getting activated or
493 	 * getting disabled).
494 	 *
495 	 * For atomic drivers specifically this protects @state.
496 	 */
497 	struct drm_modeset_lock mutex;
498 
499 	struct drm_mode_object base;
500 
501 	uint32_t possible_crtcs;
502 	uint32_t *format_types;
503 	unsigned int format_count;
504 	bool format_default;
505 
506 	uint64_t *modifiers;
507 	unsigned int modifier_count;
508 
509 	struct drm_crtc *crtc;
510 	struct drm_framebuffer *fb;
511 
512 	struct drm_framebuffer *old_fb;
513 
514 	const struct drm_plane_funcs *funcs;
515 
516 	struct drm_object_properties properties;
517 
518 	enum drm_plane_type type;
519 
520 	/**
521 	 * @index: Position inside the mode_config.list, can be used as an array
522 	 * index. It is invariant over the lifetime of the plane.
523 	 */
524 	unsigned index;
525 
526 	const struct drm_plane_helper_funcs *helper_private;
527 
528 	/**
529 	 * @state:
530 	 *
531 	 * Current atomic state for this plane.
532 	 *
533 	 * This is protected by @mutex. Note that nonblocking atomic commits
534 	 * access the current plane state without taking locks. Either by going
535 	 * through the &struct drm_atomic_state pointers, see
536 	 * for_each_plane_in_state(), for_each_oldnew_plane_in_state(),
537 	 * for_each_old_plane_in_state() and for_each_new_plane_in_state(). Or
538 	 * through careful ordering of atomic commit operations as implemented
539 	 * in the atomic helpers, see &struct drm_crtc_commit.
540 	 */
541 	struct drm_plane_state *state;
542 
543 	struct drm_property *zpos_property;
544 	struct drm_property *rotation_property;
545 };
546 
547 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
548 
549 __printf(9, 10)
550 int drm_universal_plane_init(struct drm_device *dev,
551 			     struct drm_plane *plane,
552 			     uint32_t possible_crtcs,
553 			     const struct drm_plane_funcs *funcs,
554 			     const uint32_t *formats,
555 			     unsigned int format_count,
556 			     const uint64_t *format_modifiers,
557 			     enum drm_plane_type type,
558 			     const char *name, ...);
559 int drm_plane_init(struct drm_device *dev,
560 		   struct drm_plane *plane,
561 		   uint32_t possible_crtcs,
562 		   const struct drm_plane_funcs *funcs,
563 		   const uint32_t *formats, unsigned int format_count,
564 		   bool is_primary);
565 void drm_plane_cleanup(struct drm_plane *plane);
566 
567 /**
568  * drm_plane_index - find the index of a registered plane
569  * @plane: plane to find index for
570  *
571  * Given a registered plane, return the index of that plane within a DRM
572  * device's list of planes.
573  */
574 static inline unsigned int drm_plane_index(struct drm_plane *plane)
575 {
576 	return plane->index;
577 }
578 struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
579 void drm_plane_force_disable(struct drm_plane *plane);
580 
581 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
582 				       struct drm_property *property,
583 				       uint64_t value);
584 
585 /**
586  * drm_plane_find - find a &drm_plane
587  * @dev: DRM device
588  * @id: plane id
589  *
590  * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
591  * drm_mode_object_find().
592  */
593 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
594 		uint32_t id)
595 {
596 	struct drm_mode_object *mo;
597 	mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
598 	return mo ? obj_to_plane(mo) : NULL;
599 }
600 
601 /**
602  * drm_for_each_plane_mask - iterate over planes specified by bitmask
603  * @plane: the loop cursor
604  * @dev: the DRM device
605  * @plane_mask: bitmask of plane indices
606  *
607  * Iterate over all planes specified by bitmask.
608  */
609 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
610 	list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
611 		for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
612 
613 /**
614  * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
615  * @plane: the loop cursor
616  * @dev: the DRM device
617  *
618  * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
619  * This is useful for implementing userspace apis when userspace is not
620  * universal plane aware. See also &enum drm_plane_type.
621  */
622 #define drm_for_each_legacy_plane(plane, dev) \
623 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
624 		for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
625 
626 /**
627  * drm_for_each_plane - iterate over all planes
628  * @plane: the loop cursor
629  * @dev: the DRM device
630  *
631  * Iterate over all planes of @dev, include primary and cursor planes.
632  */
633 #define drm_for_each_plane(plane, dev) \
634 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
635 
636 
637 #endif
638