1		    Multi-monitor Mode Setting APIs
2	           Keith Packard, <keithp@keithp.com
3		   	   6 March 2007
4
51. Introduction
6
7This document describes a set of mode setting APIs added in X server version
81.3 that support multiple monitors per card. These interfaces expose the
9underlying hardware CRTC and output concepts to the xf86 DDX layer so that
10the implementation of initial server setup and mode changes through
11extensions can be shared across drivers. In addition, these new interfaces
12support a new configuration mechanism as well which allows each monitor to
13be customized separately providing a consistent cross-driver configuration
14mechanism that supports the full range of output features.
15
16All of the code implementing this interface can be found in hw/xfree86/modes
17in the X server sources.
18
192. Overview
20
21This document describes both the driver API and the configuration data
22placed in xorg.conf; these are entirely separate as the driver has no
23interaction with the configuration information at all. Much of the structure
24here is cloned from the RandR extension version 1.2 additions which deal
25with the same kinds of information.
26
272.1 API overview
28
29The mode setting API is expressed through two new driver-visible objects,
30the 'CRTC' (xf86CrtcRec) and the 'Output' (xf86OutputRec). A CRTC refers to
31hardware within the video system that can scan a subset of the framebuffer
32and generate a video signal. An Output receives that signal and transmits it
33to a monitor, projector or other device.
34
35The xf86CrtcRec and xf86OutputRec contain a small amount of state data
36related to the object along with a pointer to a set of functions provided by
37the driver that manipulate the object in fairly simple ways.
38
39To emulate older behaviour, one of the outputs is picked as the 'compat'
40output; this output changes over time as outputs are detected and used, the
41goal is to always have one 'special' output which is used for operations
42which need a single defined monitor (like XFree86-VidModeExtension mode
43setting, RandR 1.1 mode setting, DDC property setting, etc.).
44
452.1.1 Output overview 
46
47As outputs are connected to monitors, they hold a list of modes supported by
48the monitor. If the monitor and output support DDC, then the list of modes
49generally comes from the EDID data in the monitor. Otherwise, the server
50uses the standard VESA modes, pruned by monitor timing. If the configuration
51file doesn't contain monitor timing data, the server uses default timing
52information which supports 640x480, 800x600 and 1024x768 all with a 60Hz
53refresh rate.
54
55As hardware often limits possible configuration combinations, each output
56knows the set of CRTCs that it can be connected to as well as the set of
57other outputs which can be simutaneously connected to a CRTC.
58
592.1.2 CRTC overview
60
61CRTCs serve only to stream frame buffer data to outputs using a mode line.
62Ideally, they would not be presented to the user at all, and in fact the
63configuration file doesn't expose them. The RandR 1.2 protocol does, but the
64hope there is that client-side applications will hide them carefully away.
65
66Each crtc has an associated cursor, along with the current configuration.
67All of the data needed to determine valid configurations is contained within
68the Outputs.
69
702.2 Configuration overview
71
72As outputs drive monitors, the "Monitor" section has been repurposed to
73define their configuration. This provides for a bit more syntax than
74the large list of driver-specific options that were used in the past for
75similar configuration.
76
77However, the existing "Monitor" section referenced by the active "Screen"
78section no longer has any use at all; some sensible meaning for this
79parameter is needed now that a Screen can have multiple Monitors.
80
813. Public Functions
82
833.1 PreInit functions
84
85These functions should be used during the driver PreInit phase, they are
86arranged in the order they should be invoked.
87
88    void
89    xf86CrtcConfigInit (ScrnInfoPtr			scrn
90			const xf86CrtcConfigFuncsRec	*funcs)
91
92This function allocates and initializes structures needed to track CRTC and
93Output state.
94
95    void
96    xf86CrtcSetSizeRange (ScrnInfoPtr scrn,
97			  int minWidth, int minHeight,
98			  int maxWidth, int maxHeight)
99
100This sets the range of screen sizes supported by the driver.
101
102    xf86CrtcPtr
103    xf86CrtcCreate (ScrnInfoPtr             scrn,
104		    const xf86CrtcFuncsRec  *funcs)
105    
106Create one CRTC object. See the discussion below for a description of the
107contents of the xf86CrtcFuncsRec. Note that this is done in PreInit, so it
108should not be re-invoked at each server generation. Create one of these for
109each CRTC present in the hardware.
110    
111    xf86OutputPtr
112    xf86OutputCreate (ScrnInfoPtr              scrn,
113		      const xf86OutputFuncsRec *funcs,
114		      const char	       *name)
115
116Create one Output object. See the discussion below for a description of the
117contents of the xf86OutputFuncsRec. This is also called from PreInit and
118need not be re-invoked at each ScreenInit time. An Output should be created
119for every Output present in the hardware, not just for outputs which have
120detected monitors.
121    
122    Bool
123    xf86OutputRename (xf86OutputPtr output, const char *name)
124
125If necessary, the name of an output can be changed after it is created using
126this function.
127    
128    Bool
129    xf86InitialConfiguration (ScrnInfoPtr scrn, Bool canGrow)
130
131Using the resources provided, and the configuration specified by the user,
132this function computes an initial configuration for the server. It tries to
133enable as much hardware as possible using some fairly simple heuristics. 
134
135The 'canGrow' parameter indicates that the frame buffer does not have a fixed
136size (fixed size frame buffers are required by XAA). When the frame buffer
137has a fixed size, the configuration selects a 'reasonablely large' frame
138buffer so that common reconfiguration options are possible. For resizable
139frame buffers, the frame buffer is set to the smallest size that encloses
140the desired configuration.
141    
1423.2 ScreenInit functions
143
144These functions should be used during the driver ScreenInit phase.
145
146    Bool
147    xf86DiDGAInit (ScreenPtr screen, unsigned long dga_address)
148
149This function provides driver-independent accelerated DGA support for some
150of the DGA operations; using this, the driver can avoid needing to implement
151any of the rest of DGA.
152
153    Bool
154    xf86SaveScreen(ScreenPtr pScreen, int mode)
155
156Stick this in pScreen->SaveScreen and the core X screen saver will be
157implemented by disabling outputs and crtcs using their dpms functions.
158
159    void
160    xf86DPMSSet(ScrnInfoPtr scrn, int mode, int flags)
161
162Pass this function to xf86DPMSInit and all DPMS mode switching will be
163managed by using the dpms functions provided by the Outputs and CRTCs.
164
165    Bool
166    xf86CrtcScreenInit (ScreenPtr screen)
167
168This function completes the screen initialization process for the crtc and
169output objects. Call it near the end of the ScreenInit function, after the
170frame buffer and acceleration layers have been added.
171
1723.3 EnterVT functions
173
174Functions used during EnterVT, or whenever the current configuration needs
175to be applied to the hardware.
176
177    Bool
178    xf86SetDesiredModes (ScrnInfoPtr scrn)
179
180xf86InitialConfiguration selects the desired configuration at PreInit time;
181when the server finally hits ScreenInit, xf86SetDesiredModes is used by the
182driver to take that configuration and apply it to the hardware. In addition,
183successful mode selection at other times updates the configuration that will
184be used by this function, so LeaveVT/EnterVT pairs can simply invoke this
185and return to the previous configuration.
186
1873.4 SwitchMode functions
188
189Functions called from the pScrn->SwitchMode hook, which is used by the
190XFree86-VidModeExtension and the keypad mode switch commands.
191
192    Bool
193    xf86SetSingleMode (ScrnInfoPtr	scrn, 
194		       DisplayModePtr   desired,
195		       Rotation		rotation)
196
197This function applies the specified mode to all active outputs. Which is to
198say, it picks reasonable modes for all active outputs, attempting to get the
199screen to the specified size while not breaking anything that is currently
200working.
201
2023.7 get_modes functions
203
204Functions called during output->get_modes to help build lists of modes
205
206    xf86MonPtr
207    xf86OutputGetEDID (xf86OutputPtr output, I2CBusPtr pDDCBus)
208
209This returns the EDID data structure for the 'output' using the I2C bus
210'pDDCBus'. This has no effect on 'output' itself.
211
212    void
213    xf86OutputSetEDID (xf86OutputPtr output, xf86MonPtr edid_mon)
214
215Once the EDID data has been fetched, this call applies the EDID data to the
216output object, setting the physical size and also various properties, like
217the DDC root window property (when output is the 'compat' output), and the
218RandR 1.2 EDID output properties.
219
220    DisplayModePtr
221    xf86OutputGetEDIDModes (xf86OutputPtr output)
222
223Given an EDID data structure, this function computes a list of suitable
224modes. This function also applies a sequence of 'quirks' during this process
225so that the returned modes may not actually match the mode data present in
226the EDID data.
227
2283.6 Other functions
229
230These remaining functions in the API can be used by the driver as needed.
231
232    Bool
233    xf86CrtcSetMode (xf86CrtcPtr crtc, DisplayModePtr mode, Rotation rotation,
234		     int x, int y)
235
236Applies a mode to a CRTC. All of the outputs which are currently using the
237specified CRTC are included in the mode setting process. 'x' and 'y' are the
238offset within the frame buffer that the crtc is placed at. No checking is
239done in this function to ensure that the mode is usable by the active
240outputs.
241    
242    void
243    xf86ProbeOutputModes (ScrnInfoPtr pScrn, int maxX, int maxY)
244
245This discards the mode lists for all outputs, re-detects monitor presence
246and then acquires new mode lists for all monitors which are not disconnected.
247Monitor configuration data is used to modify the mode lists returned by the
248outputs. 'maxX' and 'maxY' limit the maximum size modes that will be
249returned.
250    
251    void
252    xf86SetScrnInfoModes (ScrnInfoPtr pScrn)
253
254This copies the 'compat' output mode list into the pScrn modes list which is
255used by the XFree86-VidModeExtension and the keypad mode switching
256operations. The current 'desired' mode for the CRTC associated with the
257'compat' output is placed first in this list to indicate the current mode.
258Usually, the driver won't need to call this function as
259xf86InitialConfiguration will do so automatically, as well as any RandR
260functions which reprobe for modes. However, if the driver reprobes for modes
261at other times using xf86ProbeOutputModes, this function needs to be called.
262    
263    Bool
264    xf86DiDGAReInit (ScreenPtr pScreen)
265
266This is similar to xf86SetScrnInfoModes, but it applies the 'compat' output
267mode list to the set of modes advertised by the DGA extension; it needs to
268be called whenever xf86ProbeOutputModes is invoked.
269
270    void
271    xf86DisableUnusedFunctions(ScrnInfoPtr pScrn)
272
273After any sequence of calls using xf86CrtcSetMode, this function cleans up
274any leftover Output and CRTC objects by disabling them, saving power. It is
275safe to call this whenever the server is running as it only disables objects
276which are not currently in use.
277    
2784. CRTC operations
279
2804.1 CRTC functions
281
282These functions provide an abstract interface for the CRTC object; most
283manipulation of the CRTC object is done through these functions.
284
285    void
286    crtc->funcs->dpms (xf86CrtcPtr crtc, int mode)
287
288Where 'mode' is one of DPMSModeOff, DPMSModeSuspend, DPMSModeStandby or
289DPMSModeOn. This requests that the crtc go to the specified power state.
290When changing power states, the output dpms functions are invoked before the
291crtc dpms functions.
292
293    void
294    crtc->funcs->save (xf86CrtcPtr crtc)
295	
296    void
297    crtc->funcs->restore (xf86CrtcPtr crtc)
298
299Preserve/restore any register contents related to the CRTC. These are
300strictly a convenience for the driver writer; if the existing driver has
301fully operation save/restore functions, you need not place any additional
302code here. In particular, the server itself never uses this function.
303
304    Bool
305    crtc->funcs->lock (xf86CrtcPtr crtc)
306	
307    void
308    crtc->funcs->unlock (xf86CrtcPtr crtc)
309
310These functions are invoked around mode setting operations; the intent is
311that DRI locking be done here to prevent DRI applications from manipulating
312the hardware while the server is busy changing the output configuration. If
313the lock function returns FALSE, the unlock function will not be invoked.
314
315    Bool
316    crtc->funcs->mode_fixup (xf86CrtcPtr crtc, 
317			     DisplayModePtr mode,
318			     DisplayModePtr adjusted_mode)
319
320This call gives the CRTC a chance to see what mode will be set and to
321comment on the mode by changing 'adjusted_mode' as needed. This function
322shall not modify the state of the crtc hardware at all. If the CRTC cannot
323accept this mode, this function may return FALSE.
324
325    void
326    crtc->funcs->prepare (xf86CrtcPtr crtc)
327
328This call is made just before the mode is set to make the hardware ready for
329the operation. A usual function to perform here is to disable the crtc so
330that mode setting can occur with clocks turned off and outputs deactivated.
331
332    void
333    crtc->funcs->mode_set (xf86CrtcPtr crtc,
334			   DisplayModePtr mode,
335			   DisplayModePtr adjusted_mode)
336
337This function applies the specified mode (possibly adjusted by the CRTC
338and/or Outputs).
339
340    void
341    crtc->funcs->commit (xf86CrtcPtr crtc)
342
343Once the mode has been applied to the CRTC and Outputs, this function is
344invoked to let the hardware turn things back on.
345
346    void
347    crtc->funcs->gamma_set (xf86CrtcPtr crtc, CARD16 *red,
348			    CARD16 *green, CARD16 *blue, int size)
349
350This function adjusts the gamma ramps for the specified crtc.
351
352    void *
353    crtc->funcs->shadow_allocate (xf86CrtcPtr crtc, int width, int height)
354
355This function allocates frame buffer space for a shadow frame buffer. When
356allocated, the crtc must scan from the shadow instead of the main frame
357buffer. This is used for rotation. The address returned is passed to the
358shadow_create function. This function should return NULL on failure.
359
360    PixmapPtr
361    crtc->funcs->shadow_create (xf86CrtcPtr crtc, void *data,
362				int width, int height)
363
364This function creates a pixmap object that will be used as a shadow of the
365main frame buffer for CRTCs which are rotated or reflected. 'data' is the
366value returned by shadow_allocate.
367
368    void
369    crtc->funcs->shadow_destroy (xf86CrtcPtr crtc, PixmapPtr pPixmap,
370				 void *data)
371
372Destroys any associated shadow objects. If pPixmap is NULL, then a pixmap
373was not created, but 'data' may still be non-NULL indicating that the shadow
374had been allocated.
375
376    void
377    crtc->funcs->destroy (xf86CrtcPtr crtc)
378
379When a CRTC is destroyed (which only happens in error cases), this function
380can clean up any driver-specific data.
381
3824.2 CRTC fields
383
384The CRTC object is not opaque; there are several fields of interest to the
385driver writer.
386
387    struct _xf86Crtc {
388	/**
389	 * Associated ScrnInfo
390	 */
391	ScrnInfoPtr     scrn;
392    
393	/**
394	 * Active state of this CRTC
395	 *
396	 * Set when this CRTC is driving one or more outputs
397	 */
398	Bool            enabled;
399    
400	/** Track whether cursor is within CRTC range  */
401	Bool            cursorInRange;
402    
403	/** Track state of cursor associated with this CRTC */
404	Bool            cursorShown;
405    
406	/**
407	 * Active mode
408	 *
409	 * This reflects the mode as set in the CRTC currently
410	 * It will be cleared when the VT is not active or
411	 * during server startup
412	 */
413	DisplayModeRec  mode;
414	Rotation        rotation;
415	PixmapPtr       rotatedPixmap;
416	void            *rotatedData;
417    
418	/**
419	 * Position on screen
420	 *
421	 * Locates this CRTC within the frame buffer
422	 */
423	int             x, y;
424    
425	/**
426	 * Desired mode
427	 *
428	 * This is set to the requested mode, independent of
429	 * whether the VT is active. In particular, it receives
430	 * the startup configured mode and saves the active mode
431	 * on VT switch.
432	 */
433	DisplayModeRec  desiredMode;
434	Rotation        desiredRotation;
435	int             desiredX, desiredY;
436    
437	/** crtc-specific functions */
438	const xf86CrtcFuncsRec *funcs;
439    
440	/**
441	 * Driver private
442	 *
443	 * Holds driver-private information
444	 */
445	void            *driver_private;
446    #ifdef RANDR_12_INTERFACE
447	/**
448	 * RandR crtc
449	 *
450	 * When RandR 1.2 is available, this
451	 * points at the associated crtc object
452	 */
453	RRCrtcPtr       randr_crtc;
454    #else
455	void            *randr_crtc;
456    #endif
457    };
458
459    
4605. Output functions.
461
4626. Configuration
463
464Because the configuration file syntax is fixed,
465this was done by creating new "Driver" section options that hook specific
466outputs to specific "Monitor" sections in the file. The option:
467section of the form:
468
469	Option	"monitor-VGA" "My VGA Monitor"
470
471connects the VGA output of this driver to the "Monitor" section with
472Identifier "My VGA Monitor". All of the usual monitor options can now be
473placed in that "Monitor" section and will be applied to the VGA output
474configuration.
475