drm/mode: introduce wrapper to read framebuffer refcount.
[cascardo/linux.git] / include / drm / drm_crtc.h
1 /*
2  * Copyright © 2006 Keith Packard
3  * Copyright © 2007-2008 Dave Airlie
4  * Copyright © 2007-2008 Intel Corporation
5  *   Jesse Barnes <jesse.barnes@intel.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef __DRM_CRTC_H__
26 #define __DRM_CRTC_H__
27
28 #include <linux/i2c.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/idr.h>
32 #include <linux/fb.h>
33 #include <linux/hdmi.h>
34 #include <linux/media-bus-format.h>
35 #include <uapi/drm/drm_mode.h>
36 #include <uapi/drm/drm_fourcc.h>
37 #include <drm/drm_modeset_lock.h>
38
39 struct drm_device;
40 struct drm_mode_set;
41 struct drm_framebuffer;
42 struct drm_object_properties;
43 struct drm_file;
44 struct drm_clip_rect;
45 struct device_node;
46 struct fence;
47
48 struct drm_mode_object {
49         uint32_t id;
50         uint32_t type;
51         struct drm_object_properties *properties;
52 };
53
54 #define DRM_OBJECT_MAX_PROPERTY 24
55 struct drm_object_properties {
56         int count, atomic_count;
57         /* NOTE: if we ever start dynamically destroying properties (ie.
58          * not at drm_mode_config_cleanup() time), then we'd have to do
59          * a better job of detaching property from mode objects to avoid
60          * dangling property pointers:
61          */
62         struct drm_property *properties[DRM_OBJECT_MAX_PROPERTY];
63         /* do not read/write values directly, but use drm_object_property_get_value()
64          * and drm_object_property_set_value():
65          */
66         uint64_t values[DRM_OBJECT_MAX_PROPERTY];
67 };
68
69 static inline int64_t U642I64(uint64_t val)
70 {
71         return (int64_t)*((int64_t *)&val);
72 }
73 static inline uint64_t I642U64(int64_t val)
74 {
75         return (uint64_t)*((uint64_t *)&val);
76 }
77
78 /*
79  * Rotation property bits. DRM_ROTATE_<degrees> rotates the image by the
80  * specified amount in degrees in counter clockwise direction. DRM_REFLECT_X and
81  * DRM_REFLECT_Y reflects the image along the specified axis prior to rotation
82  */
83 #define DRM_ROTATE_MASK 0x0f
84 #define DRM_ROTATE_0    0
85 #define DRM_ROTATE_90   1
86 #define DRM_ROTATE_180  2
87 #define DRM_ROTATE_270  3
88 #define DRM_REFLECT_MASK (~DRM_ROTATE_MASK)
89 #define DRM_REFLECT_X   4
90 #define DRM_REFLECT_Y   5
91
92 enum drm_connector_force {
93         DRM_FORCE_UNSPECIFIED,
94         DRM_FORCE_OFF,
95         DRM_FORCE_ON,         /* force on analog part normally */
96         DRM_FORCE_ON_DIGITAL, /* for DVI-I use digital connector */
97 };
98
99 #include <drm/drm_modes.h>
100
101 enum drm_connector_status {
102         connector_status_connected = 1,
103         connector_status_disconnected = 2,
104         connector_status_unknown = 3,
105 };
106
107 enum subpixel_order {
108         SubPixelUnknown = 0,
109         SubPixelHorizontalRGB,
110         SubPixelHorizontalBGR,
111         SubPixelVerticalRGB,
112         SubPixelVerticalBGR,
113         SubPixelNone,
114 };
115
116 #define DRM_COLOR_FORMAT_RGB444         (1<<0)
117 #define DRM_COLOR_FORMAT_YCRCB444       (1<<1)
118 #define DRM_COLOR_FORMAT_YCRCB422       (1<<2)
119 /*
120  * Describes a given display (e.g. CRT or flat panel) and its limitations.
121  */
122 struct drm_display_info {
123         char name[DRM_DISPLAY_INFO_LEN];
124
125         /* Physical size */
126         unsigned int width_mm;
127         unsigned int height_mm;
128
129         /* Clock limits FIXME: storage format */
130         unsigned int min_vfreq, max_vfreq;
131         unsigned int min_hfreq, max_hfreq;
132         unsigned int pixel_clock;
133         unsigned int bpc;
134
135         enum subpixel_order subpixel_order;
136         u32 color_formats;
137
138         const u32 *bus_formats;
139         unsigned int num_bus_formats;
140
141         /* Mask of supported hdmi deep color modes */
142         u8 edid_hdmi_dc_modes;
143
144         u8 cea_rev;
145 };
146
147 /* data corresponds to displayid vend/prod/serial */
148 struct drm_tile_group {
149         struct kref refcount;
150         struct drm_device *dev;
151         int id;
152         u8 group_data[8];
153 };
154
155 /**
156  * struct drm_framebuffer_funcs - framebuffer hooks
157  */
158 struct drm_framebuffer_funcs {
159         /**
160          * @destroy:
161          *
162          * Clean up framebuffer resources, specifically also unreference the
163          * backing storage. The core guarantees to call this function for every
164          * framebuffer successfully created by ->fb_create() in
165          * &drm_mode_config_funcs. Drivers must also call
166          * drm_framebuffer_cleanup() to release DRM core resources for this
167          * framebuffer.
168          */
169         void (*destroy)(struct drm_framebuffer *framebuffer);
170
171         /**
172          * @create_handle:
173          *
174          * Create a buffer handle in the driver-specific buffer manager (either
175          * GEM or TTM) valid for the passed-in struct &drm_file. This is used by
176          * the core to implement the GETFB IOCTL, which returns (for
177          * sufficiently priviledged user) also a native buffer handle. This can
178          * be used for seamless transitions between modesetting clients by
179          * copying the current screen contents to a private buffer and blending
180          * between that and the new contents.
181          *
182          * GEM based drivers should call drm_gem_handle_create() to create the
183          * handle.
184          *
185          * RETURNS:
186          *
187          * 0 on success or a negative error code on failure.
188          */
189         int (*create_handle)(struct drm_framebuffer *fb,
190                              struct drm_file *file_priv,
191                              unsigned int *handle);
192         /**
193          * @dirty:
194          *
195          * Optional callback for the dirty fb IOCTL.
196          *
197          * Userspace can notify the driver via this callback that an area of the
198          * framebuffer has changed and should be flushed to the display
199          * hardware. This can also be used internally, e.g. by the fbdev
200          * emulation, though that's not the case currently.
201          *
202          * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
203          * for more information as all the semantics and arguments have a one to
204          * one mapping on this function.
205          *
206          * RETURNS:
207          *
208          * 0 on success or a negative error code on failure.
209          */
210         int (*dirty)(struct drm_framebuffer *framebuffer,
211                      struct drm_file *file_priv, unsigned flags,
212                      unsigned color, struct drm_clip_rect *clips,
213                      unsigned num_clips);
214 };
215
216 struct drm_framebuffer {
217         struct drm_device *dev;
218         /*
219          * Note that the fb is refcounted for the benefit of driver internals,
220          * for example some hw, disabling a CRTC/plane is asynchronous, and
221          * scanout does not actually complete until the next vblank.  So some
222          * cleanup (like releasing the reference(s) on the backing GEM bo(s))
223          * should be deferred.  In cases like this, the driver would like to
224          * hold a ref to the fb even though it has already been removed from
225          * userspace perspective.
226          */
227         struct kref refcount;
228         /*
229          * Place on the dev->mode_config.fb_list, access protected by
230          * dev->mode_config.fb_lock.
231          */
232         struct list_head head;
233         struct drm_mode_object base;
234         const struct drm_framebuffer_funcs *funcs;
235         unsigned int pitches[4];
236         unsigned int offsets[4];
237         uint64_t modifier[4];
238         unsigned int width;
239         unsigned int height;
240         /* depth can be 15 or 16 */
241         unsigned int depth;
242         int bits_per_pixel;
243         int flags;
244         uint32_t pixel_format; /* fourcc format */
245         struct list_head filp_head;
246 };
247
248 struct drm_property_blob {
249         struct drm_mode_object base;
250         struct drm_device *dev;
251         struct kref refcount;
252         struct list_head head_global;
253         struct list_head head_file;
254         size_t length;
255         unsigned char data[];
256 };
257
258 struct drm_property_enum {
259         uint64_t value;
260         struct list_head head;
261         char name[DRM_PROP_NAME_LEN];
262 };
263
264 struct drm_property {
265         struct list_head head;
266         struct drm_mode_object base;
267         uint32_t flags;
268         char name[DRM_PROP_NAME_LEN];
269         uint32_t num_values;
270         uint64_t *values;
271         struct drm_device *dev;
272
273         struct list_head enum_list;
274 };
275
276 struct drm_crtc;
277 struct drm_connector;
278 struct drm_encoder;
279 struct drm_pending_vblank_event;
280 struct drm_plane;
281 struct drm_bridge;
282 struct drm_atomic_state;
283
284 struct drm_crtc_helper_funcs;
285 struct drm_encoder_helper_funcs;
286 struct drm_connector_helper_funcs;
287 struct drm_plane_helper_funcs;
288
289 /**
290  * struct drm_crtc_state - mutable CRTC state
291  * @crtc: backpointer to the CRTC
292  * @enable: whether the CRTC should be enabled, gates all other state
293  * @active: whether the CRTC is actively displaying (used for DPMS)
294  * @planes_changed: planes on this crtc are updated
295  * @mode_changed: crtc_state->mode or crtc_state->enable has been changed
296  * @active_changed: crtc_state->active has been toggled.
297  * @connectors_changed: connectors to this crtc have been updated
298  * @color_mgmt_changed: color management properties have changed (degamma or
299  *      gamma LUT or CSC matrix)
300  * @plane_mask: bitmask of (1 << drm_plane_index(plane)) of attached planes
301  * @connector_mask: bitmask of (1 << drm_connector_index(connector)) of attached connectors
302  * @encoder_mask: bitmask of (1 << drm_encoder_index(encoder)) of attached encoders
303  * @last_vblank_count: for helpers and drivers to capture the vblank of the
304  *      update to ensure framebuffer cleanup isn't done too early
305  * @adjusted_mode: for use by helpers and drivers to compute adjusted mode timings
306  * @mode: current mode timings
307  * @degamma_lut: Lookup table for converting framebuffer pixel data
308  *      before apply the conversion matrix
309  * @ctm: Transformation matrix
310  * @gamma_lut: Lookup table for converting pixel data after the
311  *      conversion matrix
312  * @event: optional pointer to a DRM event to signal upon completion of the
313  *      state update
314  * @state: backpointer to global drm_atomic_state
315  *
316  * Note that the distinction between @enable and @active is rather subtile:
317  * Flipping @active while @enable is set without changing anything else may
318  * never return in a failure from the ->atomic_check callback. Userspace assumes
319  * that a DPMS On will always succeed. In other words: @enable controls resource
320  * assignment, @active controls the actual hardware state.
321  */
322 struct drm_crtc_state {
323         struct drm_crtc *crtc;
324
325         bool enable;
326         bool active;
327
328         /* computed state bits used by helpers and drivers */
329         bool planes_changed : 1;
330         bool mode_changed : 1;
331         bool active_changed : 1;
332         bool connectors_changed : 1;
333         bool color_mgmt_changed : 1;
334
335         /* attached planes bitmask:
336          * WARNING: transitional helpers do not maintain plane_mask so
337          * drivers not converted over to atomic helpers should not rely
338          * on plane_mask being accurate!
339          */
340         u32 plane_mask;
341
342         u32 connector_mask;
343         u32 encoder_mask;
344
345         /* last_vblank_count: for vblank waits before cleanup */
346         u32 last_vblank_count;
347
348         /* adjusted_mode: for use by helpers and drivers */
349         struct drm_display_mode adjusted_mode;
350
351         struct drm_display_mode mode;
352
353         /* blob property to expose current mode to atomic userspace */
354         struct drm_property_blob *mode_blob;
355
356         /* blob property to expose color management to userspace */
357         struct drm_property_blob *degamma_lut;
358         struct drm_property_blob *ctm;
359         struct drm_property_blob *gamma_lut;
360
361         struct drm_pending_vblank_event *event;
362
363         struct drm_atomic_state *state;
364 };
365
366 /**
367  * struct drm_crtc_funcs - control CRTCs for a given device
368  *
369  * The drm_crtc_funcs structure is the central CRTC management structure
370  * in the DRM.  Each CRTC controls one or more connectors (note that the name
371  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
372  * connectors, not just CRTs).
373  *
374  * Each driver is responsible for filling out this structure at startup time,
375  * in addition to providing other modesetting features, like i2c and DDC
376  * bus accessors.
377  */
378 struct drm_crtc_funcs {
379         /**
380          * @reset:
381          *
382          * Reset CRTC hardware and software state to off. This function isn't
383          * called by the core directly, only through drm_mode_config_reset().
384          * It's not a helper hook only for historical reasons.
385          *
386          * Atomic drivers can use drm_atomic_helper_crtc_reset() to reset
387          * atomic state using this hook.
388          */
389         void (*reset)(struct drm_crtc *crtc);
390
391         /**
392          * @cursor_set:
393          *
394          * Update the cursor image. The cursor position is relative to the CRTC
395          * and can be partially or fully outside of the visible area.
396          *
397          * Note that contrary to all other KMS functions the legacy cursor entry
398          * points don't take a framebuffer object, but instead take directly a
399          * raw buffer object id from the driver's buffer manager (which is
400          * either GEM or TTM for current drivers).
401          *
402          * This entry point is deprecated, drivers should instead implement
403          * universal plane support and register a proper cursor plane using
404          * drm_crtc_init_with_planes().
405          *
406          * This callback is optional
407          *
408          * RETURNS:
409          *
410          * 0 on success or a negative error code on failure.
411          */
412         int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
413                           uint32_t handle, uint32_t width, uint32_t height);
414
415         /**
416          * @cursor_set2:
417          *
418          * Update the cursor image, including hotspot information. The hotspot
419          * must not affect the cursor position in CRTC coordinates, but is only
420          * meant as a hint for virtualized display hardware to coordinate the
421          * guests and hosts cursor position. The cursor hotspot is relative to
422          * the cursor image. Otherwise this works exactly like @cursor_set.
423          *
424          * This entry point is deprecated, drivers should instead implement
425          * universal plane support and register a proper cursor plane using
426          * drm_crtc_init_with_planes().
427          *
428          * This callback is optional.
429          *
430          * RETURNS:
431          *
432          * 0 on success or a negative error code on failure.
433          */
434         int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
435                            uint32_t handle, uint32_t width, uint32_t height,
436                            int32_t hot_x, int32_t hot_y);
437
438         /**
439          * @cursor_move:
440          *
441          * Update the cursor position. The cursor does not need to be visible
442          * when this hook is called.
443          *
444          * This entry point is deprecated, drivers should instead implement
445          * universal plane support and register a proper cursor plane using
446          * drm_crtc_init_with_planes().
447          *
448          * This callback is optional.
449          *
450          * RETURNS:
451          *
452          * 0 on success or a negative error code on failure.
453          */
454         int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
455
456         /**
457          * @gamma_set:
458          *
459          * Set gamma on the CRTC.
460          *
461          * This callback is optional.
462          *
463          * NOTE:
464          *
465          * Drivers that support gamma tables and also fbdev emulation through
466          * the provided helper library need to take care to fill out the gamma
467          * hooks for both. Currently there's a bit an unfortunate duplication
468          * going on, which should eventually be unified to just one set of
469          * hooks.
470          */
471         void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
472                           uint32_t start, uint32_t size);
473
474         /**
475          * @destroy:
476          *
477          * Clean up plane resources. This is only called at driver unload time
478          * through drm_mode_config_cleanup() since a CRTC cannot be hotplugged
479          * in DRM.
480          */
481         void (*destroy)(struct drm_crtc *crtc);
482
483         /**
484          * @set_config:
485          *
486          * This is the main legacy entry point to change the modeset state on a
487          * CRTC. All the details of the desired configuration are passed in a
488          * struct &drm_mode_set - see there for details.
489          *
490          * Drivers implementing atomic modeset should use
491          * drm_atomic_helper_set_config() to implement this hook.
492          *
493          * RETURNS:
494          *
495          * 0 on success or a negative error code on failure.
496          */
497         int (*set_config)(struct drm_mode_set *set);
498
499         /**
500          * @page_flip:
501          *
502          * Legacy entry point to schedule a flip to the given framebuffer.
503          *
504          * Page flipping is a synchronization mechanism that replaces the frame
505          * buffer being scanned out by the CRTC with a new frame buffer during
506          * vertical blanking, avoiding tearing (except when requested otherwise
507          * through the DRM_MODE_PAGE_FLIP_ASYNC flag). When an application
508          * requests a page flip the DRM core verifies that the new frame buffer
509          * is large enough to be scanned out by the CRTC in the currently
510          * configured mode and then calls the CRTC ->page_flip() operation with a
511          * pointer to the new frame buffer.
512          *
513          * The driver must wait for any pending rendering to the new framebuffer
514          * to complete before executing the flip. It should also wait for any
515          * pending rendering from other drivers if the underlying buffer is a
516          * shared dma-buf.
517          *
518          * An application can request to be notified when the page flip has
519          * completed. The drm core will supply a struct &drm_event in the event
520          * parameter in this case. This can be handled by the
521          * drm_crtc_send_vblank_event() function, which the driver should call on
522          * the provided event upon completion of the flip. Note that if
523          * the driver supports vblank signalling and timestamping the vblank
524          * counters and timestamps must agree with the ones returned from page
525          * flip events. With the current vblank helper infrastructure this can
526          * be achieved by holding a vblank reference while the page flip is
527          * pending, acquired through drm_crtc_vblank_get() and released with
528          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
529          * counter and timestamp tracking though, e.g. if they have accurate
530          * timestamp registers in hardware.
531          *
532          * FIXME:
533          *
534          * Up to that point drivers need to manage events themselves and can use
535          * even->base.list freely for that. Specifically they need to ensure
536          * that they don't send out page flip (or vblank) events for which the
537          * corresponding drm file has been closed already. The drm core
538          * unfortunately does not (yet) take care of that. Therefore drivers
539          * currently must clean up and release pending events in their
540          * ->preclose driver function.
541          *
542          * This callback is optional.
543          *
544          * NOTE:
545          *
546          * Very early versions of the KMS ABI mandated that the driver must
547          * block (but not reject) any rendering to the old framebuffer until the
548          * flip operation has completed and the old framebuffer is no longer
549          * visible. This requirement has been lifted, and userspace is instead
550          * expected to request delivery of an event and wait with recycling old
551          * buffers until such has been received.
552          *
553          * RETURNS:
554          *
555          * 0 on success or a negative error code on failure. Note that if a
556          * ->page_flip() operation is already pending the callback should return
557          * -EBUSY. Pageflips on a disabled CRTC (either by setting a NULL mode
558          * or just runtime disabled through DPMS respectively the new atomic
559          * "ACTIVE" state) should result in an -EINVAL error code. Note that
560          * drm_atomic_helper_page_flip() checks this already for atomic drivers.
561          */
562         int (*page_flip)(struct drm_crtc *crtc,
563                          struct drm_framebuffer *fb,
564                          struct drm_pending_vblank_event *event,
565                          uint32_t flags);
566
567         /**
568          * @set_property:
569          *
570          * This is the legacy entry point to update a property attached to the
571          * CRTC.
572          *
573          * Drivers implementing atomic modeset should use
574          * drm_atomic_helper_crtc_set_property() to implement this hook.
575          *
576          * This callback is optional if the driver does not support any legacy
577          * driver-private properties.
578          *
579          * RETURNS:
580          *
581          * 0 on success or a negative error code on failure.
582          */
583         int (*set_property)(struct drm_crtc *crtc,
584                             struct drm_property *property, uint64_t val);
585
586         /**
587          * @atomic_duplicate_state:
588          *
589          * Duplicate the current atomic state for this CRTC and return it.
590          * The core and helpers gurantee that any atomic state duplicated with
591          * this hook and still owned by the caller (i.e. not transferred to the
592          * driver by calling ->atomic_commit() from struct
593          * &drm_mode_config_funcs) will be cleaned up by calling the
594          * @atomic_destroy_state hook in this structure.
595          *
596          * Atomic drivers which don't subclass struct &drm_crtc should use
597          * drm_atomic_helper_crtc_duplicate_state(). Drivers that subclass the
598          * state structure to extend it with driver-private state should use
599          * __drm_atomic_helper_crtc_duplicate_state() to make sure shared state is
600          * duplicated in a consistent fashion across drivers.
601          *
602          * It is an error to call this hook before crtc->state has been
603          * initialized correctly.
604          *
605          * NOTE:
606          *
607          * If the duplicate state references refcounted resources this hook must
608          * acquire a reference for each of them. The driver must release these
609          * references again in @atomic_destroy_state.
610          *
611          * RETURNS:
612          *
613          * Duplicated atomic state or NULL when the allocation failed.
614          */
615         struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);
616
617         /**
618          * @atomic_destroy_state:
619          *
620          * Destroy a state duplicated with @atomic_duplicate_state and release
621          * or unreference all resources it references
622          */
623         void (*atomic_destroy_state)(struct drm_crtc *crtc,
624                                      struct drm_crtc_state *state);
625
626         /**
627          * @atomic_set_property:
628          *
629          * Decode a driver-private property value and store the decoded value
630          * into the passed-in state structure. Since the atomic core decodes all
631          * standardized properties (even for extensions beyond the core set of
632          * properties which might not be implemented by all drivers) this
633          * requires drivers to subclass the state structure.
634          *
635          * Such driver-private properties should really only be implemented for
636          * truly hardware/vendor specific state. Instead it is preferred to
637          * standardize atomic extension and decode the properties used to expose
638          * such an extension in the core.
639          *
640          * Do not call this function directly, use
641          * drm_atomic_crtc_set_property() instead.
642          *
643          * This callback is optional if the driver does not support any
644          * driver-private atomic properties.
645          *
646          * NOTE:
647          *
648          * This function is called in the state assembly phase of atomic
649          * modesets, which can be aborted for any reason (including on
650          * userspace's request to just check whether a configuration would be
651          * possible). Drivers MUST NOT touch any persistent state (hardware or
652          * software) or data structures except the passed in @state parameter.
653          *
654          * Also since userspace controls in which order properties are set this
655          * function must not do any input validation (since the state update is
656          * incomplete and hence likely inconsistent). Instead any such input
657          * validation must be done in the various atomic_check callbacks.
658          *
659          * RETURNS:
660          *
661          * 0 if the property has been found, -EINVAL if the property isn't
662          * implemented by the driver (which should never happen, the core only
663          * asks for properties attached to this CRTC). No other validation is
664          * allowed by the driver. The core already checks that the property
665          * value is within the range (integer, valid enum value, ...) the driver
666          * set when registering the property.
667          */
668         int (*atomic_set_property)(struct drm_crtc *crtc,
669                                    struct drm_crtc_state *state,
670                                    struct drm_property *property,
671                                    uint64_t val);
672         /**
673          * @atomic_get_property:
674          *
675          * Reads out the decoded driver-private property. This is used to
676          * implement the GETCRTC IOCTL.
677          *
678          * Do not call this function directly, use
679          * drm_atomic_crtc_get_property() instead.
680          *
681          * This callback is optional if the driver does not support any
682          * driver-private atomic properties.
683          *
684          * RETURNS:
685          *
686          * 0 on success, -EINVAL if the property isn't implemented by the
687          * driver (which should never happen, the core only asks for
688          * properties attached to this CRTC).
689          */
690         int (*atomic_get_property)(struct drm_crtc *crtc,
691                                    const struct drm_crtc_state *state,
692                                    struct drm_property *property,
693                                    uint64_t *val);
694 };
695
696 /**
697  * struct drm_crtc - central CRTC control structure
698  * @dev: parent DRM device
699  * @port: OF node used by drm_of_find_possible_crtcs()
700  * @head: list management
701  * @mutex: per-CRTC locking
702  * @base: base KMS object for ID tracking etc.
703  * @primary: primary plane for this CRTC
704  * @cursor: cursor plane for this CRTC
705  * @cursor_x: current x position of the cursor, used for universal cursor planes
706  * @cursor_y: current y position of the cursor, used for universal cursor planes
707  * @enabled: is this CRTC enabled?
708  * @mode: current mode timings
709  * @hwmode: mode timings as programmed to hw regs
710  * @x: x position on screen
711  * @y: y position on screen
712  * @funcs: CRTC control functions
713  * @gamma_size: size of gamma ramp
714  * @gamma_store: gamma ramp values
715  * @helper_private: mid-layer private data
716  * @properties: property tracking for this CRTC
717  * @state: current atomic state for this CRTC
718  * @acquire_ctx: per-CRTC implicit acquire context used by atomic drivers for
719  *      legacy IOCTLs
720  *
721  * Each CRTC may have one or more connectors associated with it.  This structure
722  * allows the CRTC to be controlled.
723  */
724 struct drm_crtc {
725         struct drm_device *dev;
726         struct device_node *port;
727         struct list_head head;
728
729         char *name;
730
731         /*
732          * crtc mutex
733          *
734          * This provides a read lock for the overall crtc state (mode, dpms
735          * state, ...) and a write lock for everything which can be update
736          * without a full modeset (fb, cursor data, ...)
737          */
738         struct drm_modeset_lock mutex;
739
740         struct drm_mode_object base;
741
742         /* primary and cursor planes for CRTC */
743         struct drm_plane *primary;
744         struct drm_plane *cursor;
745
746         /* position of cursor plane on crtc */
747         int cursor_x;
748         int cursor_y;
749
750         bool enabled;
751
752         /* Requested mode from modesetting. */
753         struct drm_display_mode mode;
754
755         /* Programmed mode in hw, after adjustments for encoders,
756          * crtc, panel scaling etc. Needed for timestamping etc.
757          */
758         struct drm_display_mode hwmode;
759
760         int x, y;
761         const struct drm_crtc_funcs *funcs;
762
763         /* Legacy FB CRTC gamma size for reporting to userspace */
764         uint32_t gamma_size;
765         uint16_t *gamma_store;
766
767         /* if you are using the helper */
768         const struct drm_crtc_helper_funcs *helper_private;
769
770         struct drm_object_properties properties;
771
772         struct drm_crtc_state *state;
773
774         /*
775          * For legacy crtc IOCTLs so that atomic drivers can get at the locking
776          * acquire context.
777          */
778         struct drm_modeset_acquire_ctx *acquire_ctx;
779 };
780
781 /**
782  * struct drm_connector_state - mutable connector state
783  * @connector: backpointer to the connector
784  * @crtc: CRTC to connect connector to, NULL if disabled
785  * @best_encoder: can be used by helpers and drivers to select the encoder
786  * @state: backpointer to global drm_atomic_state
787  */
788 struct drm_connector_state {
789         struct drm_connector *connector;
790
791         struct drm_crtc *crtc;  /* do not write directly, use drm_atomic_set_crtc_for_connector() */
792
793         struct drm_encoder *best_encoder;
794
795         struct drm_atomic_state *state;
796 };
797
798 /**
799  * struct drm_connector_funcs - control connectors on a given device
800  *
801  * Each CRTC may have one or more connectors attached to it.  The functions
802  * below allow the core DRM code to control connectors, enumerate available modes,
803  * etc.
804  */
805 struct drm_connector_funcs {
806         /**
807          * @dpms:
808          *
809          * Legacy entry point to set the per-connector DPMS state. Legacy DPMS
810          * is exposed as a standard property on the connector, but diverted to
811          * this callback in the drm core. Note that atomic drivers don't
812          * implement the 4 level DPMS support on the connector any more, but
813          * instead only have an on/off "ACTIVE" property on the CRTC object.
814          *
815          * Drivers implementing atomic modeset should use
816          * drm_atomic_helper_connector_dpms() to implement this hook.
817          *
818          * RETURNS:
819          *
820          * 0 on success or a negative error code on failure.
821          */
822         int (*dpms)(struct drm_connector *connector, int mode);
823
824         /**
825          * @reset:
826          *
827          * Reset connector hardware and software state to off. This function isn't
828          * called by the core directly, only through drm_mode_config_reset().
829          * It's not a helper hook only for historical reasons.
830          *
831          * Atomic drivers can use drm_atomic_helper_connector_reset() to reset
832          * atomic state using this hook.
833          */
834         void (*reset)(struct drm_connector *connector);
835
836         /**
837          * @detect:
838          *
839          * Check to see if anything is attached to the connector. The parameter
840          * force is set to false whilst polling, true when checking the
841          * connector due to a user request. force can be used by the driver to
842          * avoid expensive, destructive operations during automated probing.
843          *
844          * FIXME:
845          *
846          * Note that this hook is only called by the probe helper. It's not in
847          * the helper library vtable purely for historical reasons. The only DRM
848          * core entry point to probe connector state is @fill_modes.
849          *
850          * RETURNS:
851          *
852          * drm_connector_status indicating the connector's status.
853          */
854         enum drm_connector_status (*detect)(struct drm_connector *connector,
855                                             bool force);
856
857         /**
858          * @force:
859          *
860          * This function is called to update internal encoder state when the
861          * connector is forced to a certain state by userspace, either through
862          * the sysfs interfaces or on the kernel cmdline. In that case the
863          * @detect callback isn't called.
864          *
865          * FIXME:
866          *
867          * Note that this hook is only called by the probe helper. It's not in
868          * the helper library vtable purely for historical reasons. The only DRM
869          * core entry point to probe connector state is @fill_modes.
870          */
871         void (*force)(struct drm_connector *connector);
872
873         /**
874          * @fill_modes:
875          *
876          * Entry point for output detection and basic mode validation. The
877          * driver should reprobe the output if needed (e.g. when hotplug
878          * handling is unreliable), add all detected modes to connector->modes
879          * and filter out any the device can't support in any configuration. It
880          * also needs to filter out any modes wider or higher than the
881          * parameters max_width and max_height indicate.
882          *
883          * The drivers must also prune any modes no longer valid from
884          * connector->modes. Furthermore it must update connector->status and
885          * connector->edid.  If no EDID has been received for this output
886          * connector->edid must be NULL.
887          *
888          * Drivers using the probe helpers should use
889          * drm_helper_probe_single_connector_modes() or
890          * drm_helper_probe_single_connector_modes_nomerge() to implement this
891          * function.
892          *
893          * RETURNS:
894          *
895          * The number of modes detected and filled into connector->modes.
896          */
897         int (*fill_modes)(struct drm_connector *connector, uint32_t max_width, uint32_t max_height);
898
899         /**
900          * @set_property:
901          *
902          * This is the legacy entry point to update a property attached to the
903          * connector.
904          *
905          * Drivers implementing atomic modeset should use
906          * drm_atomic_helper_connector_set_property() to implement this hook.
907          *
908          * This callback is optional if the driver does not support any legacy
909          * driver-private properties.
910          *
911          * RETURNS:
912          *
913          * 0 on success or a negative error code on failure.
914          */
915         int (*set_property)(struct drm_connector *connector, struct drm_property *property,
916                              uint64_t val);
917
918         /**
919          * @destroy:
920          *
921          * Clean up connector resources. This is called at driver unload time
922          * through drm_mode_config_cleanup(). It can also be called at runtime
923          * when a connector is being hot-unplugged for drivers that support
924          * connector hotplugging (e.g. DisplayPort MST).
925          */
926         void (*destroy)(struct drm_connector *connector);
927
928         /**
929          * @atomic_duplicate_state:
930          *
931          * Duplicate the current atomic state for this connector and return it.
932          * The core and helpers gurantee that any atomic state duplicated with
933          * this hook and still owned by the caller (i.e. not transferred to the
934          * driver by calling ->atomic_commit() from struct
935          * &drm_mode_config_funcs) will be cleaned up by calling the
936          * @atomic_destroy_state hook in this structure.
937          *
938          * Atomic drivers which don't subclass struct &drm_connector_state should use
939          * drm_atomic_helper_connector_duplicate_state(). Drivers that subclass the
940          * state structure to extend it with driver-private state should use
941          * __drm_atomic_helper_connector_duplicate_state() to make sure shared state is
942          * duplicated in a consistent fashion across drivers.
943          *
944          * It is an error to call this hook before connector->state has been
945          * initialized correctly.
946          *
947          * NOTE:
948          *
949          * If the duplicate state references refcounted resources this hook must
950          * acquire a reference for each of them. The driver must release these
951          * references again in @atomic_destroy_state.
952          *
953          * RETURNS:
954          *
955          * Duplicated atomic state or NULL when the allocation failed.
956          */
957         struct drm_connector_state *(*atomic_duplicate_state)(struct drm_connector *connector);
958
959         /**
960          * @atomic_destroy_state:
961          *
962          * Destroy a state duplicated with @atomic_duplicate_state and release
963          * or unreference all resources it references
964          */
965         void (*atomic_destroy_state)(struct drm_connector *connector,
966                                      struct drm_connector_state *state);
967
968         /**
969          * @atomic_set_property:
970          *
971          * Decode a driver-private property value and store the decoded value
972          * into the passed-in state structure. Since the atomic core decodes all
973          * standardized properties (even for extensions beyond the core set of
974          * properties which might not be implemented by all drivers) this
975          * requires drivers to subclass the state structure.
976          *
977          * Such driver-private properties should really only be implemented for
978          * truly hardware/vendor specific state. Instead it is preferred to
979          * standardize atomic extension and decode the properties used to expose
980          * such an extension in the core.
981          *
982          * Do not call this function directly, use
983          * drm_atomic_connector_set_property() instead.
984          *
985          * This callback is optional if the driver does not support any
986          * driver-private atomic properties.
987          *
988          * NOTE:
989          *
990          * This function is called in the state assembly phase of atomic
991          * modesets, which can be aborted for any reason (including on
992          * userspace's request to just check whether a configuration would be
993          * possible). Drivers MUST NOT touch any persistent state (hardware or
994          * software) or data structures except the passed in @state parameter.
995          *
996          * Also since userspace controls in which order properties are set this
997          * function must not do any input validation (since the state update is
998          * incomplete and hence likely inconsistent). Instead any such input
999          * validation must be done in the various atomic_check callbacks.
1000          *
1001          * RETURNS:
1002          *
1003          * 0 if the property has been found, -EINVAL if the property isn't
1004          * implemented by the driver (which shouldn't ever happen, the core only
1005          * asks for properties attached to this connector). No other validation
1006          * is allowed by the driver. The core already checks that the property
1007          * value is within the range (integer, valid enum value, ...) the driver
1008          * set when registering the property.
1009          */
1010         int (*atomic_set_property)(struct drm_connector *connector,
1011                                    struct drm_connector_state *state,
1012                                    struct drm_property *property,
1013                                    uint64_t val);
1014
1015         /**
1016          * @atomic_get_property:
1017          *
1018          * Reads out the decoded driver-private property. This is used to
1019          * implement the GETCONNECTOR IOCTL.
1020          *
1021          * Do not call this function directly, use
1022          * drm_atomic_connector_get_property() instead.
1023          *
1024          * This callback is optional if the driver does not support any
1025          * driver-private atomic properties.
1026          *
1027          * RETURNS:
1028          *
1029          * 0 on success, -EINVAL if the property isn't implemented by the
1030          * driver (which shouldn't ever happen, the core only asks for
1031          * properties attached to this connector).
1032          */
1033         int (*atomic_get_property)(struct drm_connector *connector,
1034                                    const struct drm_connector_state *state,
1035                                    struct drm_property *property,
1036                                    uint64_t *val);
1037 };
1038
1039 /**
1040  * struct drm_encoder_funcs - encoder controls
1041  *
1042  * Encoders sit between CRTCs and connectors.
1043  */
1044 struct drm_encoder_funcs {
1045         /**
1046          * @reset:
1047          *
1048          * Reset encoder hardware and software state to off. This function isn't
1049          * called by the core directly, only through drm_mode_config_reset().
1050          * It's not a helper hook only for historical reasons.
1051          */
1052         void (*reset)(struct drm_encoder *encoder);
1053
1054         /**
1055          * @destroy:
1056          *
1057          * Clean up encoder resources. This is only called at driver unload time
1058          * through drm_mode_config_cleanup() since an encoder cannot be
1059          * hotplugged in DRM.
1060          */
1061         void (*destroy)(struct drm_encoder *encoder);
1062 };
1063
1064 #define DRM_CONNECTOR_MAX_ENCODER 3
1065
1066 /**
1067  * struct drm_encoder - central DRM encoder structure
1068  * @dev: parent DRM device
1069  * @head: list management
1070  * @base: base KMS object
1071  * @name: encoder name
1072  * @encoder_type: one of the %DRM_MODE_ENCODER_<foo> types in drm_mode.h
1073  * @possible_crtcs: bitmask of potential CRTC bindings
1074  * @possible_clones: bitmask of potential sibling encoders for cloning
1075  * @crtc: currently bound CRTC
1076  * @bridge: bridge associated to the encoder
1077  * @funcs: control functions
1078  * @helper_private: mid-layer private data
1079  *
1080  * CRTCs drive pixels to encoders, which convert them into signals
1081  * appropriate for a given connector or set of connectors.
1082  */
1083 struct drm_encoder {
1084         struct drm_device *dev;
1085         struct list_head head;
1086
1087         struct drm_mode_object base;
1088         char *name;
1089         int encoder_type;
1090         uint32_t possible_crtcs;
1091         uint32_t possible_clones;
1092
1093         struct drm_crtc *crtc;
1094         struct drm_bridge *bridge;
1095         const struct drm_encoder_funcs *funcs;
1096         const struct drm_encoder_helper_funcs *helper_private;
1097 };
1098
1099 /* should we poll this connector for connects and disconnects */
1100 /* hot plug detectable */
1101 #define DRM_CONNECTOR_POLL_HPD (1 << 0)
1102 /* poll for connections */
1103 #define DRM_CONNECTOR_POLL_CONNECT (1 << 1)
1104 /* can cleanly poll for disconnections without flickering the screen */
1105 /* DACs should rarely do this without a lot of testing */
1106 #define DRM_CONNECTOR_POLL_DISCONNECT (1 << 2)
1107
1108 #define MAX_ELD_BYTES   128
1109
1110 /**
1111  * struct drm_connector - central DRM connector control structure
1112  * @dev: parent DRM device
1113  * @kdev: kernel device for sysfs attributes
1114  * @attr: sysfs attributes
1115  * @head: list management
1116  * @base: base KMS object
1117  * @name: connector name
1118  * @connector_type: one of the %DRM_MODE_CONNECTOR_<foo> types from drm_mode.h
1119  * @connector_type_id: index into connector type enum
1120  * @interlace_allowed: can this connector handle interlaced modes?
1121  * @doublescan_allowed: can this connector handle doublescan?
1122  * @stereo_allowed: can this connector handle stereo modes?
1123  * @modes: modes available on this connector (from fill_modes() + user)
1124  * @status: one of the drm_connector_status enums (connected, not, or unknown)
1125  * @probed_modes: list of modes derived directly from the display
1126  * @display_info: information about attached display (e.g. from EDID)
1127  * @funcs: connector control functions
1128  * @edid_blob_ptr: DRM property containing EDID if present
1129  * @properties: property tracking for this connector
1130  * @path_blob_ptr: DRM blob property data for the DP MST path property
1131  * @polled: a %DRM_CONNECTOR_POLL_<foo> value for core driven polling
1132  * @dpms: current dpms state
1133  * @helper_private: mid-layer private data
1134  * @cmdline_mode: mode line parsed from the kernel cmdline for this connector
1135  * @force: a %DRM_FORCE_<foo> state for forced mode sets
1136  * @override_edid: has the EDID been overwritten through debugfs for testing?
1137  * @encoder_ids: valid encoders for this connector
1138  * @encoder: encoder driving this connector, if any
1139  * @eld: EDID-like data, if present
1140  * @dvi_dual: dual link DVI, if found
1141  * @max_tmds_clock: max clock rate, if found
1142  * @latency_present: AV delay info from ELD, if found
1143  * @video_latency: video latency info from ELD, if found
1144  * @audio_latency: audio latency info from ELD, if found
1145  * @null_edid_counter: track sinks that give us all zeros for the EDID
1146  * @bad_edid_counter: track sinks that give us an EDID with invalid checksum
1147  * @edid_corrupt: indicates whether the last read EDID was corrupt
1148  * @debugfs_entry: debugfs directory for this connector
1149  * @state: current atomic state for this connector
1150  * @has_tile: is this connector connected to a tiled monitor
1151  * @tile_group: tile group for the connected monitor
1152  * @tile_is_single_monitor: whether the tile is one monitor housing
1153  * @num_h_tile: number of horizontal tiles in the tile group
1154  * @num_v_tile: number of vertical tiles in the tile group
1155  * @tile_h_loc: horizontal location of this tile
1156  * @tile_v_loc: vertical location of this tile
1157  * @tile_h_size: horizontal size of this tile.
1158  * @tile_v_size: vertical size of this tile.
1159  *
1160  * Each connector may be connected to one or more CRTCs, or may be clonable by
1161  * another connector if they can share a CRTC.  Each connector also has a specific
1162  * position in the broader display (referred to as a 'screen' though it could
1163  * span multiple monitors).
1164  */
1165 struct drm_connector {
1166         struct drm_device *dev;
1167         struct device *kdev;
1168         struct device_attribute *attr;
1169         struct list_head head;
1170
1171         struct drm_mode_object base;
1172
1173         char *name;
1174         int connector_id;
1175         int connector_type;
1176         int connector_type_id;
1177         bool interlace_allowed;
1178         bool doublescan_allowed;
1179         bool stereo_allowed;
1180         struct list_head modes; /* list of modes on this connector */
1181
1182         enum drm_connector_status status;
1183
1184         /* these are modes added by probing with DDC or the BIOS */
1185         struct list_head probed_modes;
1186
1187         struct drm_display_info display_info;
1188         const struct drm_connector_funcs *funcs;
1189
1190         struct drm_property_blob *edid_blob_ptr;
1191         struct drm_object_properties properties;
1192
1193         struct drm_property_blob *path_blob_ptr;
1194
1195         struct drm_property_blob *tile_blob_ptr;
1196
1197         uint8_t polled; /* DRM_CONNECTOR_POLL_* */
1198
1199         /* requested DPMS state */
1200         int dpms;
1201
1202         const struct drm_connector_helper_funcs *helper_private;
1203
1204         /* forced on connector */
1205         struct drm_cmdline_mode cmdline_mode;
1206         enum drm_connector_force force;
1207         bool override_edid;
1208         uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
1209         struct drm_encoder *encoder; /* currently active encoder */
1210
1211         /* EDID bits */
1212         uint8_t eld[MAX_ELD_BYTES];
1213         bool dvi_dual;
1214         int max_tmds_clock;     /* in MHz */
1215         bool latency_present[2];
1216         int video_latency[2];   /* [0]: progressive, [1]: interlaced */
1217         int audio_latency[2];
1218         int null_edid_counter; /* needed to workaround some HW bugs where we get all 0s */
1219         unsigned bad_edid_counter;
1220
1221         /* Flag for raw EDID header corruption - used in Displayport
1222          * compliance testing - * Displayport Link CTS Core 1.2 rev1.1 4.2.2.6
1223          */
1224         bool edid_corrupt;
1225
1226         struct dentry *debugfs_entry;
1227
1228         struct drm_connector_state *state;
1229
1230         /* DisplayID bits */
1231         bool has_tile;
1232         struct drm_tile_group *tile_group;
1233         bool tile_is_single_monitor;
1234
1235         uint8_t num_h_tile, num_v_tile;
1236         uint8_t tile_h_loc, tile_v_loc;
1237         uint16_t tile_h_size, tile_v_size;
1238 };
1239
1240 /**
1241  * struct drm_plane_state - mutable plane state
1242  * @plane: backpointer to the plane
1243  * @crtc: currently bound CRTC, NULL if disabled
1244  * @fb: currently bound framebuffer
1245  * @fence: optional fence to wait for before scanning out @fb
1246  * @crtc_x: left position of visible portion of plane on crtc
1247  * @crtc_y: upper position of visible portion of plane on crtc
1248  * @crtc_w: width of visible portion of plane on crtc
1249  * @crtc_h: height of visible portion of plane on crtc
1250  * @src_x: left position of visible portion of plane within
1251  *      plane (in 16.16)
1252  * @src_y: upper position of visible portion of plane within
1253  *      plane (in 16.16)
1254  * @src_w: width of visible portion of plane (in 16.16)
1255  * @src_h: height of visible portion of plane (in 16.16)
1256  * @state: backpointer to global drm_atomic_state
1257  */
1258 struct drm_plane_state {
1259         struct drm_plane *plane;
1260
1261         struct drm_crtc *crtc;   /* do not write directly, use drm_atomic_set_crtc_for_plane() */
1262         struct drm_framebuffer *fb;  /* do not write directly, use drm_atomic_set_fb_for_plane() */
1263         struct fence *fence;
1264
1265         /* Signed dest location allows it to be partially off screen */
1266         int32_t crtc_x, crtc_y;
1267         uint32_t crtc_w, crtc_h;
1268
1269         /* Source values are 16.16 fixed point */
1270         uint32_t src_x, src_y;
1271         uint32_t src_h, src_w;
1272
1273         /* Plane rotation */
1274         unsigned int rotation;
1275
1276         struct drm_atomic_state *state;
1277 };
1278
1279
1280 /**
1281  * struct drm_plane_funcs - driver plane control functions
1282  */
1283 struct drm_plane_funcs {
1284         /**
1285          * @update_plane:
1286          *
1287          * This is the legacy entry point to enable and configure the plane for
1288          * the given CRTC and framebuffer. It is never called to disable the
1289          * plane, i.e. the passed-in crtc and fb paramters are never NULL.
1290          *
1291          * The source rectangle in frame buffer memory coordinates is given by
1292          * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
1293          * values). Devices that don't support subpixel plane coordinates can
1294          * ignore the fractional part.
1295          *
1296          * The destination rectangle in CRTC coordinates is given by the
1297          * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
1298          * Devices scale the source rectangle to the destination rectangle. If
1299          * scaling is not supported, and the source rectangle size doesn't match
1300          * the destination rectangle size, the driver must return a
1301          * -<errorname>EINVAL</errorname> error.
1302          *
1303          * Drivers implementing atomic modeset should use
1304          * drm_atomic_helper_update_plane() to implement this hook.
1305          *
1306          * RETURNS:
1307          *
1308          * 0 on success or a negative error code on failure.
1309          */
1310         int (*update_plane)(struct drm_plane *plane,
1311                             struct drm_crtc *crtc, struct drm_framebuffer *fb,
1312                             int crtc_x, int crtc_y,
1313                             unsigned int crtc_w, unsigned int crtc_h,
1314                             uint32_t src_x, uint32_t src_y,
1315                             uint32_t src_w, uint32_t src_h);
1316
1317         /**
1318          * @disable_plane:
1319          *
1320          * This is the legacy entry point to disable the plane. The DRM core
1321          * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
1322          * with the frame buffer ID set to 0.  Disabled planes must not be
1323          * processed by the CRTC.
1324          *
1325          * Drivers implementing atomic modeset should use
1326          * drm_atomic_helper_disable_plane() to implement this hook.
1327          *
1328          * RETURNS:
1329          *
1330          * 0 on success or a negative error code on failure.
1331          */
1332         int (*disable_plane)(struct drm_plane *plane);
1333
1334         /**
1335          * @destroy:
1336          *
1337          * Clean up plane resources. This is only called at driver unload time
1338          * through drm_mode_config_cleanup() since a plane cannot be hotplugged
1339          * in DRM.
1340          */
1341         void (*destroy)(struct drm_plane *plane);
1342
1343         /**
1344          * @reset:
1345          *
1346          * Reset plane hardware and software state to off. This function isn't
1347          * called by the core directly, only through drm_mode_config_reset().
1348          * It's not a helper hook only for historical reasons.
1349          *
1350          * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
1351          * atomic state using this hook.
1352          */
1353         void (*reset)(struct drm_plane *plane);
1354
1355         /**
1356          * @set_property:
1357          *
1358          * This is the legacy entry point to update a property attached to the
1359          * plane.
1360          *
1361          * Drivers implementing atomic modeset should use
1362          * drm_atomic_helper_plane_set_property() to implement this hook.
1363          *
1364          * This callback is optional if the driver does not support any legacy
1365          * driver-private properties.
1366          *
1367          * RETURNS:
1368          *
1369          * 0 on success or a negative error code on failure.
1370          */
1371         int (*set_property)(struct drm_plane *plane,
1372                             struct drm_property *property, uint64_t val);
1373
1374         /**
1375          * @atomic_duplicate_state:
1376          *
1377          * Duplicate the current atomic state for this plane and return it.
1378          * The core and helpers gurantee that any atomic state duplicated with
1379          * this hook and still owned by the caller (i.e. not transferred to the
1380          * driver by calling ->atomic_commit() from struct
1381          * &drm_mode_config_funcs) will be cleaned up by calling the
1382          * @atomic_destroy_state hook in this structure.
1383          *
1384          * Atomic drivers which don't subclass struct &drm_plane_state should use
1385          * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
1386          * state structure to extend it with driver-private state should use
1387          * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
1388          * duplicated in a consistent fashion across drivers.
1389          *
1390          * It is an error to call this hook before plane->state has been
1391          * initialized correctly.
1392          *
1393          * NOTE:
1394          *
1395          * If the duplicate state references refcounted resources this hook must
1396          * acquire a reference for each of them. The driver must release these
1397          * references again in @atomic_destroy_state.
1398          *
1399          * RETURNS:
1400          *
1401          * Duplicated atomic state or NULL when the allocation failed.
1402          */
1403         struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
1404
1405         /**
1406          * @atomic_destroy_state:
1407          *
1408          * Destroy a state duplicated with @atomic_duplicate_state and release
1409          * or unreference all resources it references
1410          */
1411         void (*atomic_destroy_state)(struct drm_plane *plane,
1412                                      struct drm_plane_state *state);
1413
1414         /**
1415          * @atomic_set_property:
1416          *
1417          * Decode a driver-private property value and store the decoded value
1418          * into the passed-in state structure. Since the atomic core decodes all
1419          * standardized properties (even for extensions beyond the core set of
1420          * properties which might not be implemented by all drivers) this
1421          * requires drivers to subclass the state structure.
1422          *
1423          * Such driver-private properties should really only be implemented for
1424          * truly hardware/vendor specific state. Instead it is preferred to
1425          * standardize atomic extension and decode the properties used to expose
1426          * such an extension in the core.
1427          *
1428          * Do not call this function directly, use
1429          * drm_atomic_plane_set_property() instead.
1430          *
1431          * This callback is optional if the driver does not support any
1432          * driver-private atomic properties.
1433          *
1434          * NOTE:
1435          *
1436          * This function is called in the state assembly phase of atomic
1437          * modesets, which can be aborted for any reason (including on
1438          * userspace's request to just check whether a configuration would be
1439          * possible). Drivers MUST NOT touch any persistent state (hardware or
1440          * software) or data structures except the passed in @state parameter.
1441          *
1442          * Also since userspace controls in which order properties are set this
1443          * function must not do any input validation (since the state update is
1444          * incomplete and hence likely inconsistent). Instead any such input
1445          * validation must be done in the various atomic_check callbacks.
1446          *
1447          * RETURNS:
1448          *
1449          * 0 if the property has been found, -EINVAL if the property isn't
1450          * implemented by the driver (which shouldn't ever happen, the core only
1451          * asks for properties attached to this plane). No other validation is
1452          * allowed by the driver. The core already checks that the property
1453          * value is within the range (integer, valid enum value, ...) the driver
1454          * set when registering the property.
1455          */
1456         int (*atomic_set_property)(struct drm_plane *plane,
1457                                    struct drm_plane_state *state,
1458                                    struct drm_property *property,
1459                                    uint64_t val);
1460
1461         /**
1462          * @atomic_get_property:
1463          *
1464          * Reads out the decoded driver-private property. This is used to
1465          * implement the GETPLANE IOCTL.
1466          *
1467          * Do not call this function directly, use
1468          * drm_atomic_plane_get_property() instead.
1469          *
1470          * This callback is optional if the driver does not support any
1471          * driver-private atomic properties.
1472          *
1473          * RETURNS:
1474          *
1475          * 0 on success, -EINVAL if the property isn't implemented by the
1476          * driver (which should never happen, the core only asks for
1477          * properties attached to this plane).
1478          */
1479         int (*atomic_get_property)(struct drm_plane *plane,
1480                                    const struct drm_plane_state *state,
1481                                    struct drm_property *property,
1482                                    uint64_t *val);
1483 };
1484
1485 enum drm_plane_type {
1486         DRM_PLANE_TYPE_OVERLAY,
1487         DRM_PLANE_TYPE_PRIMARY,
1488         DRM_PLANE_TYPE_CURSOR,
1489 };
1490
1491
1492 /**
1493  * struct drm_plane - central DRM plane control structure
1494  * @dev: DRM device this plane belongs to
1495  * @head: for list management
1496  * @base: base mode object
1497  * @possible_crtcs: pipes this plane can be bound to
1498  * @format_types: array of formats supported by this plane
1499  * @format_count: number of formats supported
1500  * @format_default: driver hasn't supplied supported formats for the plane
1501  * @crtc: currently bound CRTC
1502  * @fb: currently bound fb
1503  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
1504  *      drm_mode_set_config_internal() to implement correct refcounting.
1505  * @funcs: helper functions
1506  * @properties: property tracking for this plane
1507  * @type: type of plane (overlay, primary, cursor)
1508  * @state: current atomic state for this plane
1509  */
1510 struct drm_plane {
1511         struct drm_device *dev;
1512         struct list_head head;
1513
1514         char *name;
1515
1516         struct drm_modeset_lock mutex;
1517
1518         struct drm_mode_object base;
1519
1520         uint32_t possible_crtcs;
1521         uint32_t *format_types;
1522         unsigned int format_count;
1523         bool format_default;
1524
1525         struct drm_crtc *crtc;
1526         struct drm_framebuffer *fb;
1527
1528         struct drm_framebuffer *old_fb;
1529
1530         const struct drm_plane_funcs *funcs;
1531
1532         struct drm_object_properties properties;
1533
1534         enum drm_plane_type type;
1535
1536         const struct drm_plane_helper_funcs *helper_private;
1537
1538         struct drm_plane_state *state;
1539 };
1540
1541 /**
1542  * struct drm_bridge_funcs - drm_bridge control functions
1543  * @attach: Called during drm_bridge_attach
1544  */
1545 struct drm_bridge_funcs {
1546         int (*attach)(struct drm_bridge *bridge);
1547
1548         /**
1549          * @mode_fixup:
1550          *
1551          * This callback is used to validate and adjust a mode. The paramater
1552          * mode is the display mode that should be fed to the next element in
1553          * the display chain, either the final &drm_connector or the next
1554          * &drm_bridge. The parameter adjusted_mode is the input mode the bridge
1555          * requires. It can be modified by this callback and does not need to
1556          * match mode.
1557          *
1558          * This is the only hook that allows a bridge to reject a modeset. If
1559          * this function passes all other callbacks must succeed for this
1560          * configuration.
1561          *
1562          * NOTE:
1563          *
1564          * This function is called in the check phase of atomic modesets, which
1565          * can be aborted for any reason (including on userspace's request to
1566          * just check whether a configuration would be possible). Drivers MUST
1567          * NOT touch any persistent state (hardware or software) or data
1568          * structures except the passed in @state parameter.
1569          *
1570          * RETURNS:
1571          *
1572          * True if an acceptable configuration is possible, false if the modeset
1573          * operation should be rejected.
1574          */
1575         bool (*mode_fixup)(struct drm_bridge *bridge,
1576                            const struct drm_display_mode *mode,
1577                            struct drm_display_mode *adjusted_mode);
1578         /**
1579          * @disable:
1580          *
1581          * This callback should disable the bridge. It is called right before
1582          * the preceding element in the display pipe is disabled. If the
1583          * preceding element is a bridge this means it's called before that
1584          * bridge's ->disable() function. If the preceding element is a
1585          * &drm_encoder it's called right before the encoder's ->disable(),
1586          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1587          *
1588          * The bridge can assume that the display pipe (i.e. clocks and timing
1589          * signals) feeding it is still running when this callback is called.
1590          *
1591          * The disable callback is optional.
1592          */
1593         void (*disable)(struct drm_bridge *bridge);
1594
1595         /**
1596          * @post_disable:
1597          *
1598          * This callback should disable the bridge. It is called right after
1599          * the preceding element in the display pipe is disabled. If the
1600          * preceding element is a bridge this means it's called after that
1601          * bridge's ->post_disable() function. If the preceding element is a
1602          * &drm_encoder it's called right after the encoder's ->disable(),
1603          * ->prepare() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1604          *
1605          * The bridge must assume that the display pipe (i.e. clocks and timing
1606          * singals) feeding it is no longer running when this callback is
1607          * called.
1608          *
1609          * The post_disable callback is optional.
1610          */
1611         void (*post_disable)(struct drm_bridge *bridge);
1612
1613         /**
1614          * @mode_set:
1615          *
1616          * This callback should set the given mode on the bridge. It is called
1617          * after the ->mode_set() callback for the preceding element in the
1618          * display pipeline has been called already. The display pipe (i.e.
1619          * clocks and timing signals) is off when this function is called.
1620          */
1621         void (*mode_set)(struct drm_bridge *bridge,
1622                          struct drm_display_mode *mode,
1623                          struct drm_display_mode *adjusted_mode);
1624         /**
1625          * @pre_enable:
1626          *
1627          * This callback should enable the bridge. It is called right before
1628          * the preceding element in the display pipe is enabled. If the
1629          * preceding element is a bridge this means it's called before that
1630          * bridge's ->pre_enable() function. If the preceding element is a
1631          * &drm_encoder it's called right before the encoder's ->enable(),
1632          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1633          *
1634          * The display pipe (i.e. clocks and timing signals) feeding this bridge
1635          * will not yet be running when this callback is called. The bridge must
1636          * not enable the display link feeding the next bridge in the chain (if
1637          * there is one) when this callback is called.
1638          *
1639          * The pre_enable callback is optional.
1640          */
1641         void (*pre_enable)(struct drm_bridge *bridge);
1642
1643         /**
1644          * @enable:
1645          *
1646          * This callback should enable the bridge. It is called right after
1647          * the preceding element in the display pipe is enabled. If the
1648          * preceding element is a bridge this means it's called after that
1649          * bridge's ->enable() function. If the preceding element is a
1650          * &drm_encoder it's called right after the encoder's ->enable(),
1651          * ->commit() or ->dpms() hook from struct &drm_encoder_helper_funcs.
1652          *
1653          * The bridge can assume that the display pipe (i.e. clocks and timing
1654          * signals) feeding it is running when this callback is called. This
1655          * callback must enable the display link feeding the next bridge in the
1656          * chain if there is one.
1657          *
1658          * The enable callback is optional.
1659          */
1660         void (*enable)(struct drm_bridge *bridge);
1661 };
1662
1663 /**
1664  * struct drm_bridge - central DRM bridge control structure
1665  * @dev: DRM device this bridge belongs to
1666  * @encoder: encoder to which this bridge is connected
1667  * @next: the next bridge in the encoder chain
1668  * @of_node: device node pointer to the bridge
1669  * @list: to keep track of all added bridges
1670  * @funcs: control functions
1671  * @driver_private: pointer to the bridge driver's internal context
1672  */
1673 struct drm_bridge {
1674         struct drm_device *dev;
1675         struct drm_encoder *encoder;
1676         struct drm_bridge *next;
1677 #ifdef CONFIG_OF
1678         struct device_node *of_node;
1679 #endif
1680         struct list_head list;
1681
1682         const struct drm_bridge_funcs *funcs;
1683         void *driver_private;
1684 };
1685
1686 /**
1687  * struct drm_atomic_state - the global state object for atomic updates
1688  * @dev: parent DRM device
1689  * @allow_modeset: allow full modeset
1690  * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
1691  * @legacy_set_config: Disable conflicting encoders instead of failing with -EINVAL.
1692  * @planes: pointer to array of plane pointers
1693  * @plane_states: pointer to array of plane states pointers
1694  * @crtcs: pointer to array of CRTC pointers
1695  * @crtc_states: pointer to array of CRTC states pointers
1696  * @num_connector: size of the @connectors and @connector_states arrays
1697  * @connectors: pointer to array of connector pointers
1698  * @connector_states: pointer to array of connector states pointers
1699  * @acquire_ctx: acquire context for this atomic modeset state update
1700  */
1701 struct drm_atomic_state {
1702         struct drm_device *dev;
1703         bool allow_modeset : 1;
1704         bool legacy_cursor_update : 1;
1705         bool legacy_set_config : 1;
1706         struct drm_plane **planes;
1707         struct drm_plane_state **plane_states;
1708         struct drm_crtc **crtcs;
1709         struct drm_crtc_state **crtc_states;
1710         int num_connector;
1711         struct drm_connector **connectors;
1712         struct drm_connector_state **connector_states;
1713
1714         struct drm_modeset_acquire_ctx *acquire_ctx;
1715 };
1716
1717
1718 /**
1719  * struct drm_mode_set - new values for a CRTC config change
1720  * @fb: framebuffer to use for new config
1721  * @crtc: CRTC whose configuration we're about to change
1722  * @mode: mode timings to use
1723  * @x: position of this CRTC relative to @fb
1724  * @y: position of this CRTC relative to @fb
1725  * @connectors: array of connectors to drive with this CRTC if possible
1726  * @num_connectors: size of @connectors array
1727  *
1728  * Represents a single crtc the connectors that it drives with what mode
1729  * and from which framebuffer it scans out from.
1730  *
1731  * This is used to set modes.
1732  */
1733 struct drm_mode_set {
1734         struct drm_framebuffer *fb;
1735         struct drm_crtc *crtc;
1736         struct drm_display_mode *mode;
1737
1738         uint32_t x;
1739         uint32_t y;
1740
1741         struct drm_connector **connectors;
1742         size_t num_connectors;
1743 };
1744
1745 /**
1746  * struct drm_mode_config_funcs - basic driver provided mode setting functions
1747  *
1748  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
1749  * involve drivers.
1750  */
1751 struct drm_mode_config_funcs {
1752         /**
1753          * @fb_create:
1754          *
1755          * Create a new framebuffer object. The core does basic checks on the
1756          * requested metadata, but most of that is left to the driver. See
1757          * struct &drm_mode_fb_cmd2 for details.
1758          *
1759          * If the parameters are deemed valid and the backing storage objects in
1760          * the underlying memory manager all exist, then the driver allocates
1761          * a new &drm_framebuffer structure, subclassed to contain
1762          * driver-specific information (like the internal native buffer object
1763          * references). It also needs to fill out all relevant metadata, which
1764          * should be done by calling drm_helper_mode_fill_fb_struct().
1765          *
1766          * The initialization is finalized by calling drm_framebuffer_init(),
1767          * which registers the framebuffer and makes it accessible to other
1768          * threads.
1769          *
1770          * RETURNS:
1771          *
1772          * A new framebuffer with an initial reference count of 1 or a negative
1773          * error code encoded with ERR_PTR().
1774          */
1775         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
1776                                              struct drm_file *file_priv,
1777                                              const struct drm_mode_fb_cmd2 *mode_cmd);
1778
1779         /**
1780          * @output_poll_changed:
1781          *
1782          * Callback used by helpers to inform the driver of output configuration
1783          * changes.
1784          *
1785          * Drivers implementing fbdev emulation with the helpers can call
1786          * drm_fb_helper_hotplug_changed from this hook to inform the fbdev
1787          * helper of output changes.
1788          *
1789          * FIXME:
1790          *
1791          * Except that there's no vtable for device-level helper callbacks
1792          * there's no reason this is a core function.
1793          */
1794         void (*output_poll_changed)(struct drm_device *dev);
1795
1796         /**
1797          * @atomic_check:
1798          *
1799          * This is the only hook to validate an atomic modeset update. This
1800          * function must reject any modeset and state changes which the hardware
1801          * or driver doesn't support. This includes but is of course not limited
1802          * to:
1803          *
1804          *  - Checking that the modes, framebuffers, scaling and placement
1805          *    requirements and so on are within the limits of the hardware.
1806          *
1807          *  - Checking that any hidden shared resources are not oversubscribed.
1808          *    This can be shared PLLs, shared lanes, overall memory bandwidth,
1809          *    display fifo space (where shared between planes or maybe even
1810          *    CRTCs).
1811          *
1812          *  - Checking that virtualized resources exported to userspace are not
1813          *    oversubscribed. For various reasons it can make sense to expose
1814          *    more planes, crtcs or encoders than which are physically there. One
1815          *    example is dual-pipe operations (which generally should be hidden
1816          *    from userspace if when lockstepped in hardware, exposed otherwise),
1817          *    where a plane might need 1 hardware plane (if it's just on one
1818          *    pipe), 2 hardware planes (when it spans both pipes) or maybe even
1819          *    shared a hardware plane with a 2nd plane (if there's a compatible
1820          *    plane requested on the area handled by the other pipe).
1821          *
1822          *  - Check that any transitional state is possible and that if
1823          *    requested, the update can indeed be done in the vblank period
1824          *    without temporarily disabling some functions.
1825          *
1826          *  - Check any other constraints the driver or hardware might have.
1827          *
1828          *  - This callback also needs to correctly fill out the &drm_crtc_state
1829          *    in this update to make sure that drm_atomic_crtc_needs_modeset()
1830          *    reflects the nature of the possible update and returns true if and
1831          *    only if the update cannot be applied without tearing within one
1832          *    vblank on that CRTC. The core uses that information to reject
1833          *    updates which require a full modeset (i.e. blanking the screen, or
1834          *    at least pausing updates for a substantial amount of time) if
1835          *    userspace has disallowed that in its request.
1836          *
1837          *  - The driver also does not need to repeat basic input validation
1838          *    like done for the corresponding legacy entry points. The core does
1839          *    that before calling this hook.
1840          *
1841          * See the documentation of @atomic_commit for an exhaustive list of
1842          * error conditions which don't have to be checked at the
1843          * ->atomic_check() stage?
1844          *
1845          * See the documentation for struct &drm_atomic_state for how exactly
1846          * an atomic modeset update is described.
1847          *
1848          * Drivers using the atomic helpers can implement this hook using
1849          * drm_atomic_helper_check(), or one of the exported sub-functions of
1850          * it.
1851          *
1852          * RETURNS:
1853          *
1854          * 0 on success or one of the below negative error codes:
1855          *
1856          *  - -EINVAL, if any of the above constraints are violated.
1857          *
1858          *  - -EDEADLK, when returned from an attempt to acquire an additional
1859          *    &drm_modeset_lock through drm_modeset_lock().
1860          *
1861          *  - -ENOMEM, if allocating additional state sub-structures failed due
1862          *    to lack of memory.
1863          *
1864          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1865          *    This can either be due to a pending signal, or because the driver
1866          *    needs to completely bail out to recover from an exceptional
1867          *    situation like a GPU hang. From a userspace point all errors are
1868          *    treated equally.
1869          */
1870         int (*atomic_check)(struct drm_device *dev,
1871                             struct drm_atomic_state *state);
1872
1873         /**
1874          * @atomic_commit:
1875          *
1876          * This is the only hook to commit an atomic modeset update. The core
1877          * guarantees that @atomic_check has been called successfully before
1878          * calling this function, and that nothing has been changed in the
1879          * interim.
1880          *
1881          * See the documentation for struct &drm_atomic_state for how exactly
1882          * an atomic modeset update is described.
1883          *
1884          * Drivers using the atomic helpers can implement this hook using
1885          * drm_atomic_helper_commit(), or one of the exported sub-functions of
1886          * it.
1887          *
1888          * Asynchronous commits (as indicated with the async parameter) must
1889          * do any preparatory work which might result in an unsuccessful commit
1890          * in the context of this callback. The only exceptions are hardware
1891          * errors resulting in -EIO. But even in that case the driver must
1892          * ensure that the display pipe is at least running, to avoid
1893          * compositors crashing when pageflips don't work. Anything else,
1894          * specifically committing the update to the hardware, should be done
1895          * without blocking the caller. For updates which do not require a
1896          * modeset this must be guaranteed.
1897          *
1898          * The driver must wait for any pending rendering to the new
1899          * framebuffers to complete before executing the flip. It should also
1900          * wait for any pending rendering from other drivers if the underlying
1901          * buffer is a shared dma-buf. Asynchronous commits must not wait for
1902          * rendering in the context of this callback.
1903          *
1904          * An application can request to be notified when the atomic commit has
1905          * completed. These events are per-CRTC and can be distinguished by the
1906          * CRTC index supplied in &drm_event to userspace.
1907          *
1908          * The drm core will supply a struct &drm_event in the event
1909          * member of each CRTC's &drm_crtc_state structure. This can be handled by the
1910          * drm_crtc_send_vblank_event() function, which the driver should call on
1911          * the provided event upon completion of the atomic commit. Note that if
1912          * the driver supports vblank signalling and timestamping the vblank
1913          * counters and timestamps must agree with the ones returned from page
1914          * flip events. With the current vblank helper infrastructure this can
1915          * be achieved by holding a vblank reference while the page flip is
1916          * pending, acquired through drm_crtc_vblank_get() and released with
1917          * drm_crtc_vblank_put(). Drivers are free to implement their own vblank
1918          * counter and timestamp tracking though, e.g. if they have accurate
1919          * timestamp registers in hardware.
1920          *
1921          * NOTE:
1922          *
1923          * Drivers are not allowed to shut down any display pipe successfully
1924          * enabled through an atomic commit on their own. Doing so can result in
1925          * compositors crashing if a page flip is suddenly rejected because the
1926          * pipe is off.
1927          *
1928          * RETURNS:
1929          *
1930          * 0 on success or one of the below negative error codes:
1931          *
1932          *  - -EBUSY, if an asynchronous updated is requested and there is
1933          *    an earlier updated pending. Drivers are allowed to support a queue
1934          *    of outstanding updates, but currently no driver supports that.
1935          *    Note that drivers must wait for preceding updates to complete if a
1936          *    synchronous update is requested, they are not allowed to fail the
1937          *    commit in that case.
1938          *
1939          *  - -ENOMEM, if the driver failed to allocate memory. Specifically
1940          *    this can happen when trying to pin framebuffers, which must only
1941          *    be done when committing the state.
1942          *
1943          *  - -ENOSPC, as a refinement of the more generic -ENOMEM to indicate
1944          *    that the driver has run out of vram, iommu space or similar GPU
1945          *    address space needed for framebuffer.
1946          *
1947          *  - -EIO, if the hardware completely died.
1948          *
1949          *  - -EINTR, -EAGAIN or -ERESTARTSYS, if the IOCTL should be restarted.
1950          *    This can either be due to a pending signal, or because the driver
1951          *    needs to completely bail out to recover from an exceptional
1952          *    situation like a GPU hang. From a userspace point of view all errors are
1953          *    treated equally.
1954          *
1955          * This list is exhaustive. Specifically this hook is not allowed to
1956          * return -EINVAL (any invalid requests should be caught in
1957          * @atomic_check) or -EDEADLK (this function must not acquire
1958          * additional modeset locks).
1959          */
1960         int (*atomic_commit)(struct drm_device *dev,
1961                              struct drm_atomic_state *state,
1962                              bool async);
1963
1964         /**
1965          * @atomic_state_alloc:
1966          *
1967          * This optional hook can be used by drivers that want to subclass struct
1968          * &drm_atomic_state to be able to track their own driver-private global
1969          * state easily. If this hook is implemented, drivers must also
1970          * implement @atomic_state_clear and @atomic_state_free.
1971          *
1972          * RETURNS:
1973          *
1974          * A new &drm_atomic_state on success or NULL on failure.
1975          */
1976         struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev);
1977
1978         /**
1979          * @atomic_state_clear:
1980          *
1981          * This hook must clear any driver private state duplicated into the
1982          * passed-in &drm_atomic_state. This hook is called when the caller
1983          * encountered a &drm_modeset_lock deadlock and needs to drop all
1984          * already acquired locks as part of the deadlock avoidance dance
1985          * implemented in drm_modeset_lock_backoff().
1986          *
1987          * Any duplicated state must be invalidated since a concurrent atomic
1988          * update might change it, and the drm atomic interfaces always apply
1989          * updates as relative changes to the current state.
1990          *
1991          * Drivers that implement this must call drm_atomic_state_default_clear()
1992          * to clear common state.
1993          */
1994         void (*atomic_state_clear)(struct drm_atomic_state *state);
1995
1996         /**
1997          * @atomic_state_free:
1998          *
1999          * This hook needs driver private resources and the &drm_atomic_state
2000          * itself. Note that the core first calls drm_atomic_state_clear() to
2001          * avoid code duplicate between the clear and free hooks.
2002          *
2003          * Drivers that implement this must call drm_atomic_state_default_free()
2004          * to release common resources.
2005          */
2006         void (*atomic_state_free)(struct drm_atomic_state *state);
2007 };
2008
2009 /**
2010  * struct drm_mode_config - Mode configuration control structure
2011  * @mutex: mutex protecting KMS related lists and structures
2012  * @connection_mutex: ww mutex protecting connector state and routing
2013  * @acquire_ctx: global implicit acquire context used by atomic drivers for
2014  *      legacy IOCTLs
2015  * @idr_mutex: mutex for KMS ID allocation and management
2016  * @crtc_idr: main KMS ID tracking object
2017  * @fb_lock: mutex to protect fb state and lists
2018  * @num_fb: number of fbs available
2019  * @fb_list: list of framebuffers available
2020  * @num_connector: number of connectors on this device
2021  * @connector_list: list of connector objects
2022  * @num_encoder: number of encoders on this device
2023  * @encoder_list: list of encoder objects
2024  * @num_overlay_plane: number of overlay planes on this device
2025  * @num_total_plane: number of universal (i.e. with primary/curso) planes on this device
2026  * @plane_list: list of plane objects
2027  * @num_crtc: number of CRTCs on this device
2028  * @crtc_list: list of CRTC objects
2029  * @property_list: list of property objects
2030  * @min_width: minimum pixel width on this device
2031  * @min_height: minimum pixel height on this device
2032  * @max_width: maximum pixel width on this device
2033  * @max_height: maximum pixel height on this device
2034  * @funcs: core driver provided mode setting functions
2035  * @fb_base: base address of the framebuffer
2036  * @poll_enabled: track polling support for this device
2037  * @poll_running: track polling status for this device
2038  * @output_poll_work: delayed work for polling in process context
2039  * @property_blob_list: list of all the blob property objects
2040  * @blob_lock: mutex for blob property allocation and management
2041  * @*_property: core property tracking
2042  * @degamma_lut_property: LUT used to convert the framebuffer's colors to linear
2043  *      gamma
2044  * @degamma_lut_size_property: size of the degamma LUT as supported by the
2045  *      driver (read-only)
2046  * @ctm_property: Matrix used to convert colors after the lookup in the
2047  *      degamma LUT
2048  * @gamma_lut_property: LUT used to convert the colors, after the CSC matrix, to
2049  *      the gamma space of the connected screen (read-only)
2050  * @gamma_lut_size_property: size of the gamma LUT as supported by the driver
2051  * @preferred_depth: preferred RBG pixel depth, used by fb helpers
2052  * @prefer_shadow: hint to userspace to prefer shadow-fb rendering
2053  * @async_page_flip: does this device support async flips on the primary plane?
2054  * @cursor_width: hint to userspace for max cursor width
2055  * @cursor_height: hint to userspace for max cursor height
2056  *
2057  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
2058  * enumerated by the driver are added here, as are global properties.  Some
2059  * global restrictions are also here, e.g. dimension restrictions.
2060  */
2061 struct drm_mode_config {
2062         struct mutex mutex; /* protects configuration (mode lists etc.) */
2063         struct drm_modeset_lock connection_mutex; /* protects connector->encoder and encoder->crtc links */
2064         struct drm_modeset_acquire_ctx *acquire_ctx; /* for legacy _lock_all() / _unlock_all() */
2065         struct mutex idr_mutex; /* for IDR management */
2066         struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
2067         struct idr tile_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
2068         /* this is limited to one for now */
2069
2070         struct mutex fb_lock; /* proctects global and per-file fb lists */
2071         int num_fb;
2072         struct list_head fb_list;
2073
2074         int num_connector;
2075         struct ida connector_ida;
2076         struct list_head connector_list;
2077         int num_encoder;
2078         struct list_head encoder_list;
2079
2080         /*
2081          * Track # of overlay planes separately from # of total planes.  By
2082          * default we only advertise overlay planes to userspace; if userspace
2083          * sets the "universal plane" capability bit, we'll go ahead and
2084          * expose all planes.
2085          */
2086         int num_overlay_plane;
2087         int num_total_plane;
2088         struct list_head plane_list;
2089
2090         int num_crtc;
2091         struct list_head crtc_list;
2092
2093         struct list_head property_list;
2094
2095         int min_width, min_height;
2096         int max_width, max_height;
2097         const struct drm_mode_config_funcs *funcs;
2098         resource_size_t fb_base;
2099
2100         /* output poll support */
2101         bool poll_enabled;
2102         bool poll_running;
2103         bool delayed_event;
2104         struct delayed_work output_poll_work;
2105
2106         struct mutex blob_lock;
2107
2108         /* pointers to standard properties */
2109         struct list_head property_blob_list;
2110         struct drm_property *edid_property;
2111         struct drm_property *dpms_property;
2112         struct drm_property *path_property;
2113         struct drm_property *tile_property;
2114         struct drm_property *plane_type_property;
2115         struct drm_property *rotation_property;
2116         struct drm_property *prop_src_x;
2117         struct drm_property *prop_src_y;
2118         struct drm_property *prop_src_w;
2119         struct drm_property *prop_src_h;
2120         struct drm_property *prop_crtc_x;
2121         struct drm_property *prop_crtc_y;
2122         struct drm_property *prop_crtc_w;
2123         struct drm_property *prop_crtc_h;
2124         struct drm_property *prop_fb_id;
2125         struct drm_property *prop_crtc_id;
2126         struct drm_property *prop_active;
2127         struct drm_property *prop_mode_id;
2128
2129         /* DVI-I properties */
2130         struct drm_property *dvi_i_subconnector_property;
2131         struct drm_property *dvi_i_select_subconnector_property;
2132
2133         /* TV properties */
2134         struct drm_property *tv_subconnector_property;
2135         struct drm_property *tv_select_subconnector_property;
2136         struct drm_property *tv_mode_property;
2137         struct drm_property *tv_left_margin_property;
2138         struct drm_property *tv_right_margin_property;
2139         struct drm_property *tv_top_margin_property;
2140         struct drm_property *tv_bottom_margin_property;
2141         struct drm_property *tv_brightness_property;
2142         struct drm_property *tv_contrast_property;
2143         struct drm_property *tv_flicker_reduction_property;
2144         struct drm_property *tv_overscan_property;
2145         struct drm_property *tv_saturation_property;
2146         struct drm_property *tv_hue_property;
2147
2148         /* Optional properties */
2149         struct drm_property *scaling_mode_property;
2150         struct drm_property *aspect_ratio_property;
2151         struct drm_property *dirty_info_property;
2152
2153         /* Optional color correction properties */
2154         struct drm_property *degamma_lut_property;
2155         struct drm_property *degamma_lut_size_property;
2156         struct drm_property *ctm_property;
2157         struct drm_property *gamma_lut_property;
2158         struct drm_property *gamma_lut_size_property;
2159
2160         /* properties for virtual machine layout */
2161         struct drm_property *suggested_x_property;
2162         struct drm_property *suggested_y_property;
2163
2164         /* dumb ioctl parameters */
2165         uint32_t preferred_depth, prefer_shadow;
2166
2167         /* whether async page flip is supported or not */
2168         bool async_page_flip;
2169
2170         /* whether the driver supports fb modifiers */
2171         bool allow_fb_modifiers;
2172
2173         /* cursor size */
2174         uint32_t cursor_width, cursor_height;
2175 };
2176
2177 /**
2178  * drm_for_each_plane_mask - iterate over planes specified by bitmask
2179  * @plane: the loop cursor
2180  * @dev: the DRM device
2181  * @plane_mask: bitmask of plane indices
2182  *
2183  * Iterate over all planes specified by bitmask.
2184  */
2185 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
2186         list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
2187                 for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
2188
2189 /**
2190  * drm_for_each_encoder_mask - iterate over encoders specified by bitmask
2191  * @encoder: the loop cursor
2192  * @dev: the DRM device
2193  * @encoder_mask: bitmask of encoder indices
2194  *
2195  * Iterate over all encoders specified by bitmask.
2196  */
2197 #define drm_for_each_encoder_mask(encoder, dev, encoder_mask) \
2198         list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, head) \
2199                 for_each_if ((encoder_mask) & (1 << drm_encoder_index(encoder)))
2200
2201 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
2202 #define obj_to_connector(x) container_of(x, struct drm_connector, base)
2203 #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
2204 #define obj_to_mode(x) container_of(x, struct drm_display_mode, base)
2205 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
2206 #define obj_to_property(x) container_of(x, struct drm_property, base)
2207 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
2208 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
2209
2210 struct drm_prop_enum_list {
2211         int type;
2212         char *name;
2213 };
2214
2215 extern __printf(6, 7)
2216 int drm_crtc_init_with_planes(struct drm_device *dev,
2217                               struct drm_crtc *crtc,
2218                               struct drm_plane *primary,
2219                               struct drm_plane *cursor,
2220                               const struct drm_crtc_funcs *funcs,
2221                               const char *name, ...);
2222 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
2223 extern unsigned int drm_crtc_index(struct drm_crtc *crtc);
2224
2225 /**
2226  * drm_crtc_mask - find the mask of a registered CRTC
2227  * @crtc: CRTC to find mask for
2228  *
2229  * Given a registered CRTC, return the mask bit of that CRTC for an
2230  * encoder's possible_crtcs field.
2231  */
2232 static inline uint32_t drm_crtc_mask(struct drm_crtc *crtc)
2233 {
2234         return 1 << drm_crtc_index(crtc);
2235 }
2236
2237 extern void drm_connector_ida_init(void);
2238 extern void drm_connector_ida_destroy(void);
2239 extern int drm_connector_init(struct drm_device *dev,
2240                               struct drm_connector *connector,
2241                               const struct drm_connector_funcs *funcs,
2242                               int connector_type);
2243 int drm_connector_register(struct drm_connector *connector);
2244 void drm_connector_unregister(struct drm_connector *connector);
2245
2246 extern void drm_connector_cleanup(struct drm_connector *connector);
2247 static inline unsigned drm_connector_index(struct drm_connector *connector)
2248 {
2249         return connector->connector_id;
2250 }
2251
2252 /* helpers to {un}register all connectors from sysfs for device */
2253 extern int drm_connector_register_all(struct drm_device *dev);
2254 extern void drm_connector_unregister_all(struct drm_device *dev);
2255
2256 extern int drm_bridge_add(struct drm_bridge *bridge);
2257 extern void drm_bridge_remove(struct drm_bridge *bridge);
2258 extern struct drm_bridge *of_drm_find_bridge(struct device_node *np);
2259 extern int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge);
2260
2261 bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
2262                         const struct drm_display_mode *mode,
2263                         struct drm_display_mode *adjusted_mode);
2264 void drm_bridge_disable(struct drm_bridge *bridge);
2265 void drm_bridge_post_disable(struct drm_bridge *bridge);
2266 void drm_bridge_mode_set(struct drm_bridge *bridge,
2267                         struct drm_display_mode *mode,
2268                         struct drm_display_mode *adjusted_mode);
2269 void drm_bridge_pre_enable(struct drm_bridge *bridge);
2270 void drm_bridge_enable(struct drm_bridge *bridge);
2271
2272 extern __printf(5, 6)
2273 int drm_encoder_init(struct drm_device *dev,
2274                      struct drm_encoder *encoder,
2275                      const struct drm_encoder_funcs *funcs,
2276                      int encoder_type, const char *name, ...);
2277 extern unsigned int drm_encoder_index(struct drm_encoder *encoder);
2278
2279 /**
2280  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
2281  * @encoder: encoder to test
2282  * @crtc: crtc to test
2283  *
2284  * Return false if @encoder can't be driven by @crtc, true otherwise.
2285  */
2286 static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
2287                                        struct drm_crtc *crtc)
2288 {
2289         return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
2290 }
2291
2292 extern __printf(8, 9)
2293 int drm_universal_plane_init(struct drm_device *dev,
2294                              struct drm_plane *plane,
2295                              unsigned long possible_crtcs,
2296                              const struct drm_plane_funcs *funcs,
2297                              const uint32_t *formats,
2298                              unsigned int format_count,
2299                              enum drm_plane_type type,
2300                              const char *name, ...);
2301 extern int drm_plane_init(struct drm_device *dev,
2302                           struct drm_plane *plane,
2303                           unsigned long possible_crtcs,
2304                           const struct drm_plane_funcs *funcs,
2305                           const uint32_t *formats, unsigned int format_count,
2306                           bool is_primary);
2307 extern void drm_plane_cleanup(struct drm_plane *plane);
2308 extern unsigned int drm_plane_index(struct drm_plane *plane);
2309 extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
2310 extern void drm_plane_force_disable(struct drm_plane *plane);
2311 extern int drm_plane_check_pixel_format(const struct drm_plane *plane,
2312                                         u32 format);
2313 extern void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2314                                    int *hdisplay, int *vdisplay);
2315 extern int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2316                                    int x, int y,
2317                                    const struct drm_display_mode *mode,
2318                                    const struct drm_framebuffer *fb);
2319
2320 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
2321
2322 extern const char *drm_get_connector_status_name(enum drm_connector_status status);
2323 extern const char *drm_get_subpixel_order_name(enum subpixel_order order);
2324 extern const char *drm_get_dpms_name(int val);
2325 extern const char *drm_get_dvi_i_subconnector_name(int val);
2326 extern const char *drm_get_dvi_i_select_name(int val);
2327 extern const char *drm_get_tv_subconnector_name(int val);
2328 extern const char *drm_get_tv_select_name(int val);
2329 extern void drm_fb_release(struct drm_file *file_priv);
2330 extern void drm_property_destroy_user_blobs(struct drm_device *dev,
2331                                             struct drm_file *file_priv);
2332 extern bool drm_probe_ddc(struct i2c_adapter *adapter);
2333 extern struct edid *drm_get_edid(struct drm_connector *connector,
2334                                  struct i2c_adapter *adapter);
2335 extern struct edid *drm_get_edid_switcheroo(struct drm_connector *connector,
2336                                             struct i2c_adapter *adapter);
2337 extern struct edid *drm_edid_duplicate(const struct edid *edid);
2338 extern int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
2339 extern void drm_mode_config_init(struct drm_device *dev);
2340 extern void drm_mode_config_reset(struct drm_device *dev);
2341 extern void drm_mode_config_cleanup(struct drm_device *dev);
2342
2343 extern int drm_mode_connector_set_path_property(struct drm_connector *connector,
2344                                                 const char *path);
2345 int drm_mode_connector_set_tile_property(struct drm_connector *connector);
2346 extern int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2347                                                    const struct edid *edid);
2348
2349 extern int drm_display_info_set_bus_formats(struct drm_display_info *info,
2350                                             const u32 *formats,
2351                                             unsigned int num_formats);
2352
2353 static inline bool drm_property_type_is(struct drm_property *property,
2354                 uint32_t type)
2355 {
2356         /* instanceof for props.. handles extended type vs original types: */
2357         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2358                 return (property->flags & DRM_MODE_PROP_EXTENDED_TYPE) == type;
2359         return property->flags & type;
2360 }
2361
2362 static inline bool drm_property_type_valid(struct drm_property *property)
2363 {
2364         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2365                 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2366         return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2367 }
2368
2369 extern int drm_object_property_set_value(struct drm_mode_object *obj,
2370                                          struct drm_property *property,
2371                                          uint64_t val);
2372 extern int drm_object_property_get_value(struct drm_mode_object *obj,
2373                                          struct drm_property *property,
2374                                          uint64_t *value);
2375 extern int drm_framebuffer_init(struct drm_device *dev,
2376                                 struct drm_framebuffer *fb,
2377                                 const struct drm_framebuffer_funcs *funcs);
2378 extern struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
2379                                                       uint32_t id);
2380 extern void drm_framebuffer_unreference(struct drm_framebuffer *fb);
2381 extern void drm_framebuffer_reference(struct drm_framebuffer *fb);
2382 extern void drm_framebuffer_remove(struct drm_framebuffer *fb);
2383 extern void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
2384 extern void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
2385
2386 extern void drm_object_attach_property(struct drm_mode_object *obj,
2387                                        struct drm_property *property,
2388                                        uint64_t init_val);
2389 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2390                                                 const char *name, int num_values);
2391 extern struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2392                                          const char *name,
2393                                          const struct drm_prop_enum_list *props,
2394                                          int num_values);
2395 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2396                                          int flags, const char *name,
2397                                          const struct drm_prop_enum_list *props,
2398                                          int num_props,
2399                                          uint64_t supported_bits);
2400 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2401                                          const char *name,
2402                                          uint64_t min, uint64_t max);
2403 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
2404                                          int flags, const char *name,
2405                                          int64_t min, int64_t max);
2406 struct drm_property *drm_property_create_object(struct drm_device *dev,
2407                                          int flags, const char *name, uint32_t type);
2408 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
2409                                          const char *name);
2410 struct drm_property_blob *drm_property_create_blob(struct drm_device *dev,
2411                                                    size_t length,
2412                                                    const void *data);
2413 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
2414                                                    uint32_t id);
2415 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob);
2416 void drm_property_unreference_blob(struct drm_property_blob *blob);
2417 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
2418 extern int drm_property_add_enum(struct drm_property *property, int index,
2419                                  uint64_t value, const char *name);
2420 extern int drm_mode_create_dvi_i_properties(struct drm_device *dev);
2421 extern int drm_mode_create_tv_properties(struct drm_device *dev,
2422                                          unsigned int num_modes,
2423                                          const char * const modes[]);
2424 extern int drm_mode_create_scaling_mode_property(struct drm_device *dev);
2425 extern int drm_mode_create_aspect_ratio_property(struct drm_device *dev);
2426 extern int drm_mode_create_dirty_info_property(struct drm_device *dev);
2427 extern int drm_mode_create_suggested_offset_properties(struct drm_device *dev);
2428 extern bool drm_property_change_valid_get(struct drm_property *property,
2429                                          uint64_t value, struct drm_mode_object **ref);
2430 extern void drm_property_change_valid_put(struct drm_property *property,
2431                 struct drm_mode_object *ref);
2432
2433 extern int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2434                                              struct drm_encoder *encoder);
2435 extern int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2436                                          int gamma_size);
2437 extern struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
2438                 uint32_t id, uint32_t type);
2439
2440 /* IOCTLs */
2441 extern int drm_mode_getresources(struct drm_device *dev,
2442                                  void *data, struct drm_file *file_priv);
2443 extern int drm_mode_getplane_res(struct drm_device *dev, void *data,
2444                                    struct drm_file *file_priv);
2445 extern int drm_mode_getcrtc(struct drm_device *dev,
2446                             void *data, struct drm_file *file_priv);
2447 extern int drm_mode_getconnector(struct drm_device *dev,
2448                               void *data, struct drm_file *file_priv);
2449 extern int drm_mode_set_config_internal(struct drm_mode_set *set);
2450 extern int drm_mode_setcrtc(struct drm_device *dev,
2451                             void *data, struct drm_file *file_priv);
2452 extern int drm_mode_getplane(struct drm_device *dev,
2453                                void *data, struct drm_file *file_priv);
2454 extern int drm_mode_setplane(struct drm_device *dev,
2455                                void *data, struct drm_file *file_priv);
2456 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
2457                                 void *data, struct drm_file *file_priv);
2458 extern int drm_mode_cursor2_ioctl(struct drm_device *dev,
2459                                 void *data, struct drm_file *file_priv);
2460 extern int drm_mode_addfb(struct drm_device *dev,
2461                           void *data, struct drm_file *file_priv);
2462 extern int drm_mode_addfb2(struct drm_device *dev,
2463                            void *data, struct drm_file *file_priv);
2464 extern uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
2465 extern int drm_mode_rmfb(struct drm_device *dev,
2466                          void *data, struct drm_file *file_priv);
2467 extern int drm_mode_getfb(struct drm_device *dev,
2468                           void *data, struct drm_file *file_priv);
2469 extern int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2470                                   void *data, struct drm_file *file_priv);
2471
2472 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
2473                                       void *data, struct drm_file *file_priv);
2474 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
2475                                   void *data, struct drm_file *file_priv);
2476 extern int drm_mode_createblob_ioctl(struct drm_device *dev,
2477                                      void *data, struct drm_file *file_priv);
2478 extern int drm_mode_destroyblob_ioctl(struct drm_device *dev,
2479                                       void *data, struct drm_file *file_priv);
2480 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2481                                               void *data, struct drm_file *file_priv);
2482 extern int drm_mode_getencoder(struct drm_device *dev,
2483                                void *data, struct drm_file *file_priv);
2484 extern int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2485                                     void *data, struct drm_file *file_priv);
2486 extern int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2487                                     void *data, struct drm_file *file_priv);
2488 extern u8 drm_match_cea_mode(const struct drm_display_mode *to_match);
2489 extern enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code);
2490 extern bool drm_detect_hdmi_monitor(struct edid *edid);
2491 extern bool drm_detect_monitor_audio(struct edid *edid);
2492 extern bool drm_rgb_quant_range_selectable(struct edid *edid);
2493 extern int drm_mode_page_flip_ioctl(struct drm_device *dev,
2494                                     void *data, struct drm_file *file_priv);
2495 extern int drm_add_modes_noedid(struct drm_connector *connector,
2496                                 int hdisplay, int vdisplay);
2497 extern void drm_set_preferred_mode(struct drm_connector *connector,
2498                                    int hpref, int vpref);
2499
2500 extern int drm_edid_header_is_valid(const u8 *raw_edid);
2501 extern bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid,
2502                                  bool *edid_corrupt);
2503 extern bool drm_edid_is_valid(struct edid *edid);
2504 extern void drm_edid_get_monitor_name(struct edid *edid, char *name,
2505                                       int buflen);
2506
2507 extern struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2508                                                          char topology[8]);
2509 extern struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2510                                                char topology[8]);
2511 extern void drm_mode_put_tile_group(struct drm_device *dev,
2512                                    struct drm_tile_group *tg);
2513 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
2514                                            int hsize, int vsize, int fresh,
2515                                            bool rb);
2516
2517 extern int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2518                                       void *data, struct drm_file *file_priv);
2519 extern int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2520                                     void *data, struct drm_file *file_priv);
2521 extern int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2522                                       void *data, struct drm_file *file_priv);
2523 extern int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
2524                                              struct drm_file *file_priv);
2525 extern int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
2526                                            struct drm_file *file_priv);
2527 extern int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
2528                                        struct drm_property *property,
2529                                        uint64_t value);
2530 extern int drm_mode_atomic_ioctl(struct drm_device *dev,
2531                                  void *data, struct drm_file *file_priv);
2532
2533 extern void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
2534                                  int *bpp);
2535 extern int drm_format_num_planes(uint32_t format);
2536 extern int drm_format_plane_cpp(uint32_t format, int plane);
2537 extern int drm_format_horz_chroma_subsampling(uint32_t format);
2538 extern int drm_format_vert_chroma_subsampling(uint32_t format);
2539 extern int drm_format_plane_width(int width, uint32_t format, int plane);
2540 extern int drm_format_plane_height(int height, uint32_t format, int plane);
2541 extern const char *drm_get_format_name(uint32_t format);
2542 extern struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2543                                                               unsigned int supported_rotations);
2544 extern unsigned int drm_rotation_simplify(unsigned int rotation,
2545                                           unsigned int supported_rotations);
2546
2547 /* Helpers */
2548
2549 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
2550                 uint32_t id)
2551 {
2552         struct drm_mode_object *mo;
2553         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
2554         return mo ? obj_to_plane(mo) : NULL;
2555 }
2556
2557 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
2558         uint32_t id)
2559 {
2560         struct drm_mode_object *mo;
2561         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CRTC);
2562         return mo ? obj_to_crtc(mo) : NULL;
2563 }
2564
2565 static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
2566         uint32_t id)
2567 {
2568         struct drm_mode_object *mo;
2569         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
2570         return mo ? obj_to_encoder(mo) : NULL;
2571 }
2572
2573 static inline struct drm_connector *drm_connector_find(struct drm_device *dev,
2574                 uint32_t id)
2575 {
2576         struct drm_mode_object *mo;
2577         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CONNECTOR);
2578         return mo ? obj_to_connector(mo) : NULL;
2579 }
2580
2581 static inline struct drm_property *drm_property_find(struct drm_device *dev,
2582                 uint32_t id)
2583 {
2584         struct drm_mode_object *mo;
2585         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PROPERTY);
2586         return mo ? obj_to_property(mo) : NULL;
2587 }
2588
2589 /*
2590  * Extract a degamma/gamma LUT value provided by user and round it to the
2591  * precision supported by the hardware.
2592  */
2593 static inline uint32_t drm_color_lut_extract(uint32_t user_input,
2594                                              uint32_t bit_precision)
2595 {
2596         uint32_t val = user_input;
2597         uint32_t max = 0xffff >> (16 - bit_precision);
2598
2599         /* Round only if we're not using full precision. */
2600         if (bit_precision < 16) {
2601                 val += 1UL << (16 - bit_precision - 1);
2602                 val >>= 16 - bit_precision;
2603         }
2604
2605         return clamp_val(val, 0, max);
2606 }
2607
2608 /**
2609  * drm_framebuffer_read_refcount - read the framebuffer reference count.
2610  * @fb: framebuffer
2611  *
2612  * This functions returns the framebuffer's reference count.
2613  */
2614 static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
2615 {
2616         return atomic_read(&fb->refcount.refcount);
2617 }
2618
2619 /* Plane list iterator for legacy (overlay only) planes. */
2620 #define drm_for_each_legacy_plane(plane, dev) \
2621         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
2622                 for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
2623
2624 #define drm_for_each_plane(plane, dev) \
2625         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
2626
2627 #define drm_for_each_crtc(crtc, dev) \
2628         list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
2629
2630 static inline void
2631 assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
2632 {
2633         /*
2634          * The connector hotadd/remove code currently grabs both locks when
2635          * updating lists. Hence readers need only hold either of them to be
2636          * safe and the check amounts to
2637          *
2638          * WARN_ON(not_holding(A) && not_holding(B)).
2639          */
2640         WARN_ON(!mutex_is_locked(&mode_config->mutex) &&
2641                 !drm_modeset_is_locked(&mode_config->connection_mutex));
2642 }
2643
2644 #define drm_for_each_connector(connector, dev) \
2645         for (assert_drm_connector_list_read_locked(&(dev)->mode_config),        \
2646              connector = list_first_entry(&(dev)->mode_config.connector_list,   \
2647                                           struct drm_connector, head);          \
2648              &connector->head != (&(dev)->mode_config.connector_list);          \
2649              connector = list_next_entry(connector, head))
2650
2651 #define drm_for_each_encoder(encoder, dev) \
2652         list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
2653
2654 #define drm_for_each_fb(fb, dev) \
2655         for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),            \
2656              fb = list_first_entry(&(dev)->mode_config.fb_list, \
2657                                           struct drm_framebuffer, head);        \
2658              &fb->head != (&(dev)->mode_config.fb_list);                        \
2659              fb = list_next_entry(fb, head))
2660
2661 #endif /* __DRM_CRTC_H__ */