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