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