drm/rockchip: add loader protect for encoder
[firefly-linux-kernel-4.4.55.git] / include / drm / drm_crtc.h
1 /*
2  * Copyright © 2006 Keith Packard
3  * Copyright © 2007-2008 Dave Airlie
4  * Copyright © 2007-2008 Intel Corporation
5  *   Jesse Barnes <jesse.barnes@intel.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef __DRM_CRTC_H__
26 #define __DRM_CRTC_H__
27
28 #include <linux/i2c.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/idr.h>
32 #include <linux/fb.h>
33 #include <linux/hdmi.h>
34 #include <linux/media-bus-format.h>
35 #include <uapi/drm/drm_mode.h>
36 #include <uapi/drm/drm_fourcc.h>
37 #include <drm/drm_modeset_lock.h>
38
39 struct drm_device;
40 struct drm_mode_set;
41 struct drm_framebuffer;
42 struct drm_object_properties;
43 struct drm_file;
44 struct drm_clip_rect;
45 struct device_node;
46 struct fence;
47
48 #define DRM_MODE_OBJECT_CRTC 0xcccccccc
49 #define DRM_MODE_OBJECT_CONNECTOR 0xc0c0c0c0
50 #define DRM_MODE_OBJECT_ENCODER 0xe0e0e0e0
51 #define DRM_MODE_OBJECT_MODE 0xdededede
52 #define DRM_MODE_OBJECT_PROPERTY 0xb0b0b0b0
53 #define DRM_MODE_OBJECT_FB 0xfbfbfbfb
54 #define DRM_MODE_OBJECT_BLOB 0xbbbbbbbb
55 #define DRM_MODE_OBJECT_PLANE 0xeeeeeeee
56 #define DRM_MODE_OBJECT_ANY 0
57
58 struct drm_mode_object {
59         uint32_t id;
60         uint32_t type;
61         struct drm_object_properties *properties;
62 };
63
64 #define DRM_OBJECT_MAX_PROPERTY 24
65 struct drm_object_properties {
66         int count, atomic_count;
67         /* NOTE: if we ever start dynamically destroying properties (ie.
68          * not at drm_mode_config_cleanup() time), then we'd have to do
69          * a better job of detaching property from mode objects to avoid
70          * dangling property pointers:
71          */
72         struct drm_property *properties[DRM_OBJECT_MAX_PROPERTY];
73         /* do not read/write values directly, but use drm_object_property_get_value()
74          * and drm_object_property_set_value():
75          */
76         uint64_t values[DRM_OBJECT_MAX_PROPERTY];
77 };
78
79 static inline int64_t U642I64(uint64_t val)
80 {
81         return (int64_t)*((int64_t *)&val);
82 }
83 static inline uint64_t I642U64(int64_t val)
84 {
85         return (uint64_t)*((uint64_t *)&val);
86 }
87
88 /* rotation property bits */
89 #define DRM_ROTATE_MASK 0x0f
90 #define DRM_ROTATE_0    0
91 #define DRM_ROTATE_90   1
92 #define DRM_ROTATE_180  2
93 #define DRM_ROTATE_270  3
94 #define DRM_REFLECT_MASK (~DRM_ROTATE_MASK)
95 #define DRM_REFLECT_X   4
96 #define DRM_REFLECT_Y   5
97
98 enum drm_connector_force {
99         DRM_FORCE_UNSPECIFIED,
100         DRM_FORCE_OFF,
101         DRM_FORCE_ON,         /* force on analog part normally */
102         DRM_FORCE_ON_DIGITAL, /* for DVI-I use digital connector */
103 };
104
105 #include <drm/drm_modes.h>
106
107 enum drm_connector_status {
108         connector_status_connected = 1,
109         connector_status_disconnected = 2,
110         connector_status_unknown = 3,
111 };
112
113 enum subpixel_order {
114         SubPixelUnknown = 0,
115         SubPixelHorizontalRGB,
116         SubPixelHorizontalBGR,
117         SubPixelVerticalRGB,
118         SubPixelVerticalBGR,
119         SubPixelNone,
120
121 };
122
123 /**
124  * struct drm_scrambling: sink's scrambling support.
125  */
126 struct drm_scrambling {
127         /**
128          * @supported: scrambling supported for rates > 340 Mhz.
129          */
130         bool supported;
131         /**
132          * @low_rates: scrambling supported for rates <= 340 Mhz.
133          */
134         bool low_rates;
135 };
136
137 /*
138  * struct drm_scdc - Information about scdc capabilities of a HDMI 2.0 sink
139  *
140  * Provides SCDC register support and capabilities related information on a
141  * HDMI 2.0 sink. In case of a HDMI 1.4 sink, all parameter must be 0.
142  */
143 struct drm_scdc {
144         /**
145          * @supported: status control & data channel present.
146          */
147         bool supported;
148         /**
149          * @read_request: sink is capable of generating scdc read request.
150          */
151         bool read_request;
152         /**
153          * @scrambling: sink's scrambling capabilities
154          */
155         struct drm_scrambling scrambling;
156 };
157
158
159 /**
160  * struct drm_hdmi_info - runtime information about the connected HDMI sink
161  *
162  * Describes if a given display supports advanced HDMI 2.0 features.
163  * This information is available in CEA-861-F extension blocks (like HF-VSDB).
164  */
165 struct drm_hdmi_info {
166         struct drm_scdc scdc;
167 };
168
169 #define DRM_COLOR_FORMAT_RGB444         (1<<0)
170 #define DRM_COLOR_FORMAT_YCRCB444       (1<<1)
171 #define DRM_COLOR_FORMAT_YCRCB422       (1<<2)
172 /*
173  * Describes a given display (e.g. CRT or flat panel) and its limitations.
174  */
175 struct drm_display_info {
176         char name[DRM_DISPLAY_INFO_LEN];
177
178         /* Physical size */
179         unsigned int width_mm;
180         unsigned int height_mm;
181
182         /* Clock limits FIXME: storage format */
183         unsigned int min_vfreq, max_vfreq;
184         unsigned int min_hfreq, max_hfreq;
185         unsigned int pixel_clock;
186         unsigned int bpc;
187
188         enum subpixel_order subpixel_order;
189         u32 color_formats;
190
191         const u32 *bus_formats;
192         unsigned int num_bus_formats;
193
194         /**
195          * @max_tmds_clock: Maximum TMDS clock rate supported by the
196          * sink in kHz. 0 means undefined.
197          */
198         int max_tmds_clock;
199
200         /**
201          * @dvi_dual: Dual-link DVI sink?
202          */
203         bool dvi_dual;
204
205         /* Mask of supported hdmi deep color modes */
206         u8 edid_hdmi_dc_modes;
207
208         u8 cea_rev;
209
210         /**
211          * @hdmi: advance features of a HDMI sink.
212          */
213         struct drm_hdmi_info hdmi;
214 };
215
216 /* data corresponds to displayid vend/prod/serial */
217 struct drm_tile_group {
218         struct kref refcount;
219         struct drm_device *dev;
220         int id;
221         u8 group_data[8];
222 };
223
224 struct drm_framebuffer_funcs {
225         /* note: use drm_framebuffer_remove() */
226         void (*destroy)(struct drm_framebuffer *framebuffer);
227         int (*create_handle)(struct drm_framebuffer *fb,
228                              struct drm_file *file_priv,
229                              unsigned int *handle);
230         /*
231          * Optional callback for the dirty fb ioctl.
232          *
233          * Userspace can notify the driver via this callback
234          * that a area of the framebuffer has changed and should
235          * be flushed to the display hardware.
236          *
237          * See documentation in drm_mode.h for the struct
238          * drm_mode_fb_dirty_cmd for more information as all
239          * the semantics and arguments have a one to one mapping
240          * on this function.
241          */
242         int (*dirty)(struct drm_framebuffer *framebuffer,
243                      struct drm_file *file_priv, unsigned flags,
244                      unsigned color, struct drm_clip_rect *clips,
245                      unsigned num_clips);
246 };
247
248 struct drm_framebuffer {
249         struct drm_device *dev;
250         /*
251          * Note that the fb is refcounted for the benefit of driver internals,
252          * for example some hw, disabling a CRTC/plane is asynchronous, and
253          * scanout does not actually complete until the next vblank.  So some
254          * cleanup (like releasing the reference(s) on the backing GEM bo(s))
255          * should be deferred.  In cases like this, the driver would like to
256          * hold a ref to the fb even though it has already been removed from
257          * userspace perspective.
258          */
259         struct kref refcount;
260         /*
261          * Place on the dev->mode_config.fb_list, access protected by
262          * dev->mode_config.fb_lock.
263          */
264         struct list_head head;
265         struct drm_mode_object base;
266         const struct drm_framebuffer_funcs *funcs;
267         unsigned int pitches[4];
268         unsigned int offsets[4];
269         uint64_t modifier[4];
270         unsigned int width;
271         unsigned int height;
272         /* depth can be 15 or 16 */
273         unsigned int depth;
274         int bits_per_pixel;
275         int flags;
276         uint32_t pixel_format; /* fourcc format */
277         struct list_head filp_head;
278 };
279
280 struct drm_property_blob {
281         struct drm_mode_object base;
282         struct drm_device *dev;
283         struct kref refcount;
284         struct list_head head_global;
285         struct list_head head_file;
286         size_t length;
287         unsigned char data[];
288 };
289
290 struct drm_property_enum {
291         uint64_t value;
292         struct list_head head;
293         char name[DRM_PROP_NAME_LEN];
294 };
295
296 struct drm_property {
297         struct list_head head;
298         struct drm_mode_object base;
299         uint32_t flags;
300         char name[DRM_PROP_NAME_LEN];
301         uint32_t num_values;
302         uint64_t *values;
303         struct drm_device *dev;
304
305         struct list_head enum_list;
306 };
307
308 struct drm_crtc;
309 struct drm_connector;
310 struct drm_encoder;
311 struct drm_pending_vblank_event;
312 struct drm_plane;
313 struct drm_bridge;
314 struct drm_atomic_state;
315
316 /**
317  * struct drm_crtc_state - mutable CRTC state
318  * @crtc: backpointer to the CRTC
319  * @enable: whether the CRTC should be enabled, gates all other state
320  * @active: whether the CRTC is actively displaying (used for DPMS)
321  * @planes_changed: planes on this crtc are updated
322  * @mode_changed: crtc_state->mode or crtc_state->enable has been changed
323  * @active_changed: crtc_state->active has been toggled.
324  * @connectors_changed: connectors to this crtc have been updated
325  * @color_mgmt_changed: color management properties have changed (degamma or
326  *      gamma LUT or CSC matrix)
327  * @plane_mask: bitmask of (1 << drm_plane_index(plane)) of attached planes
328  * @last_vblank_count: for helpers and drivers to capture the vblank of the
329  *      update to ensure framebuffer cleanup isn't done too early
330  * @adjusted_mode: for use by helpers and drivers to compute adjusted mode timings
331  * @mode: current mode timings
332  * @degamma_lut: Lookup table for converting framebuffer pixel data
333  *      before apply the conversion matrix
334  * @ctm: Transformation matrix
335  * @gamma_lut: Lookup table for converting pixel data after the
336  *      conversion matrix
337  * @event: optional pointer to a DRM event to signal upon completion of the
338  *      state update
339  * @state: backpointer to global drm_atomic_state
340  *
341  * Note that the distinction between @enable and @active is rather subtile:
342  * Flipping @active while @enable is set without changing anything else may
343  * never return in a failure from the ->atomic_check callback. Userspace assumes
344  * that a DPMS On will always succeed. In other words: @enable controls resource
345  * assignment, @active controls the actual hardware state.
346  */
347 struct drm_crtc_state {
348         struct drm_crtc *crtc;
349
350         bool enable;
351         bool active;
352
353         /* computed state bits used by helpers and drivers */
354         bool planes_changed : 1;
355         bool mode_changed : 1;
356         bool active_changed : 1;
357         bool connectors_changed : 1;
358         bool color_mgmt_changed : 1;
359
360         /* attached planes bitmask:
361          * WARNING: transitional helpers do not maintain plane_mask so
362          * drivers not converted over to atomic helpers should not rely
363          * on plane_mask being accurate!
364          */
365         u32 plane_mask;
366
367         /* last_vblank_count: for vblank waits before cleanup */
368         u32 last_vblank_count;
369
370         /* adjusted_mode: for use by helpers and drivers */
371         struct drm_display_mode adjusted_mode;
372
373         struct drm_display_mode mode;
374
375         /* blob property to expose current mode to atomic userspace */
376         struct drm_property_blob *mode_blob;
377
378         /* blob property to expose color management to userspace */
379         struct drm_property_blob *degamma_lut;
380         struct drm_property_blob *ctm;
381         struct drm_property_blob *gamma_lut;
382
383         struct drm_pending_vblank_event *event;
384
385         struct drm_atomic_state *state;
386 };
387
388 /**
389  * struct drm_crtc_funcs - control CRTCs for a given device
390  * @save: save CRTC state
391  * @restore: restore CRTC state
392  * @reset: reset CRTC after state has been invalidated (e.g. resume)
393  * @cursor_set: setup the cursor
394  * @cursor_set2: setup the cursor with hotspot, superseeds @cursor_set if set
395  * @cursor_move: move the cursor
396  * @gamma_set: specify color ramp for CRTC
397  * @destroy: deinit and free object
398  * @set_property: called when a property is changed
399  * @set_config: apply a new CRTC configuration
400  * @page_flip: initiate a page flip
401  * @atomic_duplicate_state: duplicate the atomic state for this CRTC
402  * @atomic_destroy_state: destroy an atomic state for this CRTC
403  * @atomic_set_property: set a property on an atomic state for this CRTC
404  *    (do not call directly, use drm_atomic_crtc_set_property())
405  * @atomic_get_property: get a property on an atomic state for this CRTC
406  *    (do not call directly, use drm_atomic_crtc_get_property())
407  *
408  * The drm_crtc_funcs structure is the central CRTC management structure
409  * in the DRM.  Each CRTC controls one or more connectors (note that the name
410  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
411  * connectors, not just CRTs).
412  *
413  * Each driver is responsible for filling out this structure at startup time,
414  * in addition to providing other modesetting features, like i2c and DDC
415  * bus accessors.
416  */
417 struct drm_crtc_funcs {
418         /* Save CRTC state */
419         void (*save)(struct drm_crtc *crtc); /* suspend? */
420         /* Restore CRTC state */
421         void (*restore)(struct drm_crtc *crtc); /* resume? */
422         /* Reset CRTC state */
423         void (*reset)(struct drm_crtc *crtc);
424
425         /* cursor controls */
426         int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
427                           uint32_t handle, uint32_t width, uint32_t height);
428         int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
429                            uint32_t handle, uint32_t width, uint32_t height,
430                            int32_t hot_x, int32_t hot_y);
431         int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
432
433         /* Set gamma on the CRTC */
434         void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
435                           uint32_t start, uint32_t size);
436         /* Object destroy routine */
437         void (*destroy)(struct drm_crtc *crtc);
438
439         int (*set_config)(struct drm_mode_set *set);
440
441         /*
442          * Flip to the given framebuffer.  This implements the page
443          * flip ioctl described in drm_mode.h, specifically, the
444          * implementation must return immediately and block all
445          * rendering to the current fb until the flip has completed.
446          * If userspace set the event flag in the ioctl, the event
447          * argument will point to an event to send back when the flip
448          * completes, otherwise it will be NULL.
449          */
450         int (*page_flip)(struct drm_crtc *crtc,
451                          struct drm_framebuffer *fb,
452                          struct drm_pending_vblank_event *event,
453                          uint32_t flags);
454
455         int (*set_property)(struct drm_crtc *crtc,
456                             struct drm_property *property, uint64_t val);
457
458         /* atomic update handling */
459         struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);
460         void (*atomic_destroy_state)(struct drm_crtc *crtc,
461                                      struct drm_crtc_state *state);
462         int (*atomic_set_property)(struct drm_crtc *crtc,
463                                    struct drm_crtc_state *state,
464                                    struct drm_property *property,
465                                    uint64_t val);
466         int (*atomic_get_property)(struct drm_crtc *crtc,
467                                    const struct drm_crtc_state *state,
468                                    struct drm_property *property,
469                                    uint64_t *val);
470
471         /**
472          * @late_register:
473          *
474          * This optional hook can be used to register additional userspace
475          * interfaces attached to the crtc like debugfs interfaces.
476          * It is called late in the driver load sequence from drm_dev_register().
477          * Everything added from this callback should be unregistered in
478          * the early_unregister callback.
479          *
480          * Returns:
481          *
482          * 0 on success, or a negative error code on failure.
483          */
484         int (*late_register)(struct drm_crtc *crtc);
485
486         /**
487          * @early_unregister:
488          *
489          * This optional hook should be used to unregister the additional
490          * userspace interfaces attached to the crtc from
491          * late_unregister(). It is called from drm_dev_unregister(),
492          * early in the driver unload sequence to disable userspace access
493          * before data structures are torndown.
494          */
495         void (*early_unregister)(struct drm_crtc *crtc);
496 };
497
498 /**
499  * struct drm_crtc - central CRTC control structure
500  * @dev: parent DRM device
501  * @port: OF node used by drm_of_find_possible_crtcs()
502  * @head: list management
503  * @mutex: per-CRTC locking
504  * @base: base KMS object for ID tracking etc.
505  * @primary: primary plane for this CRTC
506  * @cursor: cursor plane for this CRTC
507  * @cursor_x: current x position of the cursor, used for universal cursor planes
508  * @cursor_y: current y position of the cursor, used for universal cursor planes
509  * @enabled: is this CRTC enabled?
510  * @mode: current mode timings
511  * @hwmode: mode timings as programmed to hw regs
512  * @x: x position on screen
513  * @y: y position on screen
514  * @funcs: CRTC control functions
515  * @gamma_size: size of gamma ramp
516  * @gamma_store: gamma ramp values
517  * @helper_private: mid-layer private data
518  * @properties: property tracking for this CRTC
519  * @state: current atomic state for this CRTC
520  * @acquire_ctx: per-CRTC implicit acquire context used by atomic drivers for
521  *      legacy ioctls
522  *
523  * Each CRTC may have one or more connectors associated with it.  This structure
524  * allows the CRTC to be controlled.
525  */
526 struct drm_crtc {
527         struct drm_device *dev;
528         struct device_node *port;
529         struct list_head head;
530
531         /*
532          * crtc mutex
533          *
534          * This provides a read lock for the overall crtc state (mode, dpms
535          * state, ...) and a write lock for everything which can be update
536          * without a full modeset (fb, cursor data, ...)
537          */
538         struct drm_modeset_lock mutex;
539
540         struct drm_mode_object base;
541
542         /* primary and cursor planes for CRTC */
543         struct drm_plane *primary;
544         struct drm_plane *cursor;
545
546         /* position of cursor plane on crtc */
547         int cursor_x;
548         int cursor_y;
549
550         bool enabled;
551
552         /* Requested mode from modesetting. */
553         struct drm_display_mode mode;
554
555         /* Programmed mode in hw, after adjustments for encoders,
556          * crtc, panel scaling etc. Needed for timestamping etc.
557          */
558         struct drm_display_mode hwmode;
559
560         int x, y;
561         const struct drm_crtc_funcs *funcs;
562
563         /* Legacy FB CRTC gamma size for reporting to userspace */
564         uint32_t gamma_size;
565         uint16_t *gamma_store;
566
567         /* if you are using the helper */
568         const void *helper_private;
569
570         struct drm_object_properties properties;
571
572         struct drm_crtc_state *state;
573
574         /*
575          * For legacy crtc ioctls so that atomic drivers can get at the locking
576          * acquire context.
577          */
578         struct drm_modeset_acquire_ctx *acquire_ctx;
579 };
580
581 /**
582  * struct drm_connector_state - mutable connector state
583  * @connector: backpointer to the connector
584  * @crtc: CRTC to connect connector to, NULL if disabled
585  * @best_encoder: can be used by helpers and drivers to select the encoder
586  * @state: backpointer to global drm_atomic_state
587  */
588 struct drm_connector_state {
589         struct drm_connector *connector;
590
591         struct drm_crtc *crtc;  /* do not write directly, use drm_atomic_set_crtc_for_connector() */
592
593         struct drm_encoder *best_encoder;
594
595         struct drm_atomic_state *state;
596 };
597
598 /**
599  * struct drm_connector_funcs - control connectors on a given device
600  * @dpms: set power state
601  * @save: save connector state
602  * @restore: restore connector state
603  * @reset: reset connector after state has been invalidated (e.g. resume)
604  * @detect: is this connector active?
605  * @fill_modes: fill mode list for this connector
606  * @set_property: property for this connector may need an update
607  * @destroy: make object go away
608  * @force: notify the driver that the connector is forced on
609  * @atomic_duplicate_state: duplicate the atomic state for this connector
610  * @atomic_destroy_state: destroy an atomic state for this connector
611  * @atomic_set_property: set a property on an atomic state for this connector
612  *    (do not call directly, use drm_atomic_connector_set_property())
613  * @atomic_get_property: get a property on an atomic state for this connector
614  *    (do not call directly, use drm_atomic_connector_get_property())
615  *
616  * Each CRTC may have one or more connectors attached to it.  The functions
617  * below allow the core DRM code to control connectors, enumerate available modes,
618  * etc.
619  */
620 struct drm_connector_funcs {
621         int (*dpms)(struct drm_connector *connector, int mode);
622         void (*save)(struct drm_connector *connector);
623         void (*restore)(struct drm_connector *connector);
624         void (*reset)(struct drm_connector *connector);
625
626         /* Check to see if anything is attached to the connector.
627          * @force is set to false whilst polling, true when checking the
628          * connector due to user request. @force can be used by the driver
629          * to avoid expensive, destructive operations during automated
630          * probing.
631          */
632         enum drm_connector_status (*detect)(struct drm_connector *connector,
633                                             bool force);
634         int (*fill_modes)(struct drm_connector *connector, uint32_t max_width, uint32_t max_height);
635         int (*set_property)(struct drm_connector *connector, struct drm_property *property,
636                              uint64_t val);
637
638         /**
639          * @late_register:
640          *
641          * This optional hook can be used to register additional userspace
642          * interfaces attached to the connector, light backlight control, i2c,
643          * DP aux or similar interfaces. It is called late in the driver load
644          * sequence from drm_connector_register() when registering all the
645          * core drm connector interfaces. Everything added from this callback
646          * should be unregistered in the early_unregister callback.
647          *
648          * Returns:
649          *
650          * 0 on success, or a negative error code on failure.
651          */
652         int (*late_register)(struct drm_connector *connector);
653
654         /**
655          * @early_unregister:
656          *
657          * This optional hook should be used to unregister the additional
658          * userspace interfaces attached to the connector from
659          * late_unregister(). It is called from drm_connector_unregister(),
660          * early in the driver unload sequence to disable userspace access
661          * before data structures are torndown.
662          */
663         void (*early_unregister)(struct drm_connector *connector);
664
665         void (*destroy)(struct drm_connector *connector);
666         void (*force)(struct drm_connector *connector);
667
668         /* atomic update handling */
669         struct drm_connector_state *(*atomic_duplicate_state)(struct drm_connector *connector);
670         void (*atomic_destroy_state)(struct drm_connector *connector,
671                                      struct drm_connector_state *state);
672         int (*atomic_set_property)(struct drm_connector *connector,
673                                    struct drm_connector_state *state,
674                                    struct drm_property *property,
675                                    uint64_t val);
676         int (*atomic_get_property)(struct drm_connector *connector,
677                                    const struct drm_connector_state *state,
678                                    struct drm_property *property,
679                                    uint64_t *val);
680 };
681
682 /**
683  * struct drm_encoder_funcs - encoder controls
684  * @reset: reset state (e.g. at init or resume time)
685  * @destroy: cleanup and free associated data
686  *
687  * Encoders sit between CRTCs and connectors.
688  */
689 struct drm_encoder_funcs {
690         void (*reset)(struct drm_encoder *encoder);
691         void (*destroy)(struct drm_encoder *encoder);
692
693         /**
694          * @late_register:
695          *
696          * This optional hook can be used to register additional userspace
697          * interfaces attached to the encoder like debugfs interfaces.
698          * It is called late in the driver load sequence from drm_dev_register().
699          * Everything added from this callback should be unregistered in
700          * the early_unregister callback.
701          *
702          * Returns:
703          *
704          * 0 on success, or a negative error code on failure.
705          */
706         int (*late_register)(struct drm_encoder *encoder);
707
708         /**
709          * @early_unregister:
710          *
711          * This optional hook should be used to unregister the additional
712          * userspace interfaces attached to the encoder from
713          * late_unregister(). It is called from drm_dev_unregister(),
714          * early in the driver unload sequence to disable userspace access
715          * before data structures are torndown.
716          */
717         void (*early_unregister)(struct drm_encoder *encoder);
718 };
719
720 #define DRM_CONNECTOR_MAX_ENCODER 3
721
722 /**
723  * struct drm_encoder - central DRM encoder structure
724  * @dev: parent DRM device
725  * @head: list management
726  * @base: base KMS object
727  * @name: encoder name
728  * @encoder_type: one of the %DRM_MODE_ENCODER_<foo> types in drm_mode.h
729  * @possible_crtcs: bitmask of potential CRTC bindings
730  * @possible_clones: bitmask of potential sibling encoders for cloning
731  * @crtc: currently bound CRTC
732  * @bridge: bridge associated to the encoder
733  * @funcs: control functions
734  * @helper_private: mid-layer private data
735  *
736  * CRTCs drive pixels to encoders, which convert them into signals
737  * appropriate for a given connector or set of connectors.
738  */
739 struct drm_encoder {
740         struct drm_device *dev;
741         struct list_head head;
742
743         struct drm_mode_object base;
744         char *name;
745         int encoder_type;
746         uint32_t possible_crtcs;
747         uint32_t possible_clones;
748         bool loader_protect;
749
750         struct drm_crtc *crtc;
751         struct drm_bridge *bridge;
752         const struct drm_encoder_funcs *funcs;
753         const void *helper_private;
754 };
755
756 /* should we poll this connector for connects and disconnects */
757 /* hot plug detectable */
758 #define DRM_CONNECTOR_POLL_HPD (1 << 0)
759 /* poll for connections */
760 #define DRM_CONNECTOR_POLL_CONNECT (1 << 1)
761 /* can cleanly poll for disconnections without flickering the screen */
762 /* DACs should rarely do this without a lot of testing */
763 #define DRM_CONNECTOR_POLL_DISCONNECT (1 << 2)
764
765 #define MAX_ELD_BYTES   128
766
767 /**
768  * struct drm_connector - central DRM connector control structure
769  * @dev: parent DRM device
770  * @port: OF node used by find connector by node.
771  * @kdev: kernel device for sysfs attributes
772  * @attr: sysfs attributes
773  * @head: list management
774  * @base: base KMS object
775  * @name: connector name
776  * @connector_type: one of the %DRM_MODE_CONNECTOR_<foo> types from drm_mode.h
777  * @connector_type_id: index into connector type enum
778  * @interlace_allowed: can this connector handle interlaced modes?
779  * @doublescan_allowed: can this connector handle doublescan?
780  * @stereo_allowed: can this connector handle stereo modes?
781  * @modes: modes available on this connector (from fill_modes() + user)
782  * @status: one of the drm_connector_status enums (connected, not, or unknown)
783  * @probed_modes: list of modes derived directly from the display
784  * @display_info: information about attached display (e.g. from EDID)
785  * @funcs: connector control functions
786  * @edid_blob_ptr: DRM property containing EDID if present
787  * @properties: property tracking for this connector
788  * @path_blob_ptr: DRM blob property data for the DP MST path property
789  * @polled: a %DRM_CONNECTOR_POLL_<foo> value for core driven polling
790  * @dpms: current dpms state
791  * @helper_private: mid-layer private data
792  * @cmdline_mode: mode line parsed from the kernel cmdline for this connector
793  * @force: a %DRM_FORCE_<foo> state for forced mode sets
794  * @override_edid: has the EDID been overwritten through debugfs for testing?
795  * @encoder_ids: valid encoders for this connector
796  * @encoder: encoder driving this connector, if any
797  * @eld: EDID-like data, if present
798  * @latency_present: AV delay info from ELD, if found
799  * @video_latency: video latency info from ELD, if found
800  * @audio_latency: audio latency info from ELD, if found
801  * @null_edid_counter: track sinks that give us all zeros for the EDID
802  * @bad_edid_counter: track sinks that give us an EDID with invalid checksum
803  * @edid_corrupt: indicates whether the last read EDID was corrupt
804  * @debugfs_entry: debugfs directory for this connector
805  * @state: current atomic state for this connector
806  * @has_tile: is this connector connected to a tiled monitor
807  * @tile_group: tile group for the connected monitor
808  * @tile_is_single_monitor: whether the tile is one monitor housing
809  * @num_h_tile: number of horizontal tiles in the tile group
810  * @num_v_tile: number of vertical tiles in the tile group
811  * @tile_h_loc: horizontal location of this tile
812  * @tile_v_loc: vertical location of this tile
813  * @tile_h_size: horizontal size of this tile.
814  * @tile_v_size: vertical size of this tile.
815  *
816  * Each connector may be connected to one or more CRTCs, or may be clonable by
817  * another connector if they can share a CRTC.  Each connector also has a specific
818  * position in the broader display (referred to as a 'screen' though it could
819  * span multiple monitors).
820  */
821 struct drm_connector {
822         struct drm_device *dev;
823         struct device_node *port;
824         struct device *kdev;
825         struct device_attribute *attr;
826         struct list_head head;
827
828         struct drm_mode_object base;
829
830         char *name;
831         int connector_type;
832         int connector_type_id;
833         bool interlace_allowed;
834         bool doublescan_allowed;
835         bool stereo_allowed;
836         struct list_head modes; /* list of modes on this connector */
837
838         enum drm_connector_status status;
839
840         /* these are modes added by probing with DDC or the BIOS */
841         struct list_head probed_modes;
842
843         struct drm_display_info display_info;
844         const struct drm_connector_funcs *funcs;
845
846         struct drm_property_blob *edid_blob_ptr;
847         struct drm_object_properties properties;
848
849         struct drm_property_blob *path_blob_ptr;
850
851         struct drm_property_blob *tile_blob_ptr;
852
853         uint8_t polled; /* DRM_CONNECTOR_POLL_* */
854
855         /* requested DPMS state */
856         int dpms;
857
858         const void *helper_private;
859
860         /* forced on connector */
861         struct drm_cmdline_mode cmdline_mode;
862         enum drm_connector_force force;
863         bool override_edid;
864         uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
865         struct drm_encoder *encoder; /* currently active encoder */
866         bool loader_protect;
867
868         /* EDID bits */
869         uint8_t eld[MAX_ELD_BYTES];
870         bool latency_present[2];
871         int video_latency[2];   /* [0]: progressive, [1]: interlaced */
872         int audio_latency[2];
873         int null_edid_counter; /* needed to workaround some HW bugs where we get all 0s */
874         unsigned bad_edid_counter;
875
876         /* Flag for raw EDID header corruption - used in Displayport
877          * compliance testing - * Displayport Link CTS Core 1.2 rev1.1 4.2.2.6
878          */
879         bool edid_corrupt;
880
881         struct dentry *debugfs_entry;
882
883         struct drm_connector_state *state;
884
885         /* DisplayID bits */
886         bool has_tile;
887         struct drm_tile_group *tile_group;
888         bool tile_is_single_monitor;
889
890         uint8_t num_h_tile, num_v_tile;
891         uint8_t tile_h_loc, tile_v_loc;
892         uint16_t tile_h_size, tile_v_size;
893 };
894
895 /**
896  * struct drm_plane_state - mutable plane state
897  * @plane: backpointer to the plane
898  * @crtc: currently bound CRTC, NULL if disabled
899  * @fb: currently bound framebuffer
900  * @fence: optional fence to wait for before scanning out @fb
901  * @crtc_x: left position of visible portion of plane on crtc
902  * @crtc_y: upper position of visible portion of plane on crtc
903  * @crtc_w: width of visible portion of plane on crtc
904  * @crtc_h: height of visible portion of plane on crtc
905  * @src_x: left position of visible portion of plane within
906  *      plane (in 16.16)
907  * @src_y: upper position of visible portion of plane within
908  *      plane (in 16.16)
909  * @src_w: width of visible portion of plane (in 16.16)
910  * @src_h: height of visible portion of plane (in 16.16)
911  * @state: backpointer to global drm_atomic_state
912  */
913 struct drm_plane_state {
914         struct drm_plane *plane;
915
916         struct drm_crtc *crtc;   /* do not write directly, use drm_atomic_set_crtc_for_plane() */
917         struct drm_framebuffer *fb;  /* do not write directly, use drm_atomic_set_fb_for_plane() */
918         struct fence *fence;
919
920         /* Signed dest location allows it to be partially off screen */
921         int32_t crtc_x, crtc_y;
922         uint32_t crtc_w, crtc_h;
923
924         /* Source values are 16.16 fixed point */
925         uint32_t src_x, src_y;
926         uint32_t src_h, src_w;
927
928         /* Plane rotation */
929         unsigned int rotation;
930
931         struct drm_atomic_state *state;
932 };
933
934
935 /**
936  * struct drm_plane_funcs - driver plane control functions
937  * @update_plane: update the plane configuration
938  * @disable_plane: shut down the plane
939  * @destroy: clean up plane resources
940  * @reset: reset plane after state has been invalidated (e.g. resume)
941  * @set_property: called when a property is changed
942  * @atomic_duplicate_state: duplicate the atomic state for this plane
943  * @atomic_destroy_state: destroy an atomic state for this plane
944  * @atomic_set_property: set a property on an atomic state for this plane
945  *    (do not call directly, use drm_atomic_plane_set_property())
946  * @atomic_get_property: get a property on an atomic state for this plane
947  *    (do not call directly, use drm_atomic_plane_get_property())
948  */
949 struct drm_plane_funcs {
950         int (*update_plane)(struct drm_plane *plane,
951                             struct drm_crtc *crtc, struct drm_framebuffer *fb,
952                             int crtc_x, int crtc_y,
953                             unsigned int crtc_w, unsigned int crtc_h,
954                             uint32_t src_x, uint32_t src_y,
955                             uint32_t src_w, uint32_t src_h);
956         int (*disable_plane)(struct drm_plane *plane);
957         void (*destroy)(struct drm_plane *plane);
958         void (*reset)(struct drm_plane *plane);
959
960         int (*set_property)(struct drm_plane *plane,
961                             struct drm_property *property, uint64_t val);
962
963         /* atomic update handling */
964         struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
965         void (*atomic_destroy_state)(struct drm_plane *plane,
966                                      struct drm_plane_state *state);
967         int (*atomic_set_property)(struct drm_plane *plane,
968                                    struct drm_plane_state *state,
969                                    struct drm_property *property,
970                                    uint64_t val);
971         int (*atomic_get_property)(struct drm_plane *plane,
972                                    const struct drm_plane_state *state,
973                                    struct drm_property *property,
974                                    uint64_t *val);
975         /**
976          * @late_register:
977          *
978          * This optional hook can be used to register additional userspace
979          * interfaces attached to the plane like debugfs interfaces.
980          * It is called late in the driver load sequence from drm_dev_register().
981          * Everything added from this callback should be unregistered in
982          * the early_unregister callback.
983          *
984          * Returns:
985          *
986          * 0 on success, or a negative error code on failure.
987          */
988         int (*late_register)(struct drm_plane *plane);
989
990         /**
991          * @early_unregister:
992          *
993          * This optional hook should be used to unregister the additional
994          * userspace interfaces attached to the plane from
995          * late_unregister(). It is called from drm_dev_unregister(),
996          * early in the driver unload sequence to disable userspace access
997          * before data structures are torndown.
998          */
999         void (*early_unregister)(struct drm_plane *plane);
1000 };
1001
1002 enum drm_plane_type {
1003         DRM_PLANE_TYPE_OVERLAY,
1004         DRM_PLANE_TYPE_PRIMARY,
1005         DRM_PLANE_TYPE_CURSOR,
1006 };
1007
1008 /**
1009  * struct drm_plane - central DRM plane control structure
1010  * @dev: DRM device this plane belongs to
1011  * @parent: this plane share some resources with parent plane.
1012  * @head: for list management
1013  * @base: base mode object
1014  * @possible_crtcs: pipes this plane can be bound to
1015  * @format_types: array of formats supported by this plane
1016  * @format_count: number of formats supported
1017  * @format_default: driver hasn't supplied supported formats for the plane
1018  * @crtc: currently bound CRTC
1019  * @fb: currently bound fb
1020  * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
1021  *      drm_mode_set_config_internal() to implement correct refcounting.
1022  * @funcs: helper functions
1023  * @properties: property tracking for this plane
1024  * @type: type of plane (overlay, primary, cursor)
1025  * @state: current atomic state for this plane
1026  */
1027 struct drm_plane {
1028         struct drm_device *dev;
1029         struct drm_plane *parent;
1030         struct list_head head;
1031
1032         struct drm_modeset_lock mutex;
1033
1034         struct drm_mode_object base;
1035
1036         uint32_t possible_crtcs;
1037         uint32_t *format_types;
1038         unsigned int format_count;
1039         bool format_default;
1040
1041         struct drm_crtc *crtc;
1042         struct drm_framebuffer *fb;
1043
1044         struct drm_framebuffer *old_fb;
1045
1046         const struct drm_plane_funcs *funcs;
1047
1048         struct drm_object_properties properties;
1049
1050         enum drm_plane_type type;
1051
1052         const void *helper_private;
1053
1054         struct drm_plane_state *state;
1055 };
1056
1057 /**
1058  * struct drm_bridge_funcs - drm_bridge control functions
1059  * @attach: Called during drm_bridge_attach
1060  * @mode_fixup: Try to fixup (or reject entirely) proposed mode for this bridge
1061  * @disable: Called right before encoder prepare, disables the bridge
1062  * @post_disable: Called right after encoder prepare, for lockstepped disable
1063  * @mode_set: Set this mode to the bridge
1064  * @pre_enable: Called right before encoder commit, for lockstepped commit
1065  * @enable: Called right after encoder commit, enables the bridge
1066  */
1067 struct drm_bridge_funcs {
1068         int (*attach)(struct drm_bridge *bridge);
1069         bool (*mode_fixup)(struct drm_bridge *bridge,
1070                            const struct drm_display_mode *mode,
1071                            struct drm_display_mode *adjusted_mode);
1072         void (*disable)(struct drm_bridge *bridge);
1073         void (*post_disable)(struct drm_bridge *bridge);
1074         void (*mode_set)(struct drm_bridge *bridge,
1075                          struct drm_display_mode *mode,
1076                          struct drm_display_mode *adjusted_mode);
1077         void (*pre_enable)(struct drm_bridge *bridge);
1078         void (*enable)(struct drm_bridge *bridge);
1079 };
1080
1081 /**
1082  * struct drm_bridge - central DRM bridge control structure
1083  * @dev: DRM device this bridge belongs to
1084  * @encoder: encoder to which this bridge is connected
1085  * @next: the next bridge in the encoder chain
1086  * @of_node: device node pointer to the bridge
1087  * @list: to keep track of all added bridges
1088  * @funcs: control functions
1089  * @driver_private: pointer to the bridge driver's internal context
1090  */
1091 struct drm_bridge {
1092         struct drm_device *dev;
1093         struct drm_encoder *encoder;
1094         struct drm_bridge *next;
1095 #ifdef CONFIG_OF
1096         struct device_node *of_node;
1097 #endif
1098         struct list_head list;
1099
1100         const struct drm_bridge_funcs *funcs;
1101         void *driver_private;
1102 };
1103
1104 /**
1105  * struct drm_atomic_state - the global state object for atomic updates
1106  * @dev: parent DRM device
1107  * @allow_modeset: allow full modeset
1108  * @legacy_cursor_update: hint to enforce legacy cursor ioctl semantics
1109  * @planes: pointer to array of plane pointers
1110  * @plane_states: pointer to array of plane states pointers
1111  * @crtcs: pointer to array of CRTC pointers
1112  * @crtc_states: pointer to array of CRTC states pointers
1113  * @num_connector: size of the @connectors and @connector_states arrays
1114  * @connectors: pointer to array of connector pointers
1115  * @connector_states: pointer to array of connector states pointers
1116  * @acquire_ctx: acquire context for this atomic modeset state update
1117  */
1118 struct drm_atomic_state {
1119         struct drm_device *dev;
1120         bool allow_modeset : 1;
1121         bool legacy_cursor_update : 1;
1122         struct drm_plane **planes;
1123         struct drm_plane_state **plane_states;
1124         struct drm_crtc **crtcs;
1125         struct drm_crtc_state **crtc_states;
1126         int num_connector;
1127         struct drm_connector **connectors;
1128         struct drm_connector_state **connector_states;
1129
1130         struct drm_modeset_acquire_ctx *acquire_ctx;
1131 };
1132
1133
1134 /**
1135  * struct drm_mode_set - new values for a CRTC config change
1136  * @fb: framebuffer to use for new config
1137  * @crtc: CRTC whose configuration we're about to change
1138  * @mode: mode timings to use
1139  * @x: position of this CRTC relative to @fb
1140  * @y: position of this CRTC relative to @fb
1141  * @connectors: array of connectors to drive with this CRTC if possible
1142  * @num_connectors: size of @connectors array
1143  *
1144  * Represents a single crtc the connectors that it drives with what mode
1145  * and from which framebuffer it scans out from.
1146  *
1147  * This is used to set modes.
1148  */
1149 struct drm_mode_set {
1150         struct drm_framebuffer *fb;
1151         struct drm_crtc *crtc;
1152         struct drm_display_mode *mode;
1153
1154         uint32_t x;
1155         uint32_t y;
1156
1157         struct drm_connector **connectors;
1158         size_t num_connectors;
1159 };
1160
1161 /**
1162  * struct drm_mode_config_funcs - basic driver provided mode setting functions
1163  * @fb_create: create a new framebuffer object
1164  * @output_poll_changed: function to handle output configuration changes
1165  * @atomic_check: check whether a given atomic state update is possible
1166  * @atomic_commit: commit an atomic state update previously verified with
1167  *      atomic_check()
1168  * @atomic_state_alloc: allocate a new atomic state
1169  * @atomic_state_clear: clear the atomic state
1170  * @atomic_state_free: free the atomic state
1171  *
1172  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
1173  * involve drivers.
1174  */
1175 struct drm_mode_config_funcs {
1176         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
1177                                              struct drm_file *file_priv,
1178                                              struct drm_mode_fb_cmd2 *mode_cmd);
1179         void (*output_poll_changed)(struct drm_device *dev);
1180
1181         int (*atomic_check)(struct drm_device *dev,
1182                             struct drm_atomic_state *a);
1183         int (*atomic_commit)(struct drm_device *dev,
1184                              struct drm_atomic_state *a,
1185                              bool async);
1186         struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev);
1187         void (*atomic_state_clear)(struct drm_atomic_state *state);
1188         void (*atomic_state_free)(struct drm_atomic_state *state);
1189 };
1190
1191 /**
1192  * struct drm_mode_config - Mode configuration control structure
1193  * @mutex: mutex protecting KMS related lists and structures
1194  * @connection_mutex: ww mutex protecting connector state and routing
1195  * @acquire_ctx: global implicit acquire context used by atomic drivers for
1196  *      legacy ioctls
1197  * @idr_mutex: mutex for KMS ID allocation and management
1198  * @crtc_idr: main KMS ID tracking object
1199  * @fb_lock: mutex to protect fb state and lists
1200  * @num_fb: number of fbs available
1201  * @fb_list: list of framebuffers available
1202  * @num_connector: number of connectors on this device
1203  * @connector_list: list of connector objects
1204  * @num_encoder: number of encoders on this device
1205  * @encoder_list: list of encoder objects
1206  * @num_overlay_plane: number of overlay planes on this device
1207  * @num_total_plane: number of universal (i.e. with primary/curso) planes on this device
1208  * @plane_list: list of plane objects
1209  * @num_crtc: number of CRTCs on this device
1210  * @crtc_list: list of CRTC objects
1211  * @property_list: list of property objects
1212  * @min_width: minimum pixel width on this device
1213  * @min_height: minimum pixel height on this device
1214  * @max_width: maximum pixel width on this device
1215  * @max_height: maximum pixel height on this device
1216  * @funcs: core driver provided mode setting functions
1217  * @fb_base: base address of the framebuffer
1218  * @poll_enabled: track polling support for this device
1219  * @poll_running: track polling status for this device
1220  * @output_poll_work: delayed work for polling in process context
1221  * @property_blob_list: list of all the blob property objects
1222  * @blob_lock: mutex for blob property allocation and management
1223  * @*_property: core property tracking
1224  * @degamma_lut_property: LUT used to convert the framebuffer's colors to linear
1225  *      gamma
1226  * @degamma_lut_size_property: size of the degamma LUT as supported by the
1227  *      driver (read-only)
1228  * @ctm_property: Matrix used to convert colors after the lookup in the
1229  *      degamma LUT
1230  * @gamma_lut_property: LUT used to convert the colors, after the CSC matrix, to
1231  *      the gamma space of the connected screen (read-only)
1232  * @gamma_lut_size_property: size of the gamma LUT as supported by the driver
1233  * @preferred_depth: preferred RBG pixel depth, used by fb helpers
1234  * @prefer_shadow: hint to userspace to prefer shadow-fb rendering
1235  * @async_page_flip: does this device support async flips on the primary plane?
1236  * @cursor_width: hint to userspace for max cursor width
1237  * @cursor_height: hint to userspace for max cursor height
1238  *
1239  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
1240  * enumerated by the driver are added here, as are global properties.  Some
1241  * global restrictions are also here, e.g. dimension restrictions.
1242  */
1243 struct drm_mode_config {
1244         struct mutex mutex; /* protects configuration (mode lists etc.) */
1245         struct drm_modeset_lock connection_mutex; /* protects connector->encoder and encoder->crtc links */
1246         struct drm_modeset_acquire_ctx *acquire_ctx; /* for legacy _lock_all() / _unlock_all() */
1247         struct mutex idr_mutex; /* for IDR management */
1248         struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
1249         struct idr tile_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
1250         /* this is limited to one for now */
1251
1252         struct mutex fb_lock; /* proctects global and per-file fb lists */
1253         int num_fb;
1254         struct list_head fb_list;
1255
1256         int num_connector;
1257         struct list_head connector_list;
1258         int num_encoder;
1259         struct list_head encoder_list;
1260
1261         /*
1262          * Track # of overlay planes separately from # of total planes.  By
1263          * default we only advertise overlay planes to userspace; if userspace
1264          * sets the "universal plane" capability bit, we'll go ahead and
1265          * expose all planes.
1266          */
1267         int num_overlay_plane;
1268         int num_total_plane;
1269         int num_share_plane;
1270         int num_share_overlay_plane;
1271         struct list_head plane_list;
1272
1273         int num_crtc;
1274         struct list_head crtc_list;
1275
1276         struct list_head property_list;
1277
1278         int min_width, min_height;
1279         int max_width, max_height;
1280         const struct drm_mode_config_funcs *funcs;
1281         resource_size_t fb_base;
1282
1283         /* output poll support */
1284         bool poll_enabled;
1285         bool poll_running;
1286         bool delayed_event;
1287         struct delayed_work output_poll_work;
1288
1289         struct mutex blob_lock;
1290
1291         /* pointers to share properties */
1292         struct drm_property *prop_share_id;
1293         struct drm_property *prop_share_flags;
1294
1295         /* pointers to standard properties */
1296         struct list_head property_blob_list;
1297         struct drm_property *edid_property;
1298         struct drm_property *dpms_property;
1299         struct drm_property *path_property;
1300         struct drm_property *tile_property;
1301         struct drm_property *plane_type_property;
1302         struct drm_property *rotation_property;
1303         struct drm_property *prop_src_x;
1304         struct drm_property *prop_src_y;
1305         struct drm_property *prop_src_w;
1306         struct drm_property *prop_src_h;
1307         struct drm_property *prop_crtc_x;
1308         struct drm_property *prop_crtc_y;
1309         struct drm_property *prop_crtc_w;
1310         struct drm_property *prop_crtc_h;
1311         struct drm_property *prop_fb_id;
1312         struct drm_property *prop_crtc_id;
1313         struct drm_property *prop_active;
1314         struct drm_property *prop_mode_id;
1315
1316         /* DVI-I properties */
1317         struct drm_property *dvi_i_subconnector_property;
1318         struct drm_property *dvi_i_select_subconnector_property;
1319
1320         /* TV properties */
1321         struct drm_property *tv_subconnector_property;
1322         struct drm_property *tv_select_subconnector_property;
1323         struct drm_property *tv_mode_property;
1324         struct drm_property *tv_left_margin_property;
1325         struct drm_property *tv_right_margin_property;
1326         struct drm_property *tv_top_margin_property;
1327         struct drm_property *tv_bottom_margin_property;
1328         struct drm_property *tv_brightness_property;
1329         struct drm_property *tv_contrast_property;
1330         struct drm_property *tv_flicker_reduction_property;
1331         struct drm_property *tv_overscan_property;
1332         struct drm_property *tv_saturation_property;
1333         struct drm_property *tv_hue_property;
1334
1335         /* Optional properties */
1336         struct drm_property *scaling_mode_property;
1337         struct drm_property *aspect_ratio_property;
1338         struct drm_property *dirty_info_property;
1339
1340         /* Optional color correction properties */
1341         struct drm_property *degamma_lut_property;
1342         struct drm_property *degamma_lut_size_property;
1343         struct drm_property *ctm_property;
1344         struct drm_property *gamma_lut_property;
1345         struct drm_property *gamma_lut_size_property;
1346
1347         /* properties for virtual machine layout */
1348         struct drm_property *suggested_x_property;
1349         struct drm_property *suggested_y_property;
1350
1351         /* dumb ioctl parameters */
1352         uint32_t preferred_depth, prefer_shadow;
1353
1354         /* whether async page flip is supported or not */
1355         bool async_page_flip;
1356
1357         /* whether the driver supports fb modifiers */
1358         bool allow_fb_modifiers;
1359
1360         /* cursor size */
1361         uint32_t cursor_width, cursor_height;
1362 };
1363
1364 /**
1365  * drm_for_each_plane_mask - iterate over planes specified by bitmask
1366  * @plane: the loop cursor
1367  * @dev: the DRM device
1368  * @plane_mask: bitmask of plane indices
1369  *
1370  * Iterate over all planes specified by bitmask.
1371  */
1372 #define drm_for_each_plane_mask(plane, dev, plane_mask) \
1373         list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
1374                 if ((plane_mask) & (1 << drm_plane_index(plane)))
1375
1376
1377 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
1378 #define obj_to_connector(x) container_of(x, struct drm_connector, base)
1379 #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
1380 #define obj_to_mode(x) container_of(x, struct drm_display_mode, base)
1381 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
1382 #define obj_to_property(x) container_of(x, struct drm_property, base)
1383 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
1384 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
1385
1386 struct drm_prop_enum_list {
1387         int type;
1388         char *name;
1389 };
1390
1391 extern __printf(6, 7)
1392 int drm_crtc_init_with_planes(struct drm_device *dev,
1393                               struct drm_crtc *crtc,
1394                               struct drm_plane *primary,
1395                               struct drm_plane *cursor,
1396                               const struct drm_crtc_funcs *funcs,
1397                               const char *name, ...);
1398 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
1399 extern unsigned int drm_crtc_index(struct drm_crtc *crtc);
1400
1401 /**
1402  * drm_crtc_mask - find the mask of a registered CRTC
1403  * @crtc: CRTC to find mask for
1404  *
1405  * Given a registered CRTC, return the mask bit of that CRTC for an
1406  * encoder's possible_crtcs field.
1407  */
1408 static inline uint32_t drm_crtc_mask(struct drm_crtc *crtc)
1409 {
1410         return 1 << drm_crtc_index(crtc);
1411 }
1412
1413 extern void drm_connector_ida_init(void);
1414 extern void drm_connector_ida_destroy(void);
1415 extern int drm_connector_init(struct drm_device *dev,
1416                               struct drm_connector *connector,
1417                               const struct drm_connector_funcs *funcs,
1418                               int connector_type);
1419 int drm_connector_register(struct drm_connector *connector);
1420 void drm_connector_unregister(struct drm_connector *connector);
1421
1422 extern void drm_connector_cleanup(struct drm_connector *connector);
1423 extern unsigned int drm_connector_index(struct drm_connector *connector);
1424
1425 /* helpers to {un}register all connectors from sysfs for device */
1426 extern int drm_connector_register_all(struct drm_device *dev);
1427 extern void drm_connector_unregister_all(struct drm_device *dev);
1428
1429 extern int drm_bridge_add(struct drm_bridge *bridge);
1430 extern void drm_bridge_remove(struct drm_bridge *bridge);
1431 extern struct drm_bridge *of_drm_find_bridge(struct device_node *np);
1432 extern int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge);
1433
1434 bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
1435                         const struct drm_display_mode *mode,
1436                         struct drm_display_mode *adjusted_mode);
1437 void drm_bridge_disable(struct drm_bridge *bridge);
1438 void drm_bridge_post_disable(struct drm_bridge *bridge);
1439 void drm_bridge_mode_set(struct drm_bridge *bridge,
1440                         struct drm_display_mode *mode,
1441                         struct drm_display_mode *adjusted_mode);
1442 void drm_bridge_pre_enable(struct drm_bridge *bridge);
1443 void drm_bridge_enable(struct drm_bridge *bridge);
1444
1445 extern __printf(5, 6)
1446 int drm_encoder_init(struct drm_device *dev,
1447                      struct drm_encoder *encoder,
1448                      const struct drm_encoder_funcs *funcs,
1449                      int encoder_type, const char *name, ...);
1450
1451 /**
1452  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
1453  * @encoder: encoder to test
1454  * @crtc: crtc to test
1455  *
1456  * Return false if @encoder can't be driven by @crtc, true otherwise.
1457  */
1458 static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
1459                                        struct drm_crtc *crtc)
1460 {
1461         return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
1462 }
1463
1464 extern __printf(8, 9)
1465 int drm_universal_plane_init(struct drm_device *dev,
1466                              struct drm_plane *plane,
1467                              unsigned long possible_crtcs,
1468                              const struct drm_plane_funcs *funcs,
1469                              const uint32_t *formats,
1470                              unsigned int format_count,
1471                              enum drm_plane_type type,
1472                              const char *name, ...);
1473 extern int drm_plane_init(struct drm_device *dev,
1474                           struct drm_plane *plane,
1475                           unsigned long possible_crtcs,
1476                           const struct drm_plane_funcs *funcs,
1477                           const uint32_t *formats, unsigned int format_count,
1478                           bool is_primary);
1479 extern int drm_share_plane_init(struct drm_device *dev, struct drm_plane *plane,
1480                                 struct drm_plane *parent,
1481                                 unsigned long possible_crtcs,
1482                                 const struct drm_plane_funcs *funcs,
1483                                 const uint32_t *formats,
1484                                 unsigned int format_count,
1485                                 enum drm_plane_type type);
1486 extern void drm_plane_cleanup(struct drm_plane *plane);
1487 extern unsigned int drm_plane_index(struct drm_plane *plane);
1488 extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
1489 extern void drm_plane_force_disable(struct drm_plane *plane);
1490 extern int drm_plane_check_pixel_format(const struct drm_plane *plane,
1491                                         u32 format);
1492 extern void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1493                                    int *hdisplay, int *vdisplay);
1494 extern int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1495                                    int x, int y,
1496                                    const struct drm_display_mode *mode,
1497                                    const struct drm_framebuffer *fb);
1498
1499 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
1500
1501 extern const char *drm_get_connector_status_name(enum drm_connector_status status);
1502 extern const char *drm_get_subpixel_order_name(enum subpixel_order order);
1503 extern const char *drm_get_dpms_name(int val);
1504 extern const char *drm_get_dvi_i_subconnector_name(int val);
1505 extern const char *drm_get_dvi_i_select_name(int val);
1506 extern const char *drm_get_tv_subconnector_name(int val);
1507 extern const char *drm_get_tv_select_name(int val);
1508 extern const char *drm_get_connector_name(int val);
1509 extern void drm_fb_release(struct drm_file *file_priv);
1510 extern void drm_property_destroy_user_blobs(struct drm_device *dev,
1511                                             struct drm_file *file_priv);
1512 extern bool drm_probe_ddc(struct i2c_adapter *adapter);
1513 extern struct edid *drm_get_edid(struct drm_connector *connector,
1514                                  struct i2c_adapter *adapter);
1515 extern struct edid *drm_edid_duplicate(const struct edid *edid);
1516 extern int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
1517 extern void drm_mode_config_init(struct drm_device *dev);
1518 extern void drm_mode_config_reset(struct drm_device *dev);
1519 extern void drm_mode_config_cleanup(struct drm_device *dev);
1520
1521 extern int drm_mode_connector_set_path_property(struct drm_connector *connector,
1522                                                 const char *path);
1523 int drm_mode_connector_set_tile_property(struct drm_connector *connector);
1524 extern int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1525                                                    const struct edid *edid);
1526
1527 extern int drm_display_info_set_bus_formats(struct drm_display_info *info,
1528                                             const u32 *formats,
1529                                             unsigned int num_formats);
1530
1531 static inline bool drm_property_type_is(struct drm_property *property,
1532                 uint32_t type)
1533 {
1534         /* instanceof for props.. handles extended type vs original types: */
1535         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
1536                 return (property->flags & DRM_MODE_PROP_EXTENDED_TYPE) == type;
1537         return property->flags & type;
1538 }
1539
1540 static inline bool drm_property_type_valid(struct drm_property *property)
1541 {
1542         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
1543                 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
1544         return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
1545 }
1546
1547 extern int drm_object_property_set_value(struct drm_mode_object *obj,
1548                                          struct drm_property *property,
1549                                          uint64_t val);
1550 extern int drm_object_property_get_value(struct drm_mode_object *obj,
1551                                          struct drm_property *property,
1552                                          uint64_t *value);
1553 extern int drm_framebuffer_init(struct drm_device *dev,
1554                                 struct drm_framebuffer *fb,
1555                                 const struct drm_framebuffer_funcs *funcs);
1556 extern struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
1557                                                       uint32_t id);
1558 extern void drm_framebuffer_unreference(struct drm_framebuffer *fb);
1559 extern void drm_framebuffer_reference(struct drm_framebuffer *fb);
1560 extern void drm_framebuffer_remove(struct drm_framebuffer *fb);
1561 extern void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
1562 extern void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
1563
1564 extern void drm_object_attach_property(struct drm_mode_object *obj,
1565                                        struct drm_property *property,
1566                                        uint64_t init_val);
1567 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1568                                                 const char *name, int num_values);
1569 extern struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
1570                                          const char *name,
1571                                          const struct drm_prop_enum_list *props,
1572                                          int num_values);
1573 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
1574                                          int flags, const char *name,
1575                                          const struct drm_prop_enum_list *props,
1576                                          int num_props,
1577                                          uint64_t supported_bits);
1578 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
1579                                          const char *name,
1580                                          uint64_t min, uint64_t max);
1581 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
1582                                          int flags, const char *name,
1583                                          int64_t min, int64_t max);
1584 struct drm_property *drm_property_create_object(struct drm_device *dev,
1585                                          int flags, const char *name, uint32_t type);
1586 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
1587                                          const char *name);
1588 struct drm_property_blob *drm_property_create_blob(struct drm_device *dev,
1589                                                    size_t length,
1590                                                    const void *data);
1591 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
1592                                                    uint32_t id);
1593 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob);
1594 void drm_property_unreference_blob(struct drm_property_blob *blob);
1595 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
1596 extern int drm_property_add_enum(struct drm_property *property, int index,
1597                                  uint64_t value, const char *name);
1598 extern int drm_mode_create_dvi_i_properties(struct drm_device *dev);
1599 extern int drm_mode_create_tv_properties(struct drm_device *dev,
1600                                          unsigned int num_modes,
1601                                          const char * const modes[]);
1602 extern int drm_mode_create_scaling_mode_property(struct drm_device *dev);
1603 extern int drm_mode_create_aspect_ratio_property(struct drm_device *dev);
1604 extern int drm_mode_create_dirty_info_property(struct drm_device *dev);
1605 extern int drm_mode_create_suggested_offset_properties(struct drm_device *dev);
1606 extern bool drm_property_change_valid_get(struct drm_property *property,
1607                                          uint64_t value, struct drm_mode_object **ref);
1608 extern void drm_property_change_valid_put(struct drm_property *property,
1609                 struct drm_mode_object *ref);
1610
1611 extern int drm_mode_connector_attach_encoder(struct drm_connector *connector,
1612                                              struct drm_encoder *encoder);
1613 extern int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1614                                          int gamma_size);
1615 extern struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
1616                 uint32_t id, uint32_t type);
1617
1618 /* IOCTLs */
1619 extern int drm_mode_getresources(struct drm_device *dev,
1620                                  void *data, struct drm_file *file_priv);
1621 extern int drm_mode_getplane_res(struct drm_device *dev, void *data,
1622                                    struct drm_file *file_priv);
1623 extern int drm_mode_getcrtc(struct drm_device *dev,
1624                             void *data, struct drm_file *file_priv);
1625 extern int drm_mode_getconnector(struct drm_device *dev,
1626                               void *data, struct drm_file *file_priv);
1627 extern int drm_mode_set_config_internal(struct drm_mode_set *set);
1628 extern int drm_mode_setcrtc(struct drm_device *dev,
1629                             void *data, struct drm_file *file_priv);
1630 extern int drm_mode_getplane(struct drm_device *dev,
1631                                void *data, struct drm_file *file_priv);
1632 extern int drm_mode_setplane(struct drm_device *dev,
1633                                void *data, struct drm_file *file_priv);
1634 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
1635                                 void *data, struct drm_file *file_priv);
1636 extern int drm_mode_cursor2_ioctl(struct drm_device *dev,
1637                                 void *data, struct drm_file *file_priv);
1638 extern int drm_mode_addfb(struct drm_device *dev,
1639                           void *data, struct drm_file *file_priv);
1640 extern int drm_mode_addfb2(struct drm_device *dev,
1641                            void *data, struct drm_file *file_priv);
1642 extern uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
1643 extern int drm_mode_rmfb(struct drm_device *dev,
1644                          void *data, struct drm_file *file_priv);
1645 extern int drm_mode_getfb(struct drm_device *dev,
1646                           void *data, struct drm_file *file_priv);
1647 extern int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1648                                   void *data, struct drm_file *file_priv);
1649
1650 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
1651                                       void *data, struct drm_file *file_priv);
1652 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
1653                                   void *data, struct drm_file *file_priv);
1654 extern int drm_mode_createblob_ioctl(struct drm_device *dev,
1655                                      void *data, struct drm_file *file_priv);
1656 extern int drm_mode_destroyblob_ioctl(struct drm_device *dev,
1657                                       void *data, struct drm_file *file_priv);
1658 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1659                                               void *data, struct drm_file *file_priv);
1660 extern int drm_mode_getencoder(struct drm_device *dev,
1661                                void *data, struct drm_file *file_priv);
1662 extern int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1663                                     void *data, struct drm_file *file_priv);
1664 extern int drm_mode_gamma_set_ioctl(struct drm_device *dev,
1665                                     void *data, struct drm_file *file_priv);
1666 extern u8 drm_match_cea_mode(const struct drm_display_mode *to_match);
1667 extern enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code);
1668 extern bool drm_detect_hdmi_monitor(struct edid *edid);
1669 extern bool drm_detect_monitor_audio(struct edid *edid);
1670 extern bool drm_rgb_quant_range_selectable(struct edid *edid);
1671 extern int drm_mode_page_flip_ioctl(struct drm_device *dev,
1672                                     void *data, struct drm_file *file_priv);
1673 extern int drm_add_modes_noedid(struct drm_connector *connector,
1674                                 int hdisplay, int vdisplay);
1675 extern void drm_set_preferred_mode(struct drm_connector *connector,
1676                                    int hpref, int vpref);
1677
1678 extern int drm_edid_header_is_valid(const u8 *raw_edid);
1679 extern bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid,
1680                                  bool *edid_corrupt);
1681 extern bool drm_edid_is_valid(struct edid *edid);
1682
1683 extern struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1684                                                          char topology[8]);
1685 extern struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1686                                                char topology[8]);
1687 extern void drm_mode_put_tile_group(struct drm_device *dev,
1688                                    struct drm_tile_group *tg);
1689 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
1690                                            int hsize, int vsize, int fresh,
1691                                            bool rb);
1692
1693 extern int drm_mode_create_dumb_ioctl(struct drm_device *dev,
1694                                       void *data, struct drm_file *file_priv);
1695 extern int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1696                                     void *data, struct drm_file *file_priv);
1697 extern int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1698                                       void *data, struct drm_file *file_priv);
1699 extern int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
1700                                              struct drm_file *file_priv);
1701 extern int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
1702                                            struct drm_file *file_priv);
1703 extern int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
1704                                        struct drm_property *property,
1705                                        uint64_t value);
1706 extern int drm_mode_atomic_ioctl(struct drm_device *dev,
1707                                  void *data, struct drm_file *file_priv);
1708
1709 extern void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
1710                                  int *bpp);
1711 extern int drm_format_num_planes(uint32_t format);
1712 extern int drm_format_plane_bpp(uint32_t format, int plane);
1713 extern int drm_format_plane_cpp(uint32_t format, int plane);
1714 extern int drm_format_horz_chroma_subsampling(uint32_t format);
1715 extern int drm_format_vert_chroma_subsampling(uint32_t format);
1716 extern const char *drm_get_format_name(uint32_t format);
1717 extern struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
1718                                                               unsigned int supported_rotations);
1719 extern unsigned int drm_rotation_simplify(unsigned int rotation,
1720                                           unsigned int supported_rotations);
1721
1722 /* Helpers */
1723
1724 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
1725                 uint32_t id)
1726 {
1727         struct drm_mode_object *mo;
1728         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
1729         return mo ? obj_to_plane(mo) : NULL;
1730 }
1731
1732 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
1733         uint32_t id)
1734 {
1735         struct drm_mode_object *mo;
1736         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CRTC);
1737         return mo ? obj_to_crtc(mo) : NULL;
1738 }
1739
1740 static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
1741         uint32_t id)
1742 {
1743         struct drm_mode_object *mo;
1744         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
1745         return mo ? obj_to_encoder(mo) : NULL;
1746 }
1747
1748 static inline struct drm_connector *drm_connector_find(struct drm_device *dev,
1749                 uint32_t id)
1750 {
1751         struct drm_mode_object *mo;
1752         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CONNECTOR);
1753         return mo ? obj_to_connector(mo) : NULL;
1754 }
1755
1756 static inline struct drm_property *drm_property_find(struct drm_device *dev,
1757                 uint32_t id)
1758 {
1759         struct drm_mode_object *mo;
1760         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PROPERTY);
1761         return mo ? obj_to_property(mo) : NULL;
1762 }
1763
1764 /*
1765  * Extract a degamma/gamma LUT value provided by user and round it to the
1766  * precision supported by the hardware.
1767  */
1768 static inline uint32_t drm_color_lut_extract(uint32_t user_input,
1769                                              uint32_t bit_precision)
1770 {
1771         uint32_t val = user_input + (1 << (16 - bit_precision - 1));
1772         uint32_t max = 0xffff >> (16 - bit_precision);
1773
1774         val >>= 16 - bit_precision;
1775
1776         return clamp_val(val, 0, max);
1777 }
1778
1779 /* Plane list iterator for legacy (overlay only) planes. */
1780 #define drm_for_each_legacy_plane(plane, dev) \
1781         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
1782                 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1783
1784 #define drm_for_each_plane(plane, dev) \
1785         list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
1786
1787 #define drm_for_each_crtc(crtc, dev) \
1788         list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
1789
1790 static inline void
1791 assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
1792 {
1793         /*
1794          * The connector hotadd/remove code currently grabs both locks when
1795          * updating lists. Hence readers need only hold either of them to be
1796          * safe and the check amounts to
1797          *
1798          * WARN_ON(not_holding(A) && not_holding(B)).
1799          */
1800         WARN_ON(!mutex_is_locked(&mode_config->mutex) &&
1801                 !drm_modeset_is_locked(&mode_config->connection_mutex));
1802 }
1803
1804 #define drm_for_each_connector(connector, dev) \
1805         for (assert_drm_connector_list_read_locked(&(dev)->mode_config),        \
1806              connector = list_first_entry(&(dev)->mode_config.connector_list,   \
1807                                           struct drm_connector, head);          \
1808              &connector->head != (&(dev)->mode_config.connector_list);          \
1809              connector = list_next_entry(connector, head))
1810
1811 #define drm_for_each_encoder(encoder, dev) \
1812         list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
1813
1814 #define drm_for_each_fb(fb, dev) \
1815         for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),            \
1816              fb = list_first_entry(&(dev)->mode_config.fb_list, \
1817                                           struct drm_framebuffer, head);        \
1818              &fb->head != (&(dev)->mode_config.fb_list);                        \
1819              fb = list_next_entry(fb, head))
1820
1821 #endif /* __DRM_CRTC_H__ */