drm: add object property type
[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 <drm/drm_mode.h>
35 #include <drm/drm_fourcc.h>
36
37 struct drm_device;
38 struct drm_mode_set;
39 struct drm_framebuffer;
40 struct drm_object_properties;
41 struct drm_file;
42 struct drm_clip_rect;
43
44 #define DRM_MODE_OBJECT_CRTC 0xcccccccc
45 #define DRM_MODE_OBJECT_CONNECTOR 0xc0c0c0c0
46 #define DRM_MODE_OBJECT_ENCODER 0xe0e0e0e0
47 #define DRM_MODE_OBJECT_MODE 0xdededede
48 #define DRM_MODE_OBJECT_PROPERTY 0xb0b0b0b0
49 #define DRM_MODE_OBJECT_FB 0xfbfbfbfb
50 #define DRM_MODE_OBJECT_BLOB 0xbbbbbbbb
51 #define DRM_MODE_OBJECT_PLANE 0xeeeeeeee
52 #define DRM_MODE_OBJECT_BRIDGE 0xbdbdbdbd
53 #define DRM_MODE_OBJECT_ANY 0
54
55 struct drm_mode_object {
56         uint32_t id;
57         uint32_t type;
58         struct drm_object_properties *properties;
59 };
60
61 #define DRM_OBJECT_MAX_PROPERTY 24
62 struct drm_object_properties {
63         int count;
64         uint32_t ids[DRM_OBJECT_MAX_PROPERTY];
65         uint64_t values[DRM_OBJECT_MAX_PROPERTY];
66 };
67
68 enum drm_connector_force {
69         DRM_FORCE_UNSPECIFIED,
70         DRM_FORCE_OFF,
71         DRM_FORCE_ON,         /* force on analog part normally */
72         DRM_FORCE_ON_DIGITAL, /* for DVI-I use digital connector */
73 };
74
75 #include <drm/drm_modes.h>
76
77 enum drm_connector_status {
78         connector_status_connected = 1,
79         connector_status_disconnected = 2,
80         connector_status_unknown = 3,
81 };
82
83 enum subpixel_order {
84         SubPixelUnknown = 0,
85         SubPixelHorizontalRGB,
86         SubPixelHorizontalBGR,
87         SubPixelVerticalRGB,
88         SubPixelVerticalBGR,
89         SubPixelNone,
90 };
91
92 #define DRM_COLOR_FORMAT_RGB444         (1<<0)
93 #define DRM_COLOR_FORMAT_YCRCB444       (1<<1)
94 #define DRM_COLOR_FORMAT_YCRCB422       (1<<2)
95 /*
96  * Describes a given display (e.g. CRT or flat panel) and its limitations.
97  */
98 struct drm_display_info {
99         char name[DRM_DISPLAY_INFO_LEN];
100
101         /* Physical size */
102         unsigned int width_mm;
103         unsigned int height_mm;
104
105         /* Clock limits FIXME: storage format */
106         unsigned int min_vfreq, max_vfreq;
107         unsigned int min_hfreq, max_hfreq;
108         unsigned int pixel_clock;
109         unsigned int bpc;
110
111         enum subpixel_order subpixel_order;
112         u32 color_formats;
113
114         u8 cea_rev;
115 };
116
117 struct drm_framebuffer_funcs {
118         /* note: use drm_framebuffer_remove() */
119         void (*destroy)(struct drm_framebuffer *framebuffer);
120         int (*create_handle)(struct drm_framebuffer *fb,
121                              struct drm_file *file_priv,
122                              unsigned int *handle);
123         /**
124          * Optinal callback for the dirty fb ioctl.
125          *
126          * Userspace can notify the driver via this callback
127          * that a area of the framebuffer has changed and should
128          * be flushed to the display hardware.
129          *
130          * See documentation in drm_mode.h for the struct
131          * drm_mode_fb_dirty_cmd for more information as all
132          * the semantics and arguments have a one to one mapping
133          * on this function.
134          */
135         int (*dirty)(struct drm_framebuffer *framebuffer,
136                      struct drm_file *file_priv, unsigned flags,
137                      unsigned color, struct drm_clip_rect *clips,
138                      unsigned num_clips);
139 };
140
141 struct drm_framebuffer {
142         struct drm_device *dev;
143         /*
144          * Note that the fb is refcounted for the benefit of driver internals,
145          * for example some hw, disabling a CRTC/plane is asynchronous, and
146          * scanout does not actually complete until the next vblank.  So some
147          * cleanup (like releasing the reference(s) on the backing GEM bo(s))
148          * should be deferred.  In cases like this, the driver would like to
149          * hold a ref to the fb even though it has already been removed from
150          * userspace perspective.
151          */
152         struct kref refcount;
153         /*
154          * Place on the dev->mode_config.fb_list, access protected by
155          * dev->mode_config.fb_lock.
156          */
157         struct list_head head;
158         struct drm_mode_object base;
159         const struct drm_framebuffer_funcs *funcs;
160         unsigned int pitches[4];
161         unsigned int offsets[4];
162         unsigned int width;
163         unsigned int height;
164         /* depth can be 15 or 16 */
165         unsigned int depth;
166         int bits_per_pixel;
167         int flags;
168         uint32_t pixel_format; /* fourcc format */
169         struct list_head filp_head;
170         /* if you are using the helper */
171         void *helper_private;
172 };
173
174 struct drm_property_blob {
175         struct drm_mode_object base;
176         struct list_head head;
177         unsigned int length;
178         unsigned char data[];
179 };
180
181 struct drm_property_enum {
182         uint64_t value;
183         struct list_head head;
184         char name[DRM_PROP_NAME_LEN];
185 };
186
187 struct drm_property {
188         struct list_head head;
189         struct drm_mode_object base;
190         uint32_t flags;
191         char name[DRM_PROP_NAME_LEN];
192         uint32_t num_values;
193         uint64_t *values;
194         struct drm_device *dev;
195
196         struct list_head enum_blob_list;
197 };
198
199 struct drm_crtc;
200 struct drm_connector;
201 struct drm_encoder;
202 struct drm_pending_vblank_event;
203 struct drm_plane;
204 struct drm_bridge;
205
206 /**
207  * drm_crtc_funcs - control CRTCs for a given device
208  * @save: save CRTC state
209  * @restore: restore CRTC state
210  * @reset: reset CRTC after state has been invalidated (e.g. resume)
211  * @cursor_set: setup the cursor
212  * @cursor_move: move the cursor
213  * @gamma_set: specify color ramp for CRTC
214  * @destroy: deinit and free object
215  * @set_property: called when a property is changed
216  * @set_config: apply a new CRTC configuration
217  * @page_flip: initiate a page flip
218  *
219  * The drm_crtc_funcs structure is the central CRTC management structure
220  * in the DRM.  Each CRTC controls one or more connectors (note that the name
221  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
222  * connectors, not just CRTs).
223  *
224  * Each driver is responsible for filling out this structure at startup time,
225  * in addition to providing other modesetting features, like i2c and DDC
226  * bus accessors.
227  */
228 struct drm_crtc_funcs {
229         /* Save CRTC state */
230         void (*save)(struct drm_crtc *crtc); /* suspend? */
231         /* Restore CRTC state */
232         void (*restore)(struct drm_crtc *crtc); /* resume? */
233         /* Reset CRTC state */
234         void (*reset)(struct drm_crtc *crtc);
235
236         /* cursor controls */
237         int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
238                           uint32_t handle, uint32_t width, uint32_t height);
239         int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
240                            uint32_t handle, uint32_t width, uint32_t height,
241                            int32_t hot_x, int32_t hot_y);
242         int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
243
244         /* Set gamma on the CRTC */
245         void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
246                           uint32_t start, uint32_t size);
247         /* Object destroy routine */
248         void (*destroy)(struct drm_crtc *crtc);
249
250         int (*set_config)(struct drm_mode_set *set);
251
252         /*
253          * Flip to the given framebuffer.  This implements the page
254          * flip ioctl described in drm_mode.h, specifically, the
255          * implementation must return immediately and block all
256          * rendering to the current fb until the flip has completed.
257          * If userspace set the event flag in the ioctl, the event
258          * argument will point to an event to send back when the flip
259          * completes, otherwise it will be NULL.
260          */
261         int (*page_flip)(struct drm_crtc *crtc,
262                          struct drm_framebuffer *fb,
263                          struct drm_pending_vblank_event *event,
264                          uint32_t flags);
265
266         int (*set_property)(struct drm_crtc *crtc,
267                             struct drm_property *property, uint64_t val);
268 };
269
270 /**
271  * drm_crtc - central CRTC control structure
272  * @dev: parent DRM device
273  * @head: list management
274  * @base: base KMS object for ID tracking etc.
275  * @primary: primary plane for this CRTC
276  * @cursor: cursor plane for this CRTC
277  * @enabled: is this CRTC enabled?
278  * @mode: current mode timings
279  * @hwmode: mode timings as programmed to hw regs
280  * @invert_dimensions: for purposes of error checking crtc vs fb sizes,
281  *    invert the width/height of the crtc.  This is used if the driver
282  *    is performing 90 or 270 degree rotated scanout
283  * @x: x position on screen
284  * @y: y position on screen
285  * @funcs: CRTC control functions
286  * @gamma_size: size of gamma ramp
287  * @gamma_store: gamma ramp values
288  * @framedur_ns: precise frame timing
289  * @framedur_ns: precise line timing
290  * @pixeldur_ns: precise pixel timing
291  * @helper_private: mid-layer private data
292  * @properties: property tracking for this CRTC
293  *
294  * Each CRTC may have one or more connectors associated with it.  This structure
295  * allows the CRTC to be controlled.
296  */
297 struct drm_crtc {
298         struct drm_device *dev;
299         struct list_head head;
300
301         /**
302          * crtc mutex
303          *
304          * This provides a read lock for the overall crtc state (mode, dpms
305          * state, ...) and a write lock for everything which can be update
306          * without a full modeset (fb, cursor data, ...)
307          */
308         struct mutex mutex;
309
310         struct drm_mode_object base;
311
312         /* primary and cursor planes for CRTC */
313         struct drm_plane *primary;
314         struct drm_plane *cursor;
315
316         /* Temporary tracking of the old fb while a modeset is ongoing. Used
317          * by drm_mode_set_config_internal to implement correct refcounting. */
318         struct drm_framebuffer *old_fb;
319
320         bool enabled;
321
322         /* Requested mode from modesetting. */
323         struct drm_display_mode mode;
324
325         /* Programmed mode in hw, after adjustments for encoders,
326          * crtc, panel scaling etc. Needed for timestamping etc.
327          */
328         struct drm_display_mode hwmode;
329
330         bool invert_dimensions;
331
332         int x, y;
333         const struct drm_crtc_funcs *funcs;
334
335         /* CRTC gamma size for reporting to userspace */
336         uint32_t gamma_size;
337         uint16_t *gamma_store;
338
339         /* Constants needed for precise vblank and swap timestamping. */
340         int framedur_ns, linedur_ns, pixeldur_ns;
341
342         /* if you are using the helper */
343         void *helper_private;
344
345         struct drm_object_properties properties;
346 };
347
348
349 /**
350  * drm_connector_funcs - control connectors on a given device
351  * @dpms: set power state (see drm_crtc_funcs above)
352  * @save: save connector state
353  * @restore: restore connector state
354  * @reset: reset connector after state has been invalidated (e.g. resume)
355  * @detect: is this connector active?
356  * @fill_modes: fill mode list for this connector
357  * @set_property: property for this connector may need an update
358  * @destroy: make object go away
359  * @force: notify the driver that the connector is forced on
360  *
361  * Each CRTC may have one or more connectors attached to it.  The functions
362  * below allow the core DRM code to control connectors, enumerate available modes,
363  * etc.
364  */
365 struct drm_connector_funcs {
366         void (*dpms)(struct drm_connector *connector, int mode);
367         void (*save)(struct drm_connector *connector);
368         void (*restore)(struct drm_connector *connector);
369         void (*reset)(struct drm_connector *connector);
370
371         /* Check to see if anything is attached to the connector.
372          * @force is set to false whilst polling, true when checking the
373          * connector due to user request. @force can be used by the driver
374          * to avoid expensive, destructive operations during automated
375          * probing.
376          */
377         enum drm_connector_status (*detect)(struct drm_connector *connector,
378                                             bool force);
379         int (*fill_modes)(struct drm_connector *connector, uint32_t max_width, uint32_t max_height);
380         int (*set_property)(struct drm_connector *connector, struct drm_property *property,
381                              uint64_t val);
382         void (*destroy)(struct drm_connector *connector);
383         void (*force)(struct drm_connector *connector);
384 };
385
386 /**
387  * drm_encoder_funcs - encoder controls
388  * @reset: reset state (e.g. at init or resume time)
389  * @destroy: cleanup and free associated data
390  *
391  * Encoders sit between CRTCs and connectors.
392  */
393 struct drm_encoder_funcs {
394         void (*reset)(struct drm_encoder *encoder);
395         void (*destroy)(struct drm_encoder *encoder);
396 };
397
398 #define DRM_CONNECTOR_MAX_ENCODER 3
399
400 /**
401  * drm_encoder - central DRM encoder structure
402  * @dev: parent DRM device
403  * @head: list management
404  * @base: base KMS object
405  * @name: encoder name
406  * @encoder_type: one of the %DRM_MODE_ENCODER_<foo> types in drm_mode.h
407  * @possible_crtcs: bitmask of potential CRTC bindings
408  * @possible_clones: bitmask of potential sibling encoders for cloning
409  * @crtc: currently bound CRTC
410  * @bridge: bridge associated to the encoder
411  * @funcs: control functions
412  * @helper_private: mid-layer private data
413  *
414  * CRTCs drive pixels to encoders, which convert them into signals
415  * appropriate for a given connector or set of connectors.
416  */
417 struct drm_encoder {
418         struct drm_device *dev;
419         struct list_head head;
420
421         struct drm_mode_object base;
422         char *name;
423         int encoder_type;
424         uint32_t possible_crtcs;
425         uint32_t possible_clones;
426
427         struct drm_crtc *crtc;
428         struct drm_bridge *bridge;
429         const struct drm_encoder_funcs *funcs;
430         void *helper_private;
431 };
432
433 /* should we poll this connector for connects and disconnects */
434 /* hot plug detectable */
435 #define DRM_CONNECTOR_POLL_HPD (1 << 0)
436 /* poll for connections */
437 #define DRM_CONNECTOR_POLL_CONNECT (1 << 1)
438 /* can cleanly poll for disconnections without flickering the screen */
439 /* DACs should rarely do this without a lot of testing */
440 #define DRM_CONNECTOR_POLL_DISCONNECT (1 << 2)
441
442 #define MAX_ELD_BYTES   128
443
444 /**
445  * drm_connector - central DRM connector control structure
446  * @dev: parent DRM device
447  * @kdev: kernel device for sysfs attributes
448  * @attr: sysfs attributes
449  * @head: list management
450  * @base: base KMS object
451  * @name: connector name
452  * @connector_type: one of the %DRM_MODE_CONNECTOR_<foo> types from drm_mode.h
453  * @connector_type_id: index into connector type enum
454  * @interlace_allowed: can this connector handle interlaced modes?
455  * @doublescan_allowed: can this connector handle doublescan?
456  * @modes: modes available on this connector (from fill_modes() + user)
457  * @status: one of the drm_connector_status enums (connected, not, or unknown)
458  * @probed_modes: list of modes derived directly from the display
459  * @display_info: information about attached display (e.g. from EDID)
460  * @funcs: connector control functions
461  * @edid_blob_ptr: DRM property containing EDID if present
462  * @properties: property tracking for this connector
463  * @polled: a %DRM_CONNECTOR_POLL_<foo> value for core driven polling
464  * @dpms: current dpms state
465  * @helper_private: mid-layer private data
466  * @force: a %DRM_FORCE_<foo> state for forced mode sets
467  * @encoder_ids: valid encoders for this connector
468  * @encoder: encoder driving this connector, if any
469  * @eld: EDID-like data, if present
470  * @dvi_dual: dual link DVI, if found
471  * @max_tmds_clock: max clock rate, if found
472  * @latency_present: AV delay info from ELD, if found
473  * @video_latency: video latency info from ELD, if found
474  * @audio_latency: audio latency info from ELD, if found
475  * @null_edid_counter: track sinks that give us all zeros for the EDID
476  *
477  * Each connector may be connected to one or more CRTCs, or may be clonable by
478  * another connector if they can share a CRTC.  Each connector also has a specific
479  * position in the broader display (referred to as a 'screen' though it could
480  * span multiple monitors).
481  */
482 struct drm_connector {
483         struct drm_device *dev;
484         struct device *kdev;
485         struct device_attribute *attr;
486         struct list_head head;
487
488         struct drm_mode_object base;
489
490         char *name;
491         int connector_type;
492         int connector_type_id;
493         bool interlace_allowed;
494         bool doublescan_allowed;
495         bool stereo_allowed;
496         struct list_head modes; /* list of modes on this connector */
497
498         enum drm_connector_status status;
499
500         /* these are modes added by probing with DDC or the BIOS */
501         struct list_head probed_modes;
502
503         struct drm_display_info display_info;
504         const struct drm_connector_funcs *funcs;
505
506         struct drm_property_blob *edid_blob_ptr;
507         struct drm_object_properties properties;
508
509         uint8_t polled; /* DRM_CONNECTOR_POLL_* */
510
511         /* requested DPMS state */
512         int dpms;
513
514         void *helper_private;
515
516         /* forced on connector */
517         enum drm_connector_force force;
518         uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
519         struct drm_encoder *encoder; /* currently active encoder */
520
521         /* EDID bits */
522         uint8_t eld[MAX_ELD_BYTES];
523         bool dvi_dual;
524         int max_tmds_clock;     /* in MHz */
525         bool latency_present[2];
526         int video_latency[2];   /* [0]: progressive, [1]: interlaced */
527         int audio_latency[2];
528         int null_edid_counter; /* needed to workaround some HW bugs where we get all 0s */
529         unsigned bad_edid_counter;
530 };
531
532 /**
533  * drm_plane_funcs - driver plane control functions
534  * @update_plane: update the plane configuration
535  * @disable_plane: shut down the plane
536  * @destroy: clean up plane resources
537  * @set_property: called when a property is changed
538  */
539 struct drm_plane_funcs {
540         int (*update_plane)(struct drm_plane *plane,
541                             struct drm_crtc *crtc, struct drm_framebuffer *fb,
542                             int crtc_x, int crtc_y,
543                             unsigned int crtc_w, unsigned int crtc_h,
544                             uint32_t src_x, uint32_t src_y,
545                             uint32_t src_w, uint32_t src_h);
546         int (*disable_plane)(struct drm_plane *plane);
547         void (*destroy)(struct drm_plane *plane);
548
549         int (*set_property)(struct drm_plane *plane,
550                             struct drm_property *property, uint64_t val);
551 };
552
553 enum drm_plane_type {
554         DRM_PLANE_TYPE_OVERLAY,
555         DRM_PLANE_TYPE_PRIMARY,
556         DRM_PLANE_TYPE_CURSOR,
557 };
558
559 /**
560  * drm_plane - central DRM plane control structure
561  * @dev: DRM device this plane belongs to
562  * @head: for list management
563  * @base: base mode object
564  * @possible_crtcs: pipes this plane can be bound to
565  * @format_types: array of formats supported by this plane
566  * @format_count: number of formats supported
567  * @crtc: currently bound CRTC
568  * @fb: currently bound fb
569  * @funcs: helper functions
570  * @properties: property tracking for this plane
571  * @type: type of plane (overlay, primary, cursor)
572  */
573 struct drm_plane {
574         struct drm_device *dev;
575         struct list_head head;
576
577         struct drm_mode_object base;
578
579         uint32_t possible_crtcs;
580         uint32_t *format_types;
581         uint32_t format_count;
582
583         struct drm_crtc *crtc;
584         struct drm_framebuffer *fb;
585
586         const struct drm_plane_funcs *funcs;
587
588         struct drm_object_properties properties;
589
590         enum drm_plane_type type;
591 };
592
593 /**
594  * drm_bridge_funcs - drm_bridge control functions
595  * @mode_fixup: Try to fixup (or reject entirely) proposed mode for this bridge
596  * @disable: Called right before encoder prepare, disables the bridge
597  * @post_disable: Called right after encoder prepare, for lockstepped disable
598  * @mode_set: Set this mode to the bridge
599  * @pre_enable: Called right before encoder commit, for lockstepped commit
600  * @enable: Called right after encoder commit, enables the bridge
601  * @destroy: make object go away
602  */
603 struct drm_bridge_funcs {
604         bool (*mode_fixup)(struct drm_bridge *bridge,
605                            const struct drm_display_mode *mode,
606                            struct drm_display_mode *adjusted_mode);
607         void (*disable)(struct drm_bridge *bridge);
608         void (*post_disable)(struct drm_bridge *bridge);
609         void (*mode_set)(struct drm_bridge *bridge,
610                          struct drm_display_mode *mode,
611                          struct drm_display_mode *adjusted_mode);
612         void (*pre_enable)(struct drm_bridge *bridge);
613         void (*enable)(struct drm_bridge *bridge);
614         void (*destroy)(struct drm_bridge *bridge);
615 };
616
617 /**
618  * drm_bridge - central DRM bridge control structure
619  * @dev: DRM device this bridge belongs to
620  * @head: list management
621  * @base: base mode object
622  * @funcs: control functions
623  * @driver_private: pointer to the bridge driver's internal context
624  */
625 struct drm_bridge {
626         struct drm_device *dev;
627         struct list_head head;
628
629         struct drm_mode_object base;
630
631         const struct drm_bridge_funcs *funcs;
632         void *driver_private;
633 };
634
635 /**
636  * drm_mode_set - new values for a CRTC config change
637  * @head: list management
638  * @fb: framebuffer to use for new config
639  * @crtc: CRTC whose configuration we're about to change
640  * @mode: mode timings to use
641  * @x: position of this CRTC relative to @fb
642  * @y: position of this CRTC relative to @fb
643  * @connectors: array of connectors to drive with this CRTC if possible
644  * @num_connectors: size of @connectors array
645  *
646  * Represents a single crtc the connectors that it drives with what mode
647  * and from which framebuffer it scans out from.
648  *
649  * This is used to set modes.
650  */
651 struct drm_mode_set {
652         struct drm_framebuffer *fb;
653         struct drm_crtc *crtc;
654         struct drm_display_mode *mode;
655
656         uint32_t x;
657         uint32_t y;
658
659         struct drm_connector **connectors;
660         size_t num_connectors;
661 };
662
663 /**
664  * struct drm_mode_config_funcs - basic driver provided mode setting functions
665  * @fb_create: create a new framebuffer object
666  * @output_poll_changed: function to handle output configuration changes
667  *
668  * Some global (i.e. not per-CRTC, connector, etc) mode setting functions that
669  * involve drivers.
670  */
671 struct drm_mode_config_funcs {
672         struct drm_framebuffer *(*fb_create)(struct drm_device *dev,
673                                              struct drm_file *file_priv,
674                                              struct drm_mode_fb_cmd2 *mode_cmd);
675         void (*output_poll_changed)(struct drm_device *dev);
676 };
677
678 /**
679  * drm_mode_group - group of mode setting resources for potential sub-grouping
680  * @num_crtcs: CRTC count
681  * @num_encoders: encoder count
682  * @num_connectors: connector count
683  * @id_list: list of KMS object IDs in this group
684  *
685  * Currently this simply tracks the global mode setting state.  But in the
686  * future it could allow groups of objects to be set aside into independent
687  * control groups for use by different user level processes (e.g. two X servers
688  * running simultaneously on different heads, each with their own mode
689  * configuration and freedom of mode setting).
690  */
691 struct drm_mode_group {
692         uint32_t num_crtcs;
693         uint32_t num_encoders;
694         uint32_t num_connectors;
695         uint32_t num_bridges;
696
697         /* list of object IDs for this group */
698         uint32_t *id_list;
699 };
700
701 /**
702  * drm_mode_config - Mode configuration control structure
703  * @mutex: mutex protecting KMS related lists and structures
704  * @idr_mutex: mutex for KMS ID allocation and management
705  * @crtc_idr: main KMS ID tracking object
706  * @num_fb: number of fbs available
707  * @fb_list: list of framebuffers available
708  * @num_connector: number of connectors on this device
709  * @connector_list: list of connector objects
710  * @num_bridge: number of bridges on this device
711  * @bridge_list: list of bridge objects
712  * @num_encoder: number of encoders on this device
713  * @encoder_list: list of encoder objects
714  * @num_crtc: number of CRTCs on this device
715  * @crtc_list: list of CRTC objects
716  * @min_width: minimum pixel width on this device
717  * @min_height: minimum pixel height on this device
718  * @max_width: maximum pixel width on this device
719  * @max_height: maximum pixel height on this device
720  * @funcs: core driver provided mode setting functions
721  * @fb_base: base address of the framebuffer
722  * @poll_enabled: track polling status for this device
723  * @output_poll_work: delayed work for polling in process context
724  * @*_property: core property tracking
725  *
726  * Core mode resource tracking structure.  All CRTC, encoders, and connectors
727  * enumerated by the driver are added here, as are global properties.  Some
728  * global restrictions are also here, e.g. dimension restrictions.
729  */
730 struct drm_mode_config {
731         struct mutex mutex; /* protects configuration (mode lists etc.) */
732         struct mutex idr_mutex; /* for IDR management */
733         struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
734         /* this is limited to one for now */
735
736
737         /**
738          * fb_lock - mutex to protect fb state
739          *
740          * Besides the global fb list his also protects the fbs list in the
741          * file_priv
742          */
743         struct mutex fb_lock;
744         int num_fb;
745         struct list_head fb_list;
746
747         int num_connector;
748         struct list_head connector_list;
749         int num_bridge;
750         struct list_head bridge_list;
751         int num_encoder;
752         struct list_head encoder_list;
753
754         /*
755          * Track # of overlay planes separately from # of total planes.  By
756          * default we only advertise overlay planes to userspace; if userspace
757          * sets the "universal plane" capability bit, we'll go ahead and
758          * expose all planes.
759          */
760         int num_overlay_plane;
761         int num_total_plane;
762         struct list_head plane_list;
763
764         int num_crtc;
765         struct list_head crtc_list;
766
767         struct list_head property_list;
768
769         int min_width, min_height;
770         int max_width, max_height;
771         const struct drm_mode_config_funcs *funcs;
772         resource_size_t fb_base;
773
774         /* output poll support */
775         bool poll_enabled;
776         bool poll_running;
777         struct delayed_work output_poll_work;
778
779         /* pointers to standard properties */
780         struct list_head property_blob_list;
781         struct drm_property *edid_property;
782         struct drm_property *dpms_property;
783         struct drm_property *plane_type_property;
784
785         /* DVI-I properties */
786         struct drm_property *dvi_i_subconnector_property;
787         struct drm_property *dvi_i_select_subconnector_property;
788
789         /* TV properties */
790         struct drm_property *tv_subconnector_property;
791         struct drm_property *tv_select_subconnector_property;
792         struct drm_property *tv_mode_property;
793         struct drm_property *tv_left_margin_property;
794         struct drm_property *tv_right_margin_property;
795         struct drm_property *tv_top_margin_property;
796         struct drm_property *tv_bottom_margin_property;
797         struct drm_property *tv_brightness_property;
798         struct drm_property *tv_contrast_property;
799         struct drm_property *tv_flicker_reduction_property;
800         struct drm_property *tv_overscan_property;
801         struct drm_property *tv_saturation_property;
802         struct drm_property *tv_hue_property;
803
804         /* Optional properties */
805         struct drm_property *scaling_mode_property;
806         struct drm_property *dirty_info_property;
807
808         /* dumb ioctl parameters */
809         uint32_t preferred_depth, prefer_shadow;
810
811         /* whether async page flip is supported or not */
812         bool async_page_flip;
813
814         /* cursor size */
815         uint32_t cursor_width, cursor_height;
816 };
817
818 #define obj_to_crtc(x) container_of(x, struct drm_crtc, base)
819 #define obj_to_connector(x) container_of(x, struct drm_connector, base)
820 #define obj_to_encoder(x) container_of(x, struct drm_encoder, base)
821 #define obj_to_mode(x) container_of(x, struct drm_display_mode, base)
822 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
823 #define obj_to_property(x) container_of(x, struct drm_property, base)
824 #define obj_to_blob(x) container_of(x, struct drm_property_blob, base)
825 #define obj_to_plane(x) container_of(x, struct drm_plane, base)
826
827 struct drm_prop_enum_list {
828         int type;
829         char *name;
830 };
831
832 extern void drm_modeset_lock_all(struct drm_device *dev);
833 extern void drm_modeset_unlock_all(struct drm_device *dev);
834 extern void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
835
836 extern int drm_crtc_init_with_planes(struct drm_device *dev,
837                                      struct drm_crtc *crtc,
838                                      struct drm_plane *primary,
839                                      void *cursor,
840                                      const struct drm_crtc_funcs *funcs);
841 extern int drm_crtc_init(struct drm_device *dev,
842                          struct drm_crtc *crtc,
843                          const struct drm_crtc_funcs *funcs);
844 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
845 extern unsigned int drm_crtc_index(struct drm_crtc *crtc);
846
847 /**
848  * drm_crtc_mask - find the mask of a registered CRTC
849  * @crtc: CRTC to find mask for
850  *
851  * Given a registered CRTC, return the mask bit of that CRTC for an
852  * encoder's possible_crtcs field.
853  */
854 static inline uint32_t drm_crtc_mask(struct drm_crtc *crtc)
855 {
856         return 1 << drm_crtc_index(crtc);
857 }
858
859 extern void drm_connector_ida_init(void);
860 extern void drm_connector_ida_destroy(void);
861 extern int drm_connector_init(struct drm_device *dev,
862                               struct drm_connector *connector,
863                               const struct drm_connector_funcs *funcs,
864                               int connector_type);
865
866 extern void drm_connector_cleanup(struct drm_connector *connector);
867 /* helper to unplug all connectors from sysfs for device */
868 extern void drm_connector_unplug_all(struct drm_device *dev);
869
870 extern int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
871                            const struct drm_bridge_funcs *funcs);
872 extern void drm_bridge_cleanup(struct drm_bridge *bridge);
873
874 extern int drm_encoder_init(struct drm_device *dev,
875                             struct drm_encoder *encoder,
876                             const struct drm_encoder_funcs *funcs,
877                             int encoder_type);
878
879 /**
880  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
881  * @encoder: encoder to test
882  * @crtc: crtc to test
883  *
884  * Return false if @encoder can't be driven by @crtc, true otherwise.
885  */
886 static inline bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
887                                        struct drm_crtc *crtc)
888 {
889         return !!(encoder->possible_crtcs & drm_crtc_mask(crtc));
890 }
891
892 extern int drm_universal_plane_init(struct drm_device *dev,
893                                     struct drm_plane *plane,
894                                     unsigned long possible_crtcs,
895                                     const struct drm_plane_funcs *funcs,
896                                     const uint32_t *formats,
897                                     uint32_t format_count,
898                                     enum drm_plane_type type);
899 extern int drm_plane_init(struct drm_device *dev,
900                           struct drm_plane *plane,
901                           unsigned long possible_crtcs,
902                           const struct drm_plane_funcs *funcs,
903                           const uint32_t *formats, uint32_t format_count,
904                           bool is_primary);
905 extern void drm_plane_cleanup(struct drm_plane *plane);
906 extern void drm_plane_force_disable(struct drm_plane *plane);
907 extern int drm_crtc_check_viewport(const struct drm_crtc *crtc,
908                                    int x, int y,
909                                    const struct drm_display_mode *mode,
910                                    const struct drm_framebuffer *fb);
911
912 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
913
914 extern const char *drm_get_connector_status_name(enum drm_connector_status status);
915 extern const char *drm_get_subpixel_order_name(enum subpixel_order order);
916 extern const char *drm_get_dpms_name(int val);
917 extern const char *drm_get_dvi_i_subconnector_name(int val);
918 extern const char *drm_get_dvi_i_select_name(int val);
919 extern const char *drm_get_tv_subconnector_name(int val);
920 extern const char *drm_get_tv_select_name(int val);
921 extern void drm_fb_release(struct drm_file *file_priv);
922 extern int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group);
923 extern void drm_mode_group_destroy(struct drm_mode_group *group);
924 extern bool drm_probe_ddc(struct i2c_adapter *adapter);
925 extern struct edid *drm_get_edid(struct drm_connector *connector,
926                                  struct i2c_adapter *adapter);
927 extern struct edid *drm_edid_duplicate(const struct edid *edid);
928 extern int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
929 extern void drm_mode_config_init(struct drm_device *dev);
930 extern void drm_mode_config_reset(struct drm_device *dev);
931 extern void drm_mode_config_cleanup(struct drm_device *dev);
932
933 extern int drm_mode_connector_update_edid_property(struct drm_connector *connector,
934                                                 struct edid *edid);
935
936 static inline bool drm_property_type_is(struct drm_property *property,
937                 uint32_t type)
938 {
939         /* instanceof for props.. handles extended type vs original types: */
940         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
941                 return (property->flags & DRM_MODE_PROP_EXTENDED_TYPE) == type;
942         return property->flags & type;
943 }
944
945 static inline bool drm_property_type_valid(struct drm_property *property)
946 {
947         if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
948                 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
949         return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
950 }
951
952 extern int drm_object_property_set_value(struct drm_mode_object *obj,
953                                          struct drm_property *property,
954                                          uint64_t val);
955 extern int drm_object_property_get_value(struct drm_mode_object *obj,
956                                          struct drm_property *property,
957                                          uint64_t *value);
958 extern int drm_framebuffer_init(struct drm_device *dev,
959                                 struct drm_framebuffer *fb,
960                                 const struct drm_framebuffer_funcs *funcs);
961 extern struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
962                                                       uint32_t id);
963 extern void drm_framebuffer_unreference(struct drm_framebuffer *fb);
964 extern void drm_framebuffer_reference(struct drm_framebuffer *fb);
965 extern void drm_framebuffer_remove(struct drm_framebuffer *fb);
966 extern void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
967 extern void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
968
969 extern void drm_object_attach_property(struct drm_mode_object *obj,
970                                        struct drm_property *property,
971                                        uint64_t init_val);
972 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
973                                                 const char *name, int num_values);
974 extern struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
975                                          const char *name,
976                                          const struct drm_prop_enum_list *props,
977                                          int num_values);
978 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
979                                          int flags, const char *name,
980                                          const struct drm_prop_enum_list *props,
981                                          int num_values);
982 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
983                                          const char *name,
984                                          uint64_t min, uint64_t max);
985 struct drm_property *drm_property_create_object(struct drm_device *dev,
986                                          int flags, const char *name, uint32_t type);
987 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
988 extern int drm_property_add_enum(struct drm_property *property, int index,
989                                  uint64_t value, const char *name);
990 extern int drm_mode_create_dvi_i_properties(struct drm_device *dev);
991 extern int drm_mode_create_tv_properties(struct drm_device *dev, int num_formats,
992                                      char *formats[]);
993 extern int drm_mode_create_scaling_mode_property(struct drm_device *dev);
994 extern int drm_mode_create_dirty_info_property(struct drm_device *dev);
995
996 extern int drm_mode_connector_attach_encoder(struct drm_connector *connector,
997                                              struct drm_encoder *encoder);
998 extern int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
999                                          int gamma_size);
1000 extern struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
1001                 uint32_t id, uint32_t type);
1002
1003 /* IOCTLs */
1004 extern int drm_mode_getresources(struct drm_device *dev,
1005                                  void *data, struct drm_file *file_priv);
1006 extern int drm_mode_getplane_res(struct drm_device *dev, void *data,
1007                                    struct drm_file *file_priv);
1008 extern int drm_mode_getcrtc(struct drm_device *dev,
1009                             void *data, struct drm_file *file_priv);
1010 extern int drm_mode_getconnector(struct drm_device *dev,
1011                               void *data, struct drm_file *file_priv);
1012 extern int drm_mode_set_config_internal(struct drm_mode_set *set);
1013 extern int drm_mode_setcrtc(struct drm_device *dev,
1014                             void *data, struct drm_file *file_priv);
1015 extern int drm_mode_getplane(struct drm_device *dev,
1016                                void *data, struct drm_file *file_priv);
1017 extern int drm_mode_setplane(struct drm_device *dev,
1018                                void *data, struct drm_file *file_priv);
1019 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
1020                                 void *data, struct drm_file *file_priv);
1021 extern int drm_mode_cursor2_ioctl(struct drm_device *dev,
1022                                 void *data, struct drm_file *file_priv);
1023 extern int drm_mode_addfb(struct drm_device *dev,
1024                           void *data, struct drm_file *file_priv);
1025 extern int drm_mode_addfb2(struct drm_device *dev,
1026                            void *data, struct drm_file *file_priv);
1027 extern uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
1028 extern int drm_mode_rmfb(struct drm_device *dev,
1029                          void *data, struct drm_file *file_priv);
1030 extern int drm_mode_getfb(struct drm_device *dev,
1031                           void *data, struct drm_file *file_priv);
1032 extern int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1033                                   void *data, struct drm_file *file_priv);
1034
1035 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
1036                                       void *data, struct drm_file *file_priv);
1037 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
1038                                   void *data, struct drm_file *file_priv);
1039 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1040                                               void *data, struct drm_file *file_priv);
1041 extern int drm_mode_getencoder(struct drm_device *dev,
1042                                void *data, struct drm_file *file_priv);
1043 extern int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1044                                     void *data, struct drm_file *file_priv);
1045 extern int drm_mode_gamma_set_ioctl(struct drm_device *dev,
1046                                     void *data, struct drm_file *file_priv);
1047 extern u8 drm_match_cea_mode(const struct drm_display_mode *to_match);
1048 extern enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code);
1049 extern bool drm_detect_hdmi_monitor(struct edid *edid);
1050 extern bool drm_detect_monitor_audio(struct edid *edid);
1051 extern bool drm_rgb_quant_range_selectable(struct edid *edid);
1052 extern int drm_mode_page_flip_ioctl(struct drm_device *dev,
1053                                     void *data, struct drm_file *file_priv);
1054 extern int drm_add_modes_noedid(struct drm_connector *connector,
1055                                 int hdisplay, int vdisplay);
1056 extern void drm_set_preferred_mode(struct drm_connector *connector,
1057                                    int hpref, int vpref);
1058
1059 extern int drm_edid_header_is_valid(const u8 *raw_edid);
1060 extern bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid);
1061 extern bool drm_edid_is_valid(struct edid *edid);
1062 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
1063                                            int hsize, int vsize, int fresh,
1064                                            bool rb);
1065
1066 extern int drm_mode_create_dumb_ioctl(struct drm_device *dev,
1067                                       void *data, struct drm_file *file_priv);
1068 extern int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1069                                     void *data, struct drm_file *file_priv);
1070 extern int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1071                                       void *data, struct drm_file *file_priv);
1072 extern int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
1073                                              struct drm_file *file_priv);
1074 extern int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
1075                                            struct drm_file *file_priv);
1076
1077 extern void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
1078                                  int *bpp);
1079 extern int drm_format_num_planes(uint32_t format);
1080 extern int drm_format_plane_cpp(uint32_t format, int plane);
1081 extern int drm_format_horz_chroma_subsampling(uint32_t format);
1082 extern int drm_format_vert_chroma_subsampling(uint32_t format);
1083 extern const char *drm_get_format_name(uint32_t format);
1084
1085 /* Helpers */
1086
1087 static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
1088                 uint32_t id)
1089 {
1090         struct drm_mode_object *mo;
1091         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
1092         return mo ? obj_to_plane(mo) : NULL;
1093 }
1094
1095 static inline struct drm_crtc *drm_crtc_find(struct drm_device *dev,
1096         uint32_t id)
1097 {
1098         struct drm_mode_object *mo;
1099         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CRTC);
1100         return mo ? obj_to_crtc(mo) : NULL;
1101 }
1102
1103 static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,
1104         uint32_t id)
1105 {
1106         struct drm_mode_object *mo;
1107         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
1108         return mo ? obj_to_encoder(mo) : NULL;
1109 }
1110
1111 static inline struct drm_connector *drm_connector_find(struct drm_device *dev,
1112                 uint32_t id)
1113 {
1114         struct drm_mode_object *mo;
1115         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_CONNECTOR);
1116         return mo ? obj_to_connector(mo) : NULL;
1117 }
1118
1119 static inline struct drm_property *drm_property_find(struct drm_device *dev,
1120                 uint32_t id)
1121 {
1122         struct drm_mode_object *mo;
1123         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PROPERTY);
1124         return mo ? obj_to_property(mo) : NULL;
1125 }
1126
1127 static inline struct drm_property_blob *
1128 drm_property_blob_find(struct drm_device *dev, uint32_t id)
1129 {
1130         struct drm_mode_object *mo;
1131         mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_BLOB);
1132         return mo ? obj_to_blob(mo) : NULL;
1133 }
1134
1135 /* Plane list iterator for legacy (overlay only) planes. */
1136 #define drm_for_each_legacy_plane(plane, planelist) \
1137         list_for_each_entry(plane, planelist, head) \
1138                 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1139
1140 #endif /* __DRM_CRTC_H__ */