dma-buf: Rename struct fence to dma_fence
[linux-2.6-block.git] / include / drm / drm_plane.h
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  * @rotation_property: rotation property for this plane
389  * @helper_private: mid-layer private data
390  */
391 struct drm_plane {
392         struct drm_device *dev;
393         struct list_head head;
394
395         char *name;
396
397         /**
398          * @mutex:
399          *
400          * Protects modeset plane state, together with the mutex of &drm_crtc
401          * this plane is linked to (when active, getting actived or getting
402          * disabled).
403          */
404         struct drm_modeset_lock mutex;
405
406         struct drm_mode_object base;
407
408         uint32_t possible_crtcs;
409         uint32_t *format_types;
410         unsigned int format_count;
411         bool format_default;
412
413         struct drm_crtc *crtc;
414         struct drm_framebuffer *fb;
415
416         struct drm_framebuffer *old_fb;
417
418         const struct drm_plane_funcs *funcs;
419
420         struct drm_object_properties properties;
421
422         enum drm_plane_type type;
423
424         /**
425          * @index: Position inside the mode_config.list, can be used as an array
426          * index. It is invariant over the lifetime of the plane.
427          */
428         unsigned index;
429
430         const struct drm_plane_helper_funcs *helper_private;
431
432         struct drm_plane_state *state;
433
434         struct drm_property *zpos_property;
435         struct drm_property *rotation_property;
436 };
437
438 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
439
440 extern __printf(8, 9)
441 int drm_universal_plane_init(struct drm_device *dev,
442                              struct drm_plane *plane,
443                              unsigned long possible_crtcs,
444                              const struct drm_plane_funcs *funcs,
445                              const uint32_t *formats,
446                              unsigned int format_count,
447                              enum drm_plane_type type,
448                              const char *name, ...);
449 extern int drm_plane_init(struct drm_device *dev,
450                           struct drm_plane *plane,
451                           unsigned long possible_crtcs,
452                           const struct drm_plane_funcs *funcs,
453                           const uint32_t *formats, unsigned int format_count,
454                           bool is_primary);
455 extern void drm_plane_cleanup(struct drm_plane *plane);
456
457 /**
458  * drm_plane_index - find the index of a registered plane
459  * @plane: plane to find index for
460  *
461  * Given a registered plane, return the index of that plane within a DRM
462  * device's list of planes.
463  */
464 static inline unsigned int drm_plane_index(struct drm_plane *plane)
465 {
466         return plane->index;
467 }
468 extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
469 extern void drm_plane_force_disable(struct drm_plane *plane);
470
471 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
472                                        struct drm_property *property,
473                                        uint64_t value);
474
475 /**
476  * drm_plane_find - find a &drm_plane
477  * @dev: DRM device
478  * @id: plane id
479  *
480  * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
481  * drm_mode_object_find().
482  */
483 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
484                 uint32_t id)
485 {
486         struct drm_mode_object *mo;
487         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
488         return mo ? obj_to_plane(mo) : NULL;
489 }
490
491 /**
492  * drm_for_each_plane_mask - iterate over planes specified by bitmask
493  * @plane: the loop cursor
494  * @dev: the DRM device
495  * @plane_mask: bitmask of plane indices
496  *
497  * Iterate over all planes specified by bitmask.
498  */
499 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
500         list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
501                 for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
502
503 /**
504  * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
505  * @plane: the loop cursor
506  * @dev: the DRM device
507  *
508  * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
509  * This is useful for implementing userspace apis when userspace is not
510  * universal plane aware. See also enum &drm_plane_type.
511  */
512 #define drm_for_each_legacy_plane(plane, dev) \
513         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
514                 for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
515
516 /**
517  * drm_for_each_plane - iterate over all planes
518  * @plane: the loop cursor
519  * @dev: the DRM device
520  *
521  * Iterate over all planes of @dev, include primary and cursor planes.
522  */
523 #define drm_for_each_plane(plane, dev) \
524         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
525
526
527 #endif