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