xref: /openbmc/linux/include/drm/drm_plane.h (revision 020c5260)
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 /**
398  * enum drm_plane_type - uapi plane type enumeration
399  *
400  * For historical reasons not all planes are made the same. This enumeration is
401  * used to tell the different types of planes apart to implement the different
402  * uapi semantics for them. For userspace which is universal plane aware and
403  * which is using that atomic IOCTL there's no difference between these planes
404  * (beyong what the driver and hardware can support of course).
405  *
406  * For compatibility with legacy userspace, only overlay planes are made
407  * available to userspace by default. Userspace clients may set the
408  * DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they
409  * wish to receive a universal plane list containing all plane types. See also
410  * drm_for_each_legacy_plane().
411  *
412  * WARNING: The values of this enum is UABI since they're exposed in the "type"
413  * property.
414  */
415 enum drm_plane_type {
416 	/**
417 	 * @DRM_PLANE_TYPE_OVERLAY:
418 	 *
419 	 * Overlay planes represent all non-primary, non-cursor planes. Some
420 	 * drivers refer to these types of planes as "sprites" internally.
421 	 */
422 	DRM_PLANE_TYPE_OVERLAY,
423 
424 	/**
425 	 * @DRM_PLANE_TYPE_PRIMARY:
426 	 *
427 	 * Primary planes represent a "main" plane for a CRTC.  Primary planes
428 	 * are the planes operated upon by CRTC modesetting and flipping
429 	 * operations described in the &drm_crtc_funcs.page_flip and
430 	 * &drm_crtc_funcs.set_config hooks.
431 	 */
432 	DRM_PLANE_TYPE_PRIMARY,
433 
434 	/**
435 	 * @DRM_PLANE_TYPE_CURSOR:
436 	 *
437 	 * Cursor planes represent a "cursor" plane for a CRTC.  Cursor planes
438 	 * are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and
439 	 * DRM_IOCTL_MODE_CURSOR2 IOCTLs.
440 	 */
441 	DRM_PLANE_TYPE_CURSOR,
442 };
443 
444 
445 /**
446  * struct drm_plane - central DRM plane control structure
447  * @dev: DRM device this plane belongs to
448  * @head: for list management
449  * @name: human readable name, can be overwritten by the driver
450  * @base: base mode object
451  * @possible_crtcs: pipes this plane can be bound to
452  * @format_types: array of formats supported by this plane
453  * @format_count: number of formats supported
454  * @format_default: driver hasn't supplied supported formats for the plane
455  * @crtc: currently bound CRTC
456  * @fb: currently bound fb
457  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
458  * 	drm_mode_set_config_internal() to implement correct refcounting.
459  * @funcs: helper functions
460  * @properties: property tracking for this plane
461  * @type: type of plane (overlay, primary, cursor)
462  * @zpos_property: zpos property for this plane
463  * @rotation_property: rotation property for this plane
464  * @helper_private: mid-layer private data
465  */
466 struct drm_plane {
467 	struct drm_device *dev;
468 	struct list_head head;
469 
470 	char *name;
471 
472 	/**
473 	 * @mutex:
474 	 *
475 	 * Protects modeset plane state, together with the &drm_crtc.mutex of
476 	 * CRTC this plane is linked to (when active, getting activated or
477 	 * getting disabled).
478 	 *
479 	 * For atomic drivers specifically this protects @state.
480 	 */
481 	struct drm_modeset_lock mutex;
482 
483 	struct drm_mode_object base;
484 
485 	uint32_t possible_crtcs;
486 	uint32_t *format_types;
487 	unsigned int format_count;
488 	bool format_default;
489 
490 	struct drm_crtc *crtc;
491 	struct drm_framebuffer *fb;
492 
493 	struct drm_framebuffer *old_fb;
494 
495 	const struct drm_plane_funcs *funcs;
496 
497 	struct drm_object_properties properties;
498 
499 	enum drm_plane_type type;
500 
501 	/**
502 	 * @index: Position inside the mode_config.list, can be used as an array
503 	 * index. It is invariant over the lifetime of the plane.
504 	 */
505 	unsigned index;
506 
507 	const struct drm_plane_helper_funcs *helper_private;
508 
509 	/**
510 	 * @state:
511 	 *
512 	 * Current atomic state for this plane.
513 	 *
514 	 * This is protected by @mutex. Note that nonblocking atomic commits
515 	 * access the current plane state without taking locks. Either by going
516 	 * through the &struct drm_atomic_state pointers, see
517 	 * for_each_plane_in_state(), for_each_oldnew_plane_in_state(),
518 	 * for_each_old_plane_in_state() and for_each_new_plane_in_state(). Or
519 	 * through careful ordering of atomic commit operations as implemented
520 	 * in the atomic helpers, see &struct drm_crtc_commit.
521 	 */
522 	struct drm_plane_state *state;
523 
524 	struct drm_property *zpos_property;
525 	struct drm_property *rotation_property;
526 };
527 
528 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
529 
530 __printf(8, 9)
531 int drm_universal_plane_init(struct drm_device *dev,
532 			     struct drm_plane *plane,
533 			     uint32_t possible_crtcs,
534 			     const struct drm_plane_funcs *funcs,
535 			     const uint32_t *formats,
536 			     unsigned int format_count,
537 			     enum drm_plane_type type,
538 			     const char *name, ...);
539 int drm_plane_init(struct drm_device *dev,
540 		   struct drm_plane *plane,
541 		   uint32_t possible_crtcs,
542 		   const struct drm_plane_funcs *funcs,
543 		   const uint32_t *formats, unsigned int format_count,
544 		   bool is_primary);
545 void drm_plane_cleanup(struct drm_plane *plane);
546 
547 /**
548  * drm_plane_index - find the index of a registered plane
549  * @plane: plane to find index for
550  *
551  * Given a registered plane, return the index of that plane within a DRM
552  * device's list of planes.
553  */
554 static inline unsigned int drm_plane_index(struct drm_plane *plane)
555 {
556 	return plane->index;
557 }
558 struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
559 void drm_plane_force_disable(struct drm_plane *plane);
560 
561 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
562 				       struct drm_property *property,
563 				       uint64_t value);
564 
565 /**
566  * drm_plane_find - find a &drm_plane
567  * @dev: DRM device
568  * @id: plane id
569  *
570  * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
571  * drm_mode_object_find().
572  */
573 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
574 		uint32_t id)
575 {
576 	struct drm_mode_object *mo;
577 	mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
578 	return mo ? obj_to_plane(mo) : NULL;
579 }
580 
581 /**
582  * drm_for_each_plane_mask - iterate over planes specified by bitmask
583  * @plane: the loop cursor
584  * @dev: the DRM device
585  * @plane_mask: bitmask of plane indices
586  *
587  * Iterate over all planes specified by bitmask.
588  */
589 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
590 	list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
591 		for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
592 
593 /**
594  * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
595  * @plane: the loop cursor
596  * @dev: the DRM device
597  *
598  * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
599  * This is useful for implementing userspace apis when userspace is not
600  * universal plane aware. See also &enum drm_plane_type.
601  */
602 #define drm_for_each_legacy_plane(plane, dev) \
603 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
604 		for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
605 
606 /**
607  * drm_for_each_plane - iterate over all planes
608  * @plane: the loop cursor
609  * @dev: the DRM device
610  *
611  * Iterate over all planes of @dev, include primary and cursor planes.
612  */
613 #define drm_for_each_plane(plane, dev) \
614 	list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
615 
616 
617 #endif
618