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