drm/fence: add out-fences support
[linux-drm-fsl-dcu.git] / include / drm / drm_mode_config.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_MODE_CONFIG_H__
24 #define __DRM_MODE_CONFIG_H__
25
26 #include <linux/mutex.h>
27 #include <linux/types.h>
28 #include <linux/idr.h>
29 #include <linux/workqueue.h>
30
31 #include <drm/drm_modeset_lock.h>
32
33 struct drm_file;
34 struct drm_device;
35 struct drm_atomic_state;
36 struct drm_mode_fb_cmd2;
37
38 /**
39  * struct drm_mode_config_funcs - basic driver provided mode setting functions
40  *
41  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
42  * involve drivers.
43  */
44 struct drm_mode_config_funcs {
45         /**
46          * @fb_create:
47          *
48          * Create a new framebuffer object. The core does basic checks on the
49          * requested metadata, but most of that is left to the driver. See
50          * struct &drm_mode_fb_cmd2 for details.
51          *
52          * If the parameters are deemed valid and the backing storage objects in
53          * the underlying memory manager all exist, then the driver allocates
54          * a new &drm_framebuffer structure, subclassed to contain
55          * driver-specific information (like the internal native buffer object
56          * references). It also needs to fill out all relevant metadata, which
57          * should be done by calling drm_helper_mode_fill_fb_struct().
58          *
59          * The initialization is finalized by calling drm_framebuffer_init(),
60          * which registers the framebuffer and makes it accessible to other
61          * threads.
62          *
63          * RETURNS:
64          *
65          * A new framebuffer with an initial reference count of 1 or a negative
66          * error code encoded with ERR_PTR().
67          */
68         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
69                                              struct drm_file *file_priv,
70                                              const struct drm_mode_fb_cmd2 *mode_cmd);
71
72         /**
73          * @output_poll_changed:
74          *
75          * Callback used by helpers to inform the driver of output configuration
76          * changes.
77          *
78          * Drivers implementing fbdev emulation with the helpers can call
79          * drm_fb_helper_hotplug_changed from this hook to inform the fbdev
80          * helper of output changes.
81          *
82          * FIXME:
83          *
84          * Except that there's no vtable for device-level helper callbacks
85          * there's no reason this is a core function.
86          */
87         void (*output_poll_changed)(struct drm_device *dev);
88
89         /**
90          * @atomic_check:
91          *
92          * This is the only hook to validate an atomic modeset update. This
93          * function must reject any modeset and state changes which the hardware
94          * or driver doesn't support. This includes but is of course not limited
95          * to:
96          *
97          *  - Checking that the modes, framebuffers, scaling and placement
98          *    requirements and so on are within the limits of the hardware.
99          *
100          *  - Checking that any hidden shared resources are not oversubscribed.
101          *    This can be shared PLLs, shared lanes, overall memory bandwidth,
102          *    display fifo space (where shared between planes or maybe even
103          *    CRTCs).
104          *
105          *  - Checking that virtualized resources exported to userspace are not
106          *    oversubscribed. For various reasons it can make sense to expose
107          *    more planes, crtcs or encoders than which are physically there. One
108          *    example is dual-pipe operations (which generally should be hidden
109          *    from userspace if when lockstepped in hardware, exposed otherwise),
110          *    where a plane might need 1 hardware plane (if it's just on one
111          *    pipe), 2 hardware planes (when it spans both pipes) or maybe even
112          *    shared a hardware plane with a 2nd plane (if there's a compatible
113          *    plane requested on the area handled by the other pipe).
114          *
115          *  - Check that any transitional state is possible and that if
116          *    requested, the update can indeed be done in the vblank period
117          *    without temporarily disabling some functions.
118          *
119          *  - Check any other constraints the driver or hardware might have.
120          *
121          *  - This callback also needs to correctly fill out the &drm_crtc_state
122          *    in this update to make sure that drm_atomic_crtc_needs_modeset()
123          *    reflects the nature of the possible update and returns true if and
124          *    only if the update cannot be applied without tearing within one
125          *    vblank on that CRTC. The core uses that information to reject
126          *    updates which require a full modeset (i.e. blanking the screen, or
127          *    at least pausing updates for a substantial amount of time) if
128          *    userspace has disallowed that in its request.
129          *
130          *  - The driver also does not need to repeat basic input validation
131          *    like done for the corresponding legacy entry points. The core does
132          *    that before calling this hook.
133          *
134          * See the documentation of @atomic_commit for an exhaustive list of
135          * error conditions which don't have to be checked at the
136          * ->atomic_check() stage?
137          *
138          * See the documentation for struct &drm_atomic_state for how exactly
139          * an atomic modeset update is described.
140          *
141          * Drivers using the atomic helpers can implement this hook using
142          * drm_atomic_helper_check(), or one of the exported sub-functions of
143          * it.
144          *
145          * RETURNS:
146          *
147          * 0 on success or one of the below negative error codes:
148          *
149          *  - -EINVAL, if any of the above constraints are violated.
150          *
151          *  - -EDEADLK, when returned from an attempt to acquire an additional
152          *    &drm_modeset_lock through drm_modeset_lock().
153          *
154          *  - -ENOMEM, if allocating additional state sub-structures failed due
155          *    to lack of memory.
156          *
157          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
158          *    This can either be due to a pending signal, or because the driver
159          *    needs to completely bail out to recover from an exceptional
160          *    situation like a GPU hang. From a userspace point all errors are
161          *    treated equally.
162          */
163         int (*atomic_check)(struct drm_device *dev,
164                             struct drm_atomic_state *state);
165
166         /**
167          * @atomic_commit:
168          *
169          * This is the only hook to commit an atomic modeset update. The core
170          * guarantees that @atomic_check has been called successfully before
171          * calling this function, and that nothing has been changed in the
172          * interim.
173          *
174          * See the documentation for struct &drm_atomic_state for how exactly
175          * an atomic modeset update is described.
176          *
177          * Drivers using the atomic helpers can implement this hook using
178          * drm_atomic_helper_commit(), or one of the exported sub-functions of
179          * it.
180          *
181          * Nonblocking commits (as indicated with the nonblock parameter) must
182          * do any preparatory work which might result in an unsuccessful commit
183          * in the context of this callback. The only exceptions are hardware
184          * errors resulting in -EIO. But even in that case the driver must
185          * ensure that the display pipe is at least running, to avoid
186          * compositors crashing when pageflips don't work. Anything else,
187          * specifically committing the update to the hardware, should be done
188          * without blocking the caller. For updates which do not require a
189          * modeset this must be guaranteed.
190          *
191          * The driver must wait for any pending rendering to the new
192          * framebuffers to complete before executing the flip. It should also
193          * wait for any pending rendering from other drivers if the underlying
194          * buffer is a shared dma-buf. Nonblocking commits must not wait for
195          * rendering in the context of this callback.
196          *
197          * An application can request to be notified when the atomic commit has
198          * completed. These events are per-CRTC and can be distinguished by the
199          * CRTC index supplied in &drm_event to userspace.
200          *
201          * The drm core will supply a struct &drm_event in the event
202          * member of each CRTC's &drm_crtc_state structure. See the
203          * documentation for &drm_crtc_state for more details about the precise
204          * semantics of this event.
205          *
206          * NOTE:
207          *
208          * Drivers are not allowed to shut down any display pipe successfully
209          * enabled through an atomic commit on their own. Doing so can result in
210          * compositors crashing if a page flip is suddenly rejected because the
211          * pipe is off.
212          *
213          * RETURNS:
214          *
215          * 0 on success or one of the below negative error codes:
216          *
217          *  - -EBUSY, if a nonblocking updated is requested and there is
218          *    an earlier updated pending. Drivers are allowed to support a queue
219          *    of outstanding updates, but currently no driver supports that.
220          *    Note that drivers must wait for preceding updates to complete if a
221          *    synchronous update is requested, they are not allowed to fail the
222          *    commit in that case.
223          *
224          *  - -ENOMEM, if the driver failed to allocate memory. Specifically
225          *    this can happen when trying to pin framebuffers, which must only
226          *    be done when committing the state.
227          *
228          *  - -ENOSPC, as a refinement of the more generic -ENOMEM to indicate
229          *    that the driver has run out of vram, iommu space or similar GPU
230          *    address space needed for framebuffer.
231          *
232          *  - -EIO, if the hardware completely died.
233          *
234          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
235          *    This can either be due to a pending signal, or because the driver
236          *    needs to completely bail out to recover from an exceptional
237          *    situation like a GPU hang. From a userspace point of view all errors are
238          *    treated equally.
239          *
240          * This list is exhaustive. Specifically this hook is not allowed to
241          * return -EINVAL (any invalid requests should be caught in
242          * @atomic_check) or -EDEADLK (this function must not acquire
243          * additional modeset locks).
244          */
245         int (*atomic_commit)(struct drm_device *dev,
246                              struct drm_atomic_state *state,
247                              bool nonblock);
248
249         /**
250          * @atomic_state_alloc:
251          *
252          * This optional hook can be used by drivers that want to subclass struct
253          * &drm_atomic_state to be able to track their own driver-private global
254          * state easily. If this hook is implemented, drivers must also
255          * implement @atomic_state_clear and @atomic_state_free.
256          *
257          * RETURNS:
258          *
259          * A new &drm_atomic_state on success or NULL on failure.
260          */
261         struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev);
262
263         /**
264          * @atomic_state_clear:
265          *
266          * This hook must clear any driver private state duplicated into the
267          * passed-in &drm_atomic_state. This hook is called when the caller
268          * encountered a &drm_modeset_lock deadlock and needs to drop all
269          * already acquired locks as part of the deadlock avoidance dance
270          * implemented in drm_modeset_lock_backoff().
271          *
272          * Any duplicated state must be invalidated since a concurrent atomic
273          * update might change it, and the drm atomic interfaces always apply
274          * updates as relative changes to the current state.
275          *
276          * Drivers that implement this must call drm_atomic_state_default_clear()
277          * to clear common state.
278          */
279         void (*atomic_state_clear)(struct drm_atomic_state *state);
280
281         /**
282          * @atomic_state_free:
283          *
284          * This hook needs driver private resources and the &drm_atomic_state
285          * itself. Note that the core first calls drm_atomic_state_clear() to
286          * avoid code duplicate between the clear and free hooks.
287          *
288          * Drivers that implement this must call drm_atomic_state_default_free()
289          * to release common resources.
290          */
291         void (*atomic_state_free)(struct drm_atomic_state *state);
292 };
293
294 /**
295  * struct drm_mode_config - Mode configuration control structure
296  * @mutex: mutex protecting KMS related lists and structures
297  * @connection_mutex: ww mutex protecting connector state and routing
298  * @acquire_ctx: global implicit acquire context used by atomic drivers for
299  *      legacy IOCTLs
300  * @fb_lock: mutex to protect fb state and lists
301  * @num_fb: number of fbs available
302  * @fb_list: list of framebuffers available
303  * @num_encoder: number of encoders on this device
304  * @encoder_list: list of encoder objects
305  * @num_overlay_plane: number of overlay planes on this device
306  * @num_total_plane: number of universal (i.e. with primary/curso) planes on this device
307  * @plane_list: list of plane objects
308  * @num_crtc: number of CRTCs on this device
309  * @crtc_list: list of CRTC objects
310  * @property_list: list of property objects
311  * @min_width: minimum pixel width on this device
312  * @min_height: minimum pixel height on this device
313  * @max_width: maximum pixel width on this device
314  * @max_height: maximum pixel height on this device
315  * @funcs: core driver provided mode setting functions
316  * @fb_base: base address of the framebuffer
317  * @poll_enabled: track polling support for this device
318  * @poll_running: track polling status for this device
319  * @delayed_event: track delayed poll uevent deliver for this device
320  * @output_poll_work: delayed work for polling in process context
321  * @property_blob_list: list of all the blob property objects
322  * @blob_lock: mutex for blob property allocation and management
323  * @*_property: core property tracking
324  * @preferred_depth: preferred RBG pixel depth, used by fb helpers
325  * @prefer_shadow: hint to userspace to prefer shadow-fb rendering
326  * @cursor_width: hint to userspace for max cursor width
327  * @cursor_height: hint to userspace for max cursor height
328  * @helper_private: mid-layer private data
329  *
330  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
331  * enumerated by the driver are added here, as are global properties.  Some
332  * global restrictions are also here, e.g. dimension restrictions.
333  */
334 struct drm_mode_config {
335         struct mutex mutex; /* protects configuration (mode lists etc.) */
336         struct drm_modeset_lock connection_mutex; /* protects connector->encoder and encoder->crtc links */
337         struct drm_modeset_acquire_ctx *acquire_ctx; /* for legacy _lock_all() / _unlock_all() */
338
339         /**
340          * @idr_mutex:
341          *
342          * Mutex for KMS ID allocation and management. Protects both @crtc_idr
343          * and @tile_idr.
344          */
345         struct mutex idr_mutex;
346
347         /**
348          * @crtc_idr:
349          *
350          * Main KMS ID tracking object. Use this idr for all IDs, fb, crtc,
351          * connector, modes - just makes life easier to have only one.
352          */
353         struct idr crtc_idr;
354
355         /**
356          * @tile_idr:
357          *
358          * Use this idr for allocating new IDs for tiled sinks like use in some
359          * high-res DP MST screens.
360          */
361         struct idr tile_idr;
362
363         struct mutex fb_lock; /* proctects global and per-file fb lists */
364         int num_fb;
365         struct list_head fb_list;
366
367         /**
368          * @num_connector: Number of connectors on this device.
369          */
370         int num_connector;
371         /**
372          * @connector_ida: ID allocator for connector indices.
373          */
374         struct ida connector_ida;
375         /**
376          * @connector_list: List of connector objects.
377          */
378         struct list_head connector_list;
379         int num_encoder;
380         struct list_head encoder_list;
381
382         /*
383          * Track # of overlay planes separately from # of total planes.  By
384          * default we only advertise overlay planes to userspace; if userspace
385          * sets the "universal plane" capability bit, we'll go ahead and
386          * expose all planes.
387          */
388         int num_overlay_plane;
389         int num_total_plane;
390         struct list_head plane_list;
391
392         int num_crtc;
393         struct list_head crtc_list;
394
395         struct list_head property_list;
396
397         int min_width, min_height;
398         int max_width, max_height;
399         const struct drm_mode_config_funcs *funcs;
400         resource_size_t fb_base;
401
402         /* output poll support */
403         bool poll_enabled;
404         bool poll_running;
405         bool delayed_event;
406         struct delayed_work output_poll_work;
407
408         struct mutex blob_lock;
409
410         /* pointers to standard properties */
411         struct list_head property_blob_list;
412         /**
413          * @edid_property: Default connector property to hold the EDID of the
414          * currently connected sink, if any.
415          */
416         struct drm_property *edid_property;
417         /**
418          * @dpms_property: Default connector property to control the
419          * connector's DPMS state.
420          */
421         struct drm_property *dpms_property;
422         /**
423          * @path_property: Default connector property to hold the DP MST path
424          * for the port.
425          */
426         struct drm_property *path_property;
427         /**
428          * @tile_property: Default connector property to store the tile
429          * position of a tiled screen, for sinks which need to be driven with
430          * multiple CRTCs.
431          */
432         struct drm_property *tile_property;
433         /**
434          * @plane_type_property: Default plane property to differentiate
435          * CURSOR, PRIMARY and OVERLAY legacy uses of planes.
436          */
437         struct drm_property *plane_type_property;
438         /**
439          * @prop_src_x: Default atomic plane property for the plane source
440          * position in the connected &drm_framebuffer.
441          */
442         struct drm_property *prop_src_x;
443         /**
444          * @prop_src_y: Default atomic plane property for the plane source
445          * position in the connected &drm_framebuffer.
446          */
447         struct drm_property *prop_src_y;
448         /**
449          * @prop_src_w: Default atomic plane property for the plane source
450          * position in the connected &drm_framebuffer.
451          */
452         struct drm_property *prop_src_w;
453         /**
454          * @prop_src_h: Default atomic plane property for the plane source
455          * position in the connected &drm_framebuffer.
456          */
457         struct drm_property *prop_src_h;
458         /**
459          * @prop_crtc_x: Default atomic plane property for the plane destination
460          * position in the &drm_crtc is is being shown on.
461          */
462         struct drm_property *prop_crtc_x;
463         /**
464          * @prop_crtc_y: Default atomic plane property for the plane destination
465          * position in the &drm_crtc is is being shown on.
466          */
467         struct drm_property *prop_crtc_y;
468         /**
469          * @prop_crtc_w: Default atomic plane property for the plane destination
470          * position in the &drm_crtc is is being shown on.
471          */
472         struct drm_property *prop_crtc_w;
473         /**
474          * @prop_crtc_h: Default atomic plane property for the plane destination
475          * position in the &drm_crtc is is being shown on.
476          */
477         struct drm_property *prop_crtc_h;
478         /**
479          * @prop_fb_id: Default atomic plane property to specify the
480          * &drm_framebuffer.
481          */
482         struct drm_property *prop_fb_id;
483         /**
484          * @prop_in_fence_fd: Sync File fd representing the incoming fences
485          * for a Plane.
486          */
487         struct drm_property *prop_in_fence_fd;
488         /**
489          * @prop_out_fence_ptr: Sync File fd pointer representing the
490          * outgoing fences for a CRTC. Userspace should provide a pointer to a
491          * value of type s64, and then cast that pointer to u64.
492          */
493         struct drm_property *prop_out_fence_ptr;
494         /**
495          * @prop_crtc_id: Default atomic plane property to specify the
496          * &drm_crtc.
497          */
498         struct drm_property *prop_crtc_id;
499         /**
500          * @prop_active: Default atomic CRTC property to control the active
501          * state, which is the simplified implementation for DPMS in atomic
502          * drivers.
503          */
504         struct drm_property *prop_active;
505         /**
506          * @prop_mode_id: Default atomic CRTC property to set the mode for a
507          * CRTC. A 0 mode implies that the CRTC is entirely disabled - all
508          * connectors must be of and active must be set to disabled, too.
509          */
510         struct drm_property *prop_mode_id;
511
512         /**
513          * @dvi_i_subconnector_property: Optional DVI-I property to
514          * differentiate between analog or digital mode.
515          */
516         struct drm_property *dvi_i_subconnector_property;
517         /**
518          * @dvi_i_select_subconnector_property: Optional DVI-I property to
519          * select between analog or digital mode.
520          */
521         struct drm_property *dvi_i_select_subconnector_property;
522
523         /**
524          * @tv_subconnector_property: Optional TV property to differentiate
525          * between different TV connector types.
526          */
527         struct drm_property *tv_subconnector_property;
528         /**
529          * @tv_select_subconnector_property: Optional TV property to select
530          * between different TV connector types.
531          */
532         struct drm_property *tv_select_subconnector_property;
533         /**
534          * @tv_mode_property: Optional TV property to select
535          * the output TV mode.
536          */
537         struct drm_property *tv_mode_property;
538         /**
539          * @tv_left_margin_property: Optional TV property to set the left
540          * margin.
541          */
542         struct drm_property *tv_left_margin_property;
543         /**
544          * @tv_right_margin_property: Optional TV property to set the right
545          * margin.
546          */
547         struct drm_property *tv_right_margin_property;
548         /**
549          * @tv_top_margin_property: Optional TV property to set the right
550          * margin.
551          */
552         struct drm_property *tv_top_margin_property;
553         /**
554          * @tv_bottom_margin_property: Optional TV property to set the right
555          * margin.
556          */
557         struct drm_property *tv_bottom_margin_property;
558         /**
559          * @tv_brightness_property: Optional TV property to set the
560          * brightness.
561          */
562         struct drm_property *tv_brightness_property;
563         /**
564          * @tv_contrast_property: Optional TV property to set the
565          * contrast.
566          */
567         struct drm_property *tv_contrast_property;
568         /**
569          * @tv_flicker_reduction_property: Optional TV property to control the
570          * flicker reduction mode.
571          */
572         struct drm_property *tv_flicker_reduction_property;
573         /**
574          * @tv_overscan_property: Optional TV property to control the overscan
575          * setting.
576          */
577         struct drm_property *tv_overscan_property;
578         /**
579          * @tv_saturation_property: Optional TV property to set the
580          * saturation.
581          */
582         struct drm_property *tv_saturation_property;
583         /**
584          * @tv_hue_property: Optional TV property to set the hue.
585          */
586         struct drm_property *tv_hue_property;
587
588         /**
589          * @scaling_mode_property: Optional connector property to control the
590          * upscaling, mostly used for built-in panels.
591          */
592         struct drm_property *scaling_mode_property;
593         /**
594          * @aspect_ratio_property: Optional connector property to control the
595          * HDMI infoframe aspect ratio setting.
596          */
597         struct drm_property *aspect_ratio_property;
598         /**
599          * @degamma_lut_property: Optional CRTC property to set the LUT used to
600          * convert the framebuffer's colors to linear gamma.
601          */
602         struct drm_property *degamma_lut_property;
603         /**
604          * @degamma_lut_size_property: Optional CRTC property for the size of
605          * the degamma LUT as supported by the driver (read-only).
606          */
607         struct drm_property *degamma_lut_size_property;
608         /**
609          * @ctm_property: Optional CRTC property to set the
610          * matrix used to convert colors after the lookup in the
611          * degamma LUT.
612          */
613         struct drm_property *ctm_property;
614         /**
615          * @gamma_lut_property: Optional CRTC property to set the LUT used to
616          * convert the colors, after the CTM matrix, to the gamma space of the
617          * connected screen.
618          */
619         struct drm_property *gamma_lut_property;
620         /**
621          * @gamma_lut_size_property: Optional CRTC property for the size of the
622          * gamma LUT as supported by the driver (read-only).
623          */
624         struct drm_property *gamma_lut_size_property;
625
626         /**
627          * @suggested_x_property: Optional connector property with a hint for
628          * the position of the output on the host's screen.
629          */
630         struct drm_property *suggested_x_property;
631         /**
632          * @suggested_y_property: Optional connector property with a hint for
633          * the position of the output on the host's screen.
634          */
635         struct drm_property *suggested_y_property;
636
637         /* dumb ioctl parameters */
638         uint32_t preferred_depth, prefer_shadow;
639
640         /**
641          * @async_page_flip: Does this device support async flips on the primary
642          * plane?
643          */
644         bool async_page_flip;
645
646         /**
647          * @allow_fb_modifiers:
648          *
649          * Whether the driver supports fb modifiers in the ADDFB2.1 ioctl call.
650          */
651         bool allow_fb_modifiers;
652
653         /* cursor size */
654         uint32_t cursor_width, cursor_height;
655
656         struct drm_mode_config_helper_funcs *helper_private;
657 };
658
659 void drm_mode_config_init(struct drm_device *dev);
660 void drm_mode_config_reset(struct drm_device *dev);
661 void drm_mode_config_cleanup(struct drm_device *dev);
662
663 #endif