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