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