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