XFree86® server 4.x Design (DRAFT) The XFree86 Project, Inc 19 December 2003 NOTE: This is a DRAFT document, and the interfaces described here are subject to change without notice. 1. Preface The broad design principles are: o keep it reasonable o We cannot rewrite the complete server o We don't want to re-invent the wheel o keep it modular o As many things as possible should go into modules o The basic loader binary should be minimal o A clean design with well defined layering is important o DDX specific global variables are a nono o The structure should be flexible enough to allow future extensions o The structure should minimize duplication of common code o keep important features in mind o multiple screens, including multiple instances of drivers o mixing different color depths and visuals on different and ideally even on the same screen o better control of the PCI device used o better config file parser o get rid of all VGA compatibility assumptions Unless we find major deficiencies in the DIX layer, we should avoid making changes there. 2. The XF86Config File The XF86Config file format is similar to the old format, with the following changes: 2.1 Device section The Device sections are similar to what they used to be, and describe hard- ware-specific information for a single video card. Device Some new keywords are added: Driver "drivername" Specifies the name of the driver to be used for the card. This is mandatory. BusID "busslot" Specifies uniquely the location of the card on the bus. The pur- pose is to identify particular cards in a multi-headed configura- tion. The format of the argument is intentionally vague, and may be architecture dependent. For a PCI bus, it is something like "bus:slot:func". A Device section is considered ``active'' if there is a reference to it in an active Screen section. 2.2 Screen section The Screen sections are similar to what they used to be. They no longer have a Driver keyword, but an Identifier keyword is added. (The Driver keyword may be accepted in place of the Identifier keyword for compatibility pur- poses.) The identifier can be used to identify which screen is to be active when multiple Screen sections are present. It is possible to specify the active screen from the command line. A default is chosen in the absence of one being specified. A Screen section is considered ``active'' if there is a reference to it either from the command line, or from an active ServerLayout section. 2.3 InputDevice section The InputDevice section is a new section that describes configuration infor- mation for input devices. It replaces the old Keyboard, Pointer and XInput sections. Like the Device section, it has two mandatory keywords: Identifier and Driver. For compatibility purposes the old Keyboard and Pointer sections are converted by the parser into InputDevice sections as follows: Keyboard Identifier "Implicit Core Keyboard" Driver "keyboard" Pointer Identifier "Implicit Core Pointer" Driver "mouse" An InputDevice section is considered active if there is a reference to it in an active ServerLayout section. An InputDevice section may also be refer- enced implicitly if there is no ServerLayout section, if the -screen command line options is used, or if the ServerLayout section doesn't reference any InputDevice sections. In this case, the first sections with drivers "key- board" and "mouse" are used as the core keyboard and pointer respectively. 2.4 ServerLayout section The ServerLayout section is a new section that is used to identify which Screen sections are to be used in a multi-headed configuration, and the rela- tive layout of those screens. It also identifies which InputDevice sections are to be used. Each ServerLayout section has an identifier, a list of Screen section identifiers, and a list of InputDevice section identifiers. ServerFlags options may also be included in a ServerLayout section, making it possible to override the global values in the ServerFlags section. A ServerLayout section can be made active by being referenced on the command line. In the absence of this, a default will be chosen (the first one found). The screen names may optionally be followed by a number specifying the preferred screen number, and optionally by information specifying the physical positioning of the screen, either in absolute terms or relative to another screen (or screens). When no screen number is specified, they are numbered according to the order in which they are listed. The old (now obso- lete) method of providing the positioning information is to give the names of the four adjacent screens. The order of these is top, bottom, left, right. Here is an example of a ServerLayout section for two screens using the old method, with the second located to the right of the first: Section "ServerLayout" Identifier "Main Layout" Screen 0 "Screen 1" "" "" "" "Screen 2" Screen 1 "Screen 2" Screen "Screen 3" EndSection The preferred way of specifying the layout is to explicitly specify the screen's location in absolute terms or relative to another screen. In the absolute case, the upper left corner's coordinates are given after the Absolute keyword. If the coordinates are omitted, a value of (0,0) is assumed. An example of absolute positioning follows: Section "ServerLayout" Identifier "Main Layout" Screen 0 "Screen 1" Absolute 0 0 Screen 1 "Screen 2" Absolute 1024 0 Screen "Screen 3" Absolute 2048 0 EndSection In the relative case, the position is specified by either using one of the following keywords followed by the name of the reference screen: RightOf LeftOf Above Below Relative When the Relative keyword is used, the reference screen name is followed by the coordinates of the new screen's origin relative to reference screen. The following example shows how to use some of the relative positioning options. Section "ServerLayout" Identifier "Main Layout" Screen 0 "Screen 1" Screen 1 "Screen 2" RightOf "Screen 1" Screen "Screen 3" Relative "Screen 1" 2048 0 EndSection 2.5 Options Options are used more extensively. They may appear in most sections now. Options related to drivers can be present in the Screen, Device and Monitor sections and the Display subsections. The order of precedence is Display, Screen, Monitor, Device. Options have been extended to allow an optional value to be specified in addition to the option name. For more details about options, see the Options (section 10., page 1) section for details. 3. Driver Interface The driver interface consists of a minimal set of entry points that are required based on the external events that the driver must react to. No non- essential structure is imposed on the way they are used beyond that. This is a significant difference compared with the old design. The entry points for drawing operations are already taken care of by the framebuffer code (including, XAA). Extensions and enhancements to frame- buffer code are outside the scope of this document. This approach to the driver interface provides good flexibility, but does increase the complexity of drivers. To help address this, the XFree86 common layer provides a set of ``helper'' functions to take care of things that most drivers need. These helpers help minimise the amount of code duplication between drivers. The use of helper functions by drivers is however optional, though encouraged. The basic philosophy behind the helper functions is that they should be useful to many drivers, that they should balance this against the complexity of their interface. It is inevitable that some drivers may find some helpers unsuitable and need to provide their own code. Events that a driver needs to react to are: ScreenInit An initialisation function is called from the DIX layer for each screen at the start of each server generation. Enter VT The server takes control of the console. Leave VT The server releases control of the console. Mode Switch Change video mode. ViewPort change Change the origin of the physical view port. ScreenSaver state change Screen saver activation/deactivation. CloseScreen A close screen function is called from the DIX layer for each screen at the end of each server generation. In addition to these events, the following functions are required by the XFree86 common layer: Identify Print a driver identifying message. Probe This is how a driver identifies if there is any hardware present that it knows how to drive. PreInit Process information from the XF86Config file, determine the full characteristics of the hardware, and determine if a valid config- uration is present. The VidMode extension also requires: ValidMode Identify if a new mode is usable with the current configuration. The PreInit function (and/or helpers it calls) may also make use of the ValidMode function or something similar. Other extensions may require other entry points. The drivers will inform the common layer of these in such cases. 4. Resource Access Control Introduction Graphics devices are accessed through ranges in I/O or memory space. While most modern graphics devices allow relocation of such ranges many of them still require the use of well established interfaces such as VGA memory and IO ranges or 8514/A IO ranges. With modern buses (like PCI) it is possible for multiple video devices to share access to these resources. The RAC (Resource Access Control) subsystem provides a mechanism for this. 4.1 Terms and Definitions 4.1.1 Bus ``Bus'' is ambiguous as it is used for different things: it may refer to physical incompatible extension connectors in a computer system. The RAC system knows two such systems: The ISA bus and the PCI bus. (On the software level EISA, MCA and VL buses are currently treated like ISA buses). ``Bus'' may also refer to logically different entities on a single bus system which are connected via bridges. A PCI system may have several distinct PCI buses connecting each other by PCI-PCI bridges or to the host CPU by HOST-PCI bridges. Systems that host more than one bus system link these together using bridges. Bridges are a concern to RAC as they might block or pass specific resources. PCI-PCI bridges may be set up to pass VGA resources to the secondary bus. PCI-ISA buses pass any resources not decoded on the primary PCI bus to the ISA bus. This way VGA resources (although exclusive on the ISA bus) can be shared by ISA and PCI cards. Currently HOST-PCI bridges are not yet handled by RAC as they require specific drivers. 4.1.2 Entity The smallest independently addressable unit on a system bus is referred to as an entity. So far we know ISA and PCI entities. PCI entities can be located on the PCI bus by an unique ID consisting of the bus, card and function num- ber. 4.1.3 Resource ``Resource'' refers to a range of memory or I/O addresses an entity can decode. If a device is capable of disabling this decoding the resource is called sharable. For PCI devices a generic method is provided to control resource decoding. Other devices will have to provide a device specific function to control decoding. If the entity is capable of decoding this range at a different location this resource is considered relocatable. Resources which start at a specific address and occupy a single continuous range are called block resources. Alternatively resource addresses can be decoded in a way that they satisfy the conditions: address & mask == base and base & mask == base Resources addressed in such a way are called sparse resources. 4.1.4 Server States The resource access control system knows two server states: the SETUP and the OPERATING state. The SETUP state is entered whenever a mode change takes place or the server exits or does VT switching. During this state all entity resources are under resource access control. During OPERATING state only those entities are controlled which actually have shared resources that con- flict with others. 5. Control Flow in the Server and Mandatory Driver Functions At the start of each server generation, main() (dix/main.c) calls the DDX function InitOutput(). This is the first place that the DDX gets control. InitOutput() is expected to fill in the global screenInfo struct, and one screenInfo.screen[] entry for each screen present. Here is what InitOutput() does: 5.1 Parse the XF86Config file This is done at the start of the first server generation only. The XF86Config file is read in full, and the resulting information stored in data structures. None of the parsed information is processed at this point. The parser data structures are opaque to the video drivers and to most of the common layer code. The entire file is parsed first to remove any section ordering requirements. 5.2 Initial processing of parsed information and command line options This is done at the start of the first server generation only. The initial processing is to determine paths like the ModulePath, etc, and to determine which ServerLayout, Screen and Device sections are active. 5.3 Enable port I/O access Port I/O access is controlled from the XFree86 common layer, and is ``all or nothing''. It is enabled prior to calling driver probes, at the start of subsequent server generations, and when VT switching back to the Xserver. It is disabled at the end of server generations, and when VT switching away from the Xserver. The implementation details of this may vary on different platforms. 5.4 General bus probe This is done at the start of the first server generation only. In the case of ix86 machines, this will be a general PCI probe. The full information obtained here will be available to the drivers. This information persists for the life of the Xserver. In the PCI case, the PCI information for all video cards found is available by calling xf86GetPciVideoInfo(). pciVideoPtr *xf86GetPciVideoInfo(void) returns a pointer to a list of pointers to pciVideoRec entries, of which there is one for each detected PCI video card. The list is terminated with a NULL pointer. If no PCI video cards were detected, the return value is NULL. After the bus probe, the resource broker is initialised. 5.5 Load initial set of modules This is done at the start of the first server generation only. The core server contains a list of mandatory modules. These are loaded first. Currently the only module on this list is the bitmap font module. The next set of modules loaded are those specified explicitly in the Module section of the config file. The final set of initial modules are the driver modules referenced by the active Device and InputDevice sections in the config file. Each of these modules is loaded exactly once. 5.6 Register Video and Input Drivers This is done at the start of the first server generation only. When a driver module is loaded, the loader calls its Setup function. For video drivers, this function calls xf86AddDriver() to register the driver's DriverRec, which contains a small set of essential details and driver entry points required during the early phase of InitOutput(). xf86AddDriver() adds it to the global xf86DriverList[] array. The DriverRec contains the driver canonical name, the Identify(), Probe() and AvailableOptions() function entry points as well as a pointer to the driver's module (as returned from the loader when the driver was loaded) and a refer- ence count which keeps track of how many screens are using the driver. The entry driver entry points are those required prior to the driver allocating and filling in its ScrnInfoRec. For a static server, the xf86DriverList[] array is initialised at build time, and the loading of modules is not done. A similar procedure is used for input drivers. The input driver's Setup function calls xf86AddInputDriver() to register the driver's InputDriverRec, which contains a small set of essential details and driver entry points required during the early phase of InitInput(). xf86AddInputDriver() adds it to the global xf86InputDriverList[] array. For a static server, the xf86InputDriverList[] array is initialised at build time. Both the xf86DriverList[] and xf86InputDriverList[] arrays have been ini- tialised by the end of this stage. Once all the drivers are registered, their ChipIdentify() functions are called. void ChipIdentify(int flags) This is expected to print a message indicating the driver name, a short summary of what it supports, and a list of the chipset names that it supports. It may use the xf86PrintChipsets() helper to do this. void xf86PrintChipsets(const char *drvname, const char *drvmsg, SymTabPtr chips) This function provides an easy way for a driver's ChipI- dentify function to format the identification message. 5.7 Initialise Access Control This is done at the start of the first server generation only. The Resource Access Control (RAC) subsystem is initialised before calling any driver functions that may access hardware. All generic bus information is probed and saved (for restoration later). All (shared resource) video devices are disabled at the generic bus level, and a probe is done to find the ``primary'' video device. These devices remain disabled for the next step. 5.8 Video Driver Probe This is done at the start of the first server generation only. The Chip- Probe() function of each registered video driver is called. Bool ChipProbe(DriverPtr drv, int flags) The purpose of this is to identify all instances of hard- ware supported by the driver. The flags value is cur- rently either 0, PROBE_DEFAULT or PROBE_DETECT. PROBE_DETECT is used if "-configure" or "-probe" command line arguments are given and indicates to the Probe() function that it should not configure the bus entities and that no XF86Config information is available. The probe must find the active device sections that match the driver by calling xf86MatchDevice(). The number of matches found limits the maximum number of instances for this driver. If no matches are found, the function should return FALSE immediately. Devices that cannot be identified by using device-inde- pendent methods should be probed at this stage (keeping in mind that access to all resources that can be disabled in a device-independent way are disabled during this phase). The probe must be a minimal probe. It should just determine if there is a card present that the driver can drive. It should use the least intrusive probe meth- ods possible. It must not do anything that is not essen- tial, like probing for other details such as the amount of memory installed, etc. It is recommended that the xf86MatchPciInstances() helper function be used for iden- tifying matching PCI devices, and similarly the xf86MatchIsaInstances() for ISA (non-PCI) devices (see the RAC (section 9., page 1) section). These helpers also checks and claims the appropriate entity. When not using the helper, that should be done with xf86CheckPciS- lot() and xf86ClaimPciSlot() for PCI devices and xf86ClaimIsaSlot() for ISA devices (see the RAC (section 9., page 1) section). The probe must register all non-relocatable resources at this stage. If a resource conflict is found between exclusive resources the driver will fail immediately. This is usually best done with the xf86ConfigPciEntity() helper function for PCI and xf86ConfigIsaEntity() for ISA (see the RAC (section 9., page 1) section). It is possi- ble to register some entity specific functions with those helpers. When not using the helpers, the xf86AddEntity- ToScreen() xf86ClaimFixedResources() and xf86SetEntity- Funcs() should be used instead (see the RAC (section 9., page 1) section). If a chipset is specified in an active device section which the driver considers relevant (ie it has no driver specified, or the driver specified matches the driver doing the probe), the Probe must return FALSE if the chipset doesn't match one supported by the driver. If there are no active device sections that the driver considers relevant, it must return FALSE. Allocate a ScrnInfoRec for each active instance of the hardware found, and fill in the basic information, including the other driver entry points. This is best done with the xf86ConfigIsaEntity() helper function for ISA instances or xf86ConfigPciEntity() for PCI instances. These functions allocate a ScrnInfoRec for active enti- ties. Optionally xf86AllocateScreen() function may also be used to allocate the ScrnInfoRec. Any of these func- tions take care of initialising fields to defined ``unused'' values. Claim the entities for each instance of the hardware found. This prevents other drivers from claiming the same hardware. Must leave hardware in the same state it found it in, and must not do any hardware initialisation. All detection can be overridden via the config file, and that parsed information is available to the driver at this stage. Returns TRUE if one or more instances are found, and FALSE otherwise. int xf86MatchDevice(const char *drivername, GDevPtr **driversectlist) This function takes the name of the driver and returns via driversectlist a list of device sections that match the driver name. The function return value is the number of matches found. If a fatal error is encountered the return value is -1. The caller should use xfree() to free *driversectlist when it is no longer needed. ScrnInfoPtr xf86AllocateScreen(DriverPtr drv, int flags) This function allocates a new ScrnInfoRec in the xf86Screens[] array. This function is normally called by the video driver ChipProbe() functions. The return value is a pointer to the newly allocated ScrnInfoRec. The scrnIndex, origIndex, module and drv fields are ini- tialised. The reference count in drv is incremented. The storage for any currently allocated ``privates'' pointers is also allocated and the privates field ini- tialised (the privates data is of course not allocated or initialised). This function never returns on failure. If the allocation fails, the server exits with a fatal error. The flags value is not currently used, and should be set to zero. At the completion of this, a list of ScrnInfoRecs have been allocated in the xf86Screens[] array, and the associated entities and fixed resources have been claimed. The following ScrnInfoRec fields must be initialised at this point: driverVersion driverName scrnIndex(*) origIndex(*) drv(*) module(*) name Probe PreInit ScreenInit EnterVT LeaveVT numEntities entityList access (*) These are initialised when the ScrnInfoRec is allocated, and not explic- itly by the driver. The following ScrnInfoRec fields must be initialised if the driver is going to use them: SwitchMode AdjustFrame FreeScreen ValidMode 5.9 Matching Screens This is done at the start of the first server generation only. After the Probe phase is finished, there will be some number of ScrnInfoRecs. These are then matched with the active Screen sections in the XF86Config, and those not having an active Screen section are deleted. If the number of remaining screens is 0, InitOutput() sets screenInfo.numScreens to 0 and returns. At this point the following fields of the ScrnInfoRecs must be initialised: confScreen 5.10 Allocate non-conflicting resources This is done at the start of the first server generation only. Before calling the drivers again, the resource information collected from the Probe phase is processed. This includes checking the extent of PCI resources for the probed devices, and resolving any conflicts in the relocatable PCI resources. It also reports conflicts, checks bus routing issues, and any- thing else that is needed to enable the entities for the next phase. If any drivers registered an EntityInit() function during the Probe phase, then they are called here. 5.11 Sort the Screens and pre-check Monitor Information This is done at the start of the first server generation only. The list of screens is sorted to match the ordering requested in the config file. The list of modes for each active monitor is checked against the monitor's parameters. Invalid modes are pruned. 5.12 PreInit This is done at the start of the first server generation only. For each ScrnInfoRec, enable access to the screens entities and call the ChipPreInit() function. Bool ChipPreInit(ScrnInfoRec screen, int flags) The purpose of this function is to find out all the information required to determine if the configuration is usable, and to initialise those parts of the ScrnInfoRec that can be set once at the beginning of the first server generation. The number of entities registered for the screen should be checked against the expected number (most drivers expect only one). The entity information for each of them should be retrieved (with xf86GetEntityInfo()) and checked for the correct bus type and that none of the sharable resources registered during the Probe phase was rejected. Access to resources for the entities that can be con- trolled in a device-independent way are enabled before this function is called. If the driver needs to access any resources that it has disabled in an EntityInit() function that it registered, then it may enable them here providing that it disables them before this function returns. This includes probing for video memory, clocks, ramdac, and all other HW info that is needed. It includes deter- mining the depth/bpp/visual and related info. It includes validating and determining the set of video modes that will be used (and anything that is required to determine that). This information should be determined in the least intru- sive way possible. The state of the HW must remain unchanged by this function. Although video memory (including MMIO) may be mapped within this function, it must be unmapped before returning. Driver specific information should be stored in a structure hooked into the ScrnInfoRec's driverPrivate field. Any other modules which require persistent data (ie data that persists across server generations) should be initialised in this function, and they should allocate a ``privates'' index to hook their data into by calling xf86AllocateScrnInfo- PrivateIndex(). The ``privates'' data is persistent. Helper functions for some of these things are provided at the XFree86 common level, and the driver can choose to make use of them. All additional resources that the screen needs must be registered here. This should be done with xf86Register- Resources(). If some of the fixed resources registered in the Probe phase are not needed or not decoded by the hardware when in the OPERATING server state, their status should be updated with xf86SetOperatingState(). Modules may be loaded at any point in this function, and all modules that the driver will need must be loaded before the end of this function. Either the xf86LoadSubModule() or the xf86LoadDrvSubModule() func- tion should be used to load modules depending on whether a ScrnInfoRec has been set up. A driver may unload a mod- ule within this function if it was only needed temporar- ily, and the xf86UnloadSubModule() function should be used to do that. Otherwise there is no need to explic- itly unload modules because the loader takes care of mod- ule dependencies and will unload submodules automatically if/when the driver module is unloaded. The bulk of the ScrnInfoRec fields should be filled out in this function. ChipPreInit() returns FALSE when the configuration is unusable in some way (unsupported depth, no valid modes, not enough video memory, etc), and TRUE if it is usable. It is expected that if the ChipPreInit() function returns TRUE, then the only reasons that subsequent stages in the driver might fail are lack or resources (like xalloc failures). All other possible reasons for failure should be determined by the ChipPreInit() function. The ScrnInfoRecs for screens where the ChipPreInit() fails are removed. If none remain, InitOutput() sets screenInfo.numScreens to 0 and returns. At this point, further fields of the ScrnInfoRecs would normally be filled in. Most are not strictly mandatory, but many are required by other layers and/or helper functions that the driver may choose to use. The documentation for those layers and helper functions indicates which they require. The following fields of the ScrnInfoRecs should be filled in if the driver is going to use them: monitor display depth pixmapBPP bitsPerPixel weight (>8bpp only) mask (>8bpp only) offset (>8bpp only) rgbBits (8bpp only) gamma defaultVisual maxHValue maxVValue virtualX virtualY displayWidth frameX0 frameY0 frameX1 frameY1 zoomLocked modePool modes currentMode progClock (TRUE if clock is programmable) chipset ramdac clockchip numClocks (if not programmable) clock[] (if not programmable) videoRam biosBase memBase memClk driverPrivate chipID chipRev pointer xf86LoadSubModule(ScrnInfoPtr pScrn, const char *name): and pointer xf86LoadDrvSubModule(DriverPtr drv, const char *name): Load a module that a driver depends on. This function loads the module name as a sub module of the driver. The return value is a handle identifying the new module. If the load fails, the return value will be NULL. If a driver needs to explicitly unload a module it has loaded in this way, the return value must be saved and passed to xf86UnloadSubModule() when unloading. void xf86UnloadSubModule(pointer module) Unloads the module referenced by module. module should be a pointer returned previously by xf86LoadSubModule() or xf86LoadDrvSubModule() . 5.13 Cleaning up Unused Drivers At this point it is known which screens will be in use, and which drivers are being used. Unreferenced drivers (and modules they may have loaded) are unloaded here. 5.14 Consistency Checks The parameters that must be global to the server, like pixmap formats, bitmap bit order, bitmap scanline unit and image byte order are compared for each of the screens. If a mismatch is found, the server exits with an appropriate message. 5.15 Check if Resource Control is Needed Determine if resource access control is needed. This is the case if more than one screen is used. If necessary the RAC wrapper module is loaded. 5.16 AddScreen (ScreenInit) At this point, the valid screens are known. AddScreen() is called for each of them, passing ChipScreenInit() as the argument. AddScreen() is a DIX function that allocates a new screenInfo.screen[] entry (aka pScreen), and does some basic initialisation of it. It then calls the ChipScreenInit() function, with pScreen as one of its arguments. If ChipScreenInit() returns FALSE, AddScreen() returns -1. Otherwise it returns the index of the screen. AddScreen() should only fail because of programming errors or failure to allocate resources (like memory). All configuration problems should be detected BEFORE this point. Bool ChipScreenInit(int index, ScreenPtr pScreen, int argc, char **argv) This is called at the start of each server generation. Fill in all of pScreen, possibly doing some of this by calling ScreenInit functions from other layers like mi, framebuffers (cfb, etc), and extensions. Decide which operations need to be placed under resource access control. The classes of operations are the frame buffer operations (RAC_FB), the pointer operations (RAC_CURSOR), the viewport change operations (RAC_VIEW- PORT) and the colormap operations (RAC_COLORMAP). Any operation that requires resources which might be disabled during OPERATING state should be set to use RAC. This can be specified separately for memory and IO resources (the racMemFlags and racIoFlags fields of the ScrnInfoRec respectively). Map any video memory or other memory regions. Save the video card state. Enough state must be saved so that the original state can later be restored. Initialise the initial video mode. The ScrnInfoRec's vtSema field should be set to TRUE just prior to changing the video hardware's state. The ChipScreenInit() function (or functions from other layers that it calls) should allocate entries in the ScreenRec's devPrivates area by calling Allo- cateScreenPrivateIndex() if it needs per-generation storage. Since the ScreenRec's devPrivates information is cleared for each server generation, this is the correct place to initialise it. After AddScreen() has successfully returned, the following ScrnInfoRec fields are initialised: pScreen racMemFlags racIoFlags The ChipScreenInit() function should initialise the CloseScreen and Save- Screen fields of pScreen. The old value of pScreen->CloseScreen should be saved as part of the driver's per-screen private data, allowing it to be called from ChipCloseScreen(). This means that the existing CloseScreen() function is wrapped. 5.17 Finalising RAC Initialisation After all the ChipScreenInit() functions have been called, each screen has registered its RAC requirements. This information is used to determine which shared resources are requested by more than one driver and set the access functions accordingly. This is done following these rules: 1. The sharable resources registered by each entity are compared. If a resource is registered by more than one entity the entity will be marked to indicate that it needs to share this resources type (IO or MEM). 2. A resource marked ``disabled'' during OPERATING state will be ignored entirely. 3. A resource marked ``unused'' will only conflict with an overlapping resource of an other entity if the second is actually in use during OPERATING state. 4. If an ``unused'' resource was found to conflict but the entity does not use any other resource of this type the entire resource type will be disabled for that entity. 5.18 Finishing InitOutput() At this point InitOutput() is finished, and all the screens have been setup in their initial video mode. 5.19 Mode Switching When a SwitchMode event is received, ChipSwitchMode() is called (when it exists): Bool ChipSwitchMode(int index, DisplayModePtr mode, int flags) Initialises the new mode for the screen identified by index;. The viewport may need to be adjusted also. 5.20 Changing Viewport When a Change Viewport event is received, ChipAdjustFrame() is called (when it exists): void ChipAdjustFrame(int index, int x, int y, int flags) Changes the viewport for the screen identified by index. It should be noted that many chipsets impose restrictions on where the viewport may be placed in the virtual reso- lution, either for alignment reasons, or to prevent the start of the viewport from being positioned within a pixel (as can happen in a 24bpp mode). After calculating the value the chipset's panning registers need to be set to for non-DGA modes, this function should recalculate the ScrnInfoRec's frameX0, frameY0, frameX1 and frameY1 fields to correspond to that value. If this is not done, switching to another mode might cause the position of a hardware cursor to change. 5.21 VT Switching When a VT switch event is received, xf86VTSwitch() is called. xf86VTSwitch() does the following: On ENTER: o enable port I/O access o save and initialise the bus/resource state o enter the SETUP server state o calls ChipEnterVT() for each screen o enter the OPERATING server state o validate GCs o Restore fb from saved pixmap for each screen o Enable all input devices On LEAVE: o Save fb to pixmap for each screen o validate GCs o enter the SETUP server state o calls ChipLeaveVT() for each screen o disable all input devices o restore bus/resource state o disables port I/O access Bool ChipEnterVT(int index, int flags) This function should initialise the current video mode and initialise the viewport, turn on the HW cursor if appropriate, etc. Should it re-save the video state before initialising the video mode? void ChipLeaveVT(int index, int flags) This function should restore the saved video state. If appropriate it should also turn off the HW cursor, and invalidate any pixmap/font caches. Optionally, ChipLeaveVT() may also unmap memory regions. If so, ChipEnterVT() will need to remap them. Additionally, if an aper- ture used to access video memory is unmapped and remapped in this fashion, ChipEnterVT() will also need to notify the framebuffer layers of the aperture's new location in virtual memory. This is done with a call to the screen's ModifyPixmapHeader() function, as follows (*pScreen->ModifyPixmapHeader)(pScrn->ppix, -1, -1, -1, -1, -1, NewApertureAddress); where the ``ppix'' field in a ScrnInfoRec points to the pixmap used by the screen's SaveRestoreImage() function to hold the screen's contents while switched out. Currently, aperture remapping, as described here, should not be attempted if the driver uses the xf8_16bpp or xf8_32bpp framebuffer layers. A pending restructuring of VT switching will address this restriction in the near future. Other layers may wrap the ChipEnterVT() and ChipLeaveVT() functions if they need to take some action when these events are received. 5.22 End of server generation At the end of each server generation, the DIX layer calls ChipCloseScreen() for each screen: Bool ChipCloseScreen(int index, ScreenPtr pScreen) This function should restore the saved video state and unmap the memory regions. It should also free per-screen data structures allocated by the driver. Note that the persistent data held in the ScrnInfoRec's driverPrivate field should not be freed here because it is needed by subsequent server genera- tions. The ScrnInfoRec's vtSema field should be set to FALSE once the video HW state has been restored. Before freeing the per-screen driver data the saved Clos- eScreen value should be restored to pScreen->CloseScreen, and that function should be called after freeing the data. 6. Optional Driver Functions The functions outlined here can be called from the XFree86 common layer, but their presence is optional. 6.1 Mode Validation When a mode validation helper supplied by the XFree86-common layer is being used, it can be useful to provide a function to check for hw specific mode constraints: ModeStatus ChipValidMode(int index, DisplayModePtr mode, Bool verbose, int flags) Check the passed mode for hw-specific constraints, and return the appropriate status value. This function may also modify the effective timings and clock of the passed mode. These have been stored in the mode's Crtc* and SynthClock elements, and have already been adjusted for interlacing, doublescanning, multiscanning and clock multipliers and dividers. The function should not modify any other mode field, unless it wants to modify the mode timings reported to the user by xf86PrintModes(). The function is called once for every mode in the XF86Config Monitor section assigned to the screen, with flags set to MODECHECK_INITIAL. It is subse- quently called for every mode in the XF86Config Display subsection assigned to the screen, with flags set to MODECHECK_FINAL. In the second case, the mode will have successfully passed all other tests. In addition, the ScrnIn- foRec's virtualX, virtualY and displayWidth fields will have been set as if the mode to be validated were to be the last mode accepted. In effect, calls with MODECHECK_INITIAL are intended for checks that do not depend on any mode other than the one being validated, while calls with MOD- ECHECK_FINAL are intended for checks that may involve more than one mode. 6.2 Free screen data When a screen is deleted prior to the completion of the ScreenInit phase the ChipFreeScreen() function is called when defined. void ChipFreeScreen(int scrnindex, int flags) Free any driver-allocated data that may have been allo- cated up to and including an unsuccessful Chip- ScreenInit() call. This would predominantly be data allocated by ChipPreInit() that persists across server generations. It would include the driverPrivate, and any ``privates'' entries that modules may have allocated. 7. Recommended driver functions The functions outlined here are for internal use by the driver only. They are entirely optional, and are never accessed directly from higher layers. The sample function declarations shown here are just examples. The interface (if any) used is up to the driver. 7.1 Save Save the video state. This could be called from ChipScreenInit() and (possi- bly) ChipEnterVT(). void ChipSave(ScrnInfoPtr pScrn) Saves the current state. This will only be saving pre- server states or states before returning to the server. There is only one current saved state per screen and it is stored in private storage in the screen. 7.2 Restore Restore the original video state. This could be called from the ChipLeaveVT() and ChipCloseScreen() functions. void ChipRestore(ScrnInfoPtr pScrn) Restores the saved state from the private storage. Usu- ally only used for restoring text modes. 7.3 Initialise Mode Initialise a video mode. This could be called from the ChipScreenInit(), ChipSwitchMode() and ChipEnterVT() functions. Bool ChipModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode) Programs the hardware for the given video mode. 8. Data and Data Structures 8.1 Command line data Command line options are typically global, and are stored in global vari- ables. These variables are read-only and are available to drivers via a function call interface. Most of these command line values are processed via helper functions to ensure that they are treated consistently by all drivers. The other means of access is provided for cases where the supplied helper functions might not be appropriate. Some of them are: xf86Verbose verbosity level xf86Bpp -bpp from the command line xf86Depth -depth from the command line xf86Weight -weight from the command line xf86Gamma -{r,g,b,}gamma from the command line xf86FlipPixels -flippixels from the command line xf86ProbeOnly -probeonly from the command line defaultColorVisualClass -cc from the command line If we ever do allow for screen-specific command line options, we may need to rethink this. These can be accessed in a read-only manner by drivers with the following functions: int xf86GetVerbosity() Returns the value of xf86Verbose. int xf86GetDepth() Returns the -depth command line setting. If not set on the command line, -1 is returned. rgb xf86GetWeight() Returns the -weight command line setting. If not set on the command line, {0, 0, 0} is returned. Gamma xf86GetGamma() Returns the -gamma or -rgamma, -ggamma, -bgamma command line settings. If not set on the command line, {0.0, 0.0, 0.0} is returned. Bool xf86GetFlipPixels() Returns TRUE if -flippixels is present on the command line, and FALSE otherwise. const char *xf86GetServerName() Returns the name of the X server from the command line. 8.2 Data handling Config file data contains parts that are global, and parts that are Screen specific. All of it is parsed into data structures that neither the drivers or most other parts of the server need to know about. The global data is typically not required by drivers, and as such, most of it is stored in the private xf86InfoRec. The screen-specific data collected from the config file is stored in screen, device, display, monitor-specific data structures that are separate from the ScrnInfoRecs, with the appropriate elements/fields hooked into the ScrnIn- foRecs as required. The screen config data is held in confScreenRec, device data in the GDevRec, monitor data in the MonRec, and display data in the Dis- pRec. The XFree86 common layer's screen specific data (the actual data in use for each screen) is held in the ScrnInfoRecs. As has been outlined above, the ScrnInfoRecs are allocated at probe time, and it is the responsibility of the Drivers' Probe() and PreInit() functions to finish filling them in based on both data provided on the command line and data provided from the Config file. The precedence for this is: command line -> config file -> probed/default data For most things in this category there are helper functions that the drivers can use to ensure that the above precedence is consistently used. As well as containing screen-specific data that the XFree86 common layer (including essential parts of the server infrastructure as well as helper functions) needs to access, it also contains some data that drivers use internally. When considering whether to add a new field to the ScrnInfoRec, consider the balance between the convenience of things that lots of drivers need and the size/obscurity of the ScrnInfoRec. Per-screen driver specific data that cannot be accommodated with the static ScrnInfoRec fields is held in a driver-defined data structure, a pointer to which is assigned to the ScrnInfoRec's driverPrivate field. This is per- screen data that persists across server generations (as does the bulk of the static ScrnInfoRec data). It would typically also include the video card's saved state. Per-screen data for other modules that the driver uses (for example, the XAA module) that is reset for each server generation is hooked into the ScrnIn- foRec through it's privates field. Once it has stabilised, the data structures and variables accessible to video drivers will be documented here. In the meantime, those things defined in the xf86.h and xf86str.h files are visible to video drivers. Things defined in xf86Priv.h and xf86Privstr.h are NOT intended to be visible to video driv- ers, and it is an error for a driver to include those files. 8.3 Accessing global data Some other global state information that the drivers may access via functions is as follows: Bool xf86ServerIsExiting() Returns TRUE if the server is at the end of a generation and is in the process of exiting, and FALSE otherwise. Bool xf86ServerIsResetting() Returns TRUE if the server is at the end of a generation and is in the process of resetting, and FALSE otherwise. Bool xf86ServerIsInitialising() Returns TRUE if the server is at the beginning of a gen- eration and is in the process of initialising, and FALSE otherwise. Bool xf86ServerIsOnlyProbing() Returns TRUE if the -probeonly command line flag was specified, and FALSE otherwise. Bool xf86CaughtSignal() Returns TRUE if the server has caught a signal, and FALSE otherwise. 8.4 Allocating private data A driver and any module it uses may allocate per-screen private storage in either the ScreenRec (DIX level) or ScrnInfoRec (XFree86 common layer level). ScreenRec storage persists only for a single server generation, and ScrnIn- foRec storage persists across generations for the lifetime of the server. The ScreenRec devPrivates data must be reallocated/initialised at the start of each new generation. This is normally done from the ChipScreenInit() function, and Init functions for other modules that it calls. Data allocated in this way should be freed by the driver's ChipCloseScreen() functions, and Close functions for other modules that it calls. A new devPrivates entry is allocated by calling the AllocateScreenPrivateIndex() function. int AllocateScreenPrivateIndex() This function allocates a new element in the devPrivates field of all currently existing ScreenRecs. The return value is the index of this new element in the devPrivates array. The devPrivates field is of type DevUnion: typedef union _DevUnion { pointer ptr; long val; unsigned long uval; pointer (*fptr)(void); } DevUnion; which allows the element to be used for any of the above types. It is commonly used as a pointer to data that the caller allocates after the new index has been allocated. This function will return -1 when there is an error allo- cating the new index. The ScrnInfoRec privates data persists for the life of the server, so only needs to be allocated once. This should be done from the ChipPreInit() func- tion, and Init functions for other modules that it calls. Data allocated in this way should be freed by the driver's ChipFreeScreen() functions, and Free functions for other modules that it calls. A new privates entry is allocated by calling the xf86AllocateScrnInfoPrivateIndex() function. int xf86AllocateScrnInfoPrivateIndex() This function allocates a new element in the privates field of all currently existing ScrnInfoRecs. The return value is the index of this new element in the privates array. The privates field is of type DevUnion: typedef union _DevUnion { pointer ptr; long val; unsigned long uval; pointer (*fptr)(void); } DevUnion; which allows the element to be used for any of the above types. It is commonly used as a pointer to data that the caller allocates after the new index has been allocated. This function will not return when there is an error allocating the new index. When there is an error it will cause the server to exit with a fatal error. The similar function for allocation privates in the ScreenRec (Allo- cateScreenPrivateIndex()) differs in this respect by returning -1 when the allocation fails. 9. Keeping Track of Bus Resources 9.1 Theory of Operation The XFree86 common layer has knowledge of generic access control mechanisms for devices on certain bus systems (currently the PCI bus) as well as of methods to enable or disable access to the buses itself. Furthermore it can access information on resources decoded by these devices and if necessary modify it. When first starting the Xserver collects all this information, saves it for restoration, checks it for consistency, and if necessary, corrects it. Finally it disables all resources on a generic level prior to calling any driver function. When the Probe() function of each driver is called the device sections are matched against the devices found in the system. The driver may probe devices at this stage that cannot be identified by using device independent methods. Access to all resources that can be controlled in a device indepen- dent way is disabled. The Probe() function should register all non-relocat- able resources at this stage. If a resource conflict is found between exclu- sive resources the driver will fail immediately. Optionally the driver might specify an EntityInit(), EntityLeave() and EntityEnter() function. EntityInit() can be used to disable any shared resources that are not con- trolled by the generic access control functions. It is called prior to the PreInit phase regardless if an entity is active or not. When calling the EntityInit(), EntityEnter() and EntityLeave() functions the common level will disable access to all other entities on a generic level. Since the common level has no knowledge of device specific methods to disable access to resources it cannot be guaranteed that certain resources are not decoded by any other entity until the EntityInit() or EntityEnter() phase is finished. Device drivers should therefore register all those resources which they are going to disable. If these resources are never to be used by any driver function they may be flagged ResInit so that they can be removed from the resource list after processing all EntityInit() functions. EntityEnter() should disable decoding of all resources which are not registered as exclu- sive and which are not handled by the generic access control in the common level. The difference to EntityInit() is that the latter one is only called once during lifetime of the server. It can therefore be used to set up vari- ables prior to disabling resources. EntityLeave() should restore the origi- nal state when exiting the server or switching to a different VT. It also needs to disable device specific access functions if they need to be disabled on server exit or VT switch. The default state is to enable them before giv- ing up the VT. In PreInit() phase each driver should check if any sharable resources it has registered during Probe() has been denied and take appropriate action which could simply be to fail. If it needs to access resources it has disabled during EntitySetup() it can do so provided it has registered these and will disable them before returning from PreInit(). This also applies to all other driver functions. Several functions are provided to request resource ranges, register these, correct PCI config space and add replacements for the generic access functions. Resources may be marked ``disabled'' or ``unused'' during OPERATING stage. Although these steps could also be performed in ScreenInit(), this is not desirable. Following PreInit() phase the common level determines if resource access con- trol is needed. This is the case if more than one screen is used. If neces- sary the RAC wrapper module is loaded. In ScreenInit() the drivers can decide which operations need to be placed under RAC. Available are the frame buffer operations, the pointer operations and the colormap operations. Any operation that requires resources which might be disabled during OPERATING state should be set to use RAC. This can be specified separately for memory and IO resources. When ScreenInit() phase is done the common level will determine which shared resources are requested by more than one driver and set the access functions accordingly. This is done following these rules: 1. The sharable resources registered by each entity are compared. If a resource is registered by more than one entity the entity will be marked to need to share this resources type (IO or MEM). 2. A resource marked ``disabled'' during OPERATING state will be ignored entirely. 3. A resource marked ``unused'' will only conflicts with an overlapping resource of an other entity if the second is actually in use during OPERATING state. 4. If an ``unused'' resource was found to conflict however the entity does not use any other resource of this type the entire resource type will be disabled for that entity. The driver has the choice among different ways to control access to certain resources: 1. It can rely on the generic access functions. This is probably the most common case. Here the driver only needs to register any resource it is going to use. 2. It can replace the generic access functions by driver specific ones. This will mostly be used in cases where no generic access functions are available. In this case the driver has to make sure these resources are disabled when entering the PreInit() stage. Since the replacement functions are registered in PreInit() the driver will have to enable these resources itself if it needs to access them during this state. The driver can specify if the replacement functions can control memory and/or I/O resources separately. 3. The driver can enable resources itself when it needs them. Each driver function enabling them needs to disable them before it will return. This should be used if a resource which can be controlled in a device dependent way is only required during SETUP state. This way it can be marked ``unused'' during OPERATING state. A resource which is decoded during OPERATING state however never accessed by the driver should be marked unused. Since access switching latencies are an issue during Xserver operation, the common level attempts to minimize the number of entities that need to be placed under RAC control. When a wrapped operation is called, the EnableAc- cess() function is called before control is passed on. EnableAccess() checks if a screen is under access control. If not it just establishes bus routing and returns. If the screen needs to be under access control, EnableAccess() determines which resource types (MEM, IO) are required. Then it tests if this access is already established. If so it simply returns. If not it dis- ables the currently established access, fixes bus routing and enables access to all entities registered for this screen. Whenever a mode switch or a VT-switch is performed the common level will return to SETUP state. 9.2 Resource Types Resource have certain properties. When registering resources each range is accompanied by a flag consisting of the ORed flags of the different proper- ties the resource has. Each resource range may be classified according to o its physical properties i.e., if it addresses memory (ResMem) or I/O space (ResIo), o if it addresses a block (ResBlock) or sparse (ResSparse) range, o its access properties. There are two known access properties: o ResExclusive for resources which may not be shared with any other device and o ResShared for resources which can be disabled and therefore can be shared. If it is necessary to test a resource against any type a generic access type ResAny is provided. If this is set the resource will conflict with any resource of a different entity intersecting its range. Further it can be specified that a resource is decoded however never used during any stage (ResUnused) or during OPERATING state (ResUnusedOpr). A resource only visi- ble during the init functions (ie. EntityInit(), EntityEnter() and EntityLeave() should be registered with the flag ResInit. A resource that might conflict with background resource ranges may be flagged with ResBios. This might be useful when registering resources ranges that were assigned by the system Bios. Several predefined resource lists are available for VGA and 8514/A resources in common/xf86Resources.h. 9.3 Available Functions The functions provided for resource management are listed in their order of use in the driver. 9.3.1 Probe Phase In this phase each driver detects those resources it is able to drive, cre- ates an entity record for each of them, registers non-relocatable resources and allocates screens and adds the resources to screens. Two helper functions are provided for matching device sections in the XF86Config file to the devices: int xf86MatchPciInstances(const char *driverName, int vendorID, SymTabPtr chipsets, PciChipsets *PCIchipsets, GDevPtr *devList, int numDevs, DriverPtr drvp, int **foundEntities) This function finds matches between PCI cards that a driver supports and config file device sections. It is intended for use in the ChipProbe() function of drivers for PCI cards. Only probed PCI devices with a vendor ID matching vendorID are considered. devList and numDevs are typically those found from calling xf86MatchDevice(), and represent the active config file device sections rel- evant to the driver. PCIchipsets is a table that pro- vides a mapping between the PCI device IDs, the driver's internal chipset tokens and a list of fixed resources. When a device section doesn't have a BusID entry it can only match the primary video device. Secondary devices are only matched with device sections that have a match- ing BusID entry. Once the preliminary matches have been found, a final match is confirmed by checking if the chipset override, ChipID override or probed PCI chipset type match one of those given in the chipsets and PCIchipsets lists. The PCIchipsets list includes a list of the PCI device IDs supported by the driver. The list should be terminated with an entry with PCI ID -1". The chipsets list is a table mapping the driver's internal chipset tokens to names, and should be terminated with a NULL entry. Only those entries with a corresponding entry in the PCI- chipsets list are considered. The order of precedence is: config file chipset, config file ChipID, probed PCI device ID. In cases where a driver handles PCI chipsets with more than one vendor ID, it may set vendorID to 0, and OR each devID in the list with (the vendor ID << 16). Entity index numbers for confirmed matches are returned as an array via foundEntities. The PCI information, chipset token and device section for each match are found in the EntityInfoRec referenced by the indices. The function return value is the number of confirmed matches. A return value of -1 indicates an internal error. The returned foundEntities array should be freed by the driver with xfree() when it is no longer needed in cases where the return value is greater than zero. int xf86MatchIsaInstances(const char *driverName, SymTabPtr chipsets, IsaChipsets *ISAchipsets, DriverPtr drvp, FindIsaDevProc FindIsaDevice, GDevPtr *devList, int numDevs, int **foundEntities) This function finds matches between ISA cards that a driver supports and config file device sections. It is intended for use in the ChipProbe() function of drivers for ISA cards. devList and numDevs are typically those found from calling xf86MatchDevice(), and represent the active config file device sections relevant to the driver. ISAchipsets is a table that provides a mapping between the driver's internal chipset tokens and the resource classes. FindIsaDevice is a driver-provided function that probes the hardware and returns the chipset token corresponding to what was detected, and -1 if noth- ing was detected. If the config file device section contains a chipset entry, then it is checked against the chipsets list. When no chipset entry is present, the FindIsaDevice func- tion is called instead. Entity index numbers for confirmed matches are returned as an array via foundEntities. The chipset token and device section for each match are found in the EntityIn- foRec referenced by the indices. The function return value is the number of confirmed matches. A return value of -1 indicates an internal error. The returned foundEntities array should be freed by the driver with xfree() when it is no longer needed in cases where the return value is greater than zero. These two helper functions make use of several core functions that are avail- able at the driver level: Bool xf86ParsePciBusString(const char *busID, int *bus, int *device, int *func) Takes a BusID string, and if it is in the correct format, returns the PCI bus, device, func values that it indi- cates. The format of the string is expected to be "PCI:bus:device:func" where each of `bus', `device' and `func' are decimal integers. The ":func" part may be omitted, and the func value assumed to be zero, but this isn't encouraged. The "PCI" prefix may also be omitted. The prefix "AGP" is currently equivalent to the "PCI" prefix. If the string isn't a valid PCI BusID, the return value is FALSE. Bool xf86ComparePciBusString(const char *busID, int bus, int device, int func) Compares a BusID string with PCI bus, device, func val- ues. If they match TRUE is returned, and FALSE if they don't. Bool xf86ParseIsaBusString(const char *busID) Compares a BusID string with the ISA bus ID string ("ISA" or "ISA:"). If they match TRUE is returned, and FALSE if they don't. Bool xf86CheckPciSlot(int bus, int device, int func) Checks if the PCI slot bus:device:func has been claimed. If so, it returns FALSE, and otherwise TRUE. int xf86ClaimPciSlot(int bus, int device, int func, DriverPtr drvp, int chipset, GDevPtr dev, Bool active) This function is used to claim a PCI slot, allocate the associated entity record and initialise their data struc- tures. The return value is the index of the newly allo- cated entity record, or -1 if the claim fails. This function should always succeed if xf86CheckPciSlot() returned TRUE for the same PCI slot. Bool xf86IsPrimaryPci(void) This function returns TRUE if the primary card is a PCI device, and FALSE otherwise. int xf86ClaimIsaSlot(DriverPtr drvp, int chipset, GDevPtr dev, Bool active) This allocates an entity record entity and initialise the data structures. The return value is the index of the newly allocated entity record. Bool xf86IsPrimaryIsa(void) This function returns TRUE if the primary card is an ISA (non-PCI) device, and FALSE otherwise. Two helper functions are provided to aid configuring entities: ScrnInfoPtr xf86ConfigPciEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex, PciChipsets *p_chip, resList res, EntityProc init, EntityProc enter, EntityProc leave, pointer private) ScrnInfoPtr xf86ConfigIsaEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex, IsaChipsets *i_chip, resList res, EntityProc init, EntityProc enter, EntityProc leave, pointer private) These functions are used to register the non-relocatable resources for an entity, and the optional entity-specific Init, Enter and Leave functions. Usually the list of fixed resources is obtained from the Isa/PciChipsets lists. However an additional list of resources may be passed. Generally this is not required. For active entities a ScrnInfoRec is allocated if the pScrn argument is NULL. The return value is TRUE when successful. The init, enter, leave functions are defined as follows: typedef void (*EntityProc)(int entityIndex, pointer private) They are passed the entity index and a pointer to a pri- vate scratch area. This can be set up during Probe() and its address can be passed to xf86ConfigIsaEntity() and xf86ConfigPciEntity() as the last argument. These two helper functions make use of several core functions that are avail- able at the driver level: void xf86ClaimFixedResources(resList list, int entityIndex) This function registers the non-relocatable resources which cannot be disabled and which therefore would cause the server to fail immediately if they were found to con- flict. It also records non-relocatable but sharable resources for processing after the Probe() phase. Bool xf86SetEntityFuncs(int entityIndex, EntityProc init, EntityProc enter, EntityProc leave, pointer) This function registers with an entity the init, enter, leave functions along with the pointer to their private area. void xf86AddEntityToScreen(ScrnInfoPtr pScrn, int entityIndex) This function associates the entity referenced by enti- tyIndex with the screen. 9.3.2 PreInit Phase During this phase the remaining resources should be registered. PreInit() should call xf86GetEntityInfo() to obtain a pointer to an EntityInfoRec for each entity it is able to drive and check if any resource are listed in its resources field. If resources registered in the Probe phase have been rejected in the post-Probe phase (resources is non-NULL), then the driver should decide if it can continue without using these or if it should fail. EntityInfoPtr xf86GetEntityInfo(int entityIndex) This function returns a pointer to the EntityInfoRec ref- erenced by entityIndex. The returned EntityInfoRec should be freed with xfree() when no longer needed. Several functions are provided to simplify resource registration: Bool xf86IsEntityPrimary(int entityIndex) This function returns TRUE if the entity referenced by entityIndex is the primary display device (i.e., the one initialised at boot time and used in text mode). Bool xf86IsScreenPrimary(int scrnIndex) This function returns TRUE if the primary entity is reg- istered with the screen referenced by scrnIndex. pciVideoPtr xf86GetPciInfoForEntity(int entityIndex) This function returns a pointer to the pciVideoRec for the specified entity. If the entity is not a PCI device, NULL is returned. The primary function for registration of resources is: resPtr xf86RegisterResources(int entityIndex, resList list, int access) This function tries to register the resources in list. If list is NULL it tries to determine the resources auto- matically. This only works for entities that provide a generic way to read out the resource ranges they decode. So far this is only the case for PCI devices. By default the PCI resources are registered as shared (ResShared) if the driver wants to set a different access type it can do so by specifying the access flags in the third argument. A value of 0 means to use the default settings. If for any reason the resource broker is not able to register some of the requested resources the function will return a pointer to a list of the failed ones. In this case the driver may be able to move the resource to different locations. In case of PCI bus entities this is done by passing the list of failed resources to xf86ReallocateP- ciResources(). When the registration succeeds, the return value is NULL. resPtr xf86ReallocatePciResources(int entityIndex, resPtr pRes) This function takes a list of PCI resources that need to be reallocated and returns NULL when all relocations are successful. xf86RegisterResources() should be called again to register the relocated resources with the bro- ker. If the reallocation fails, a list of the resources that could not be relocated is returned. Two functions are provided to obtain a resource range of a given type: resRange xf86GetBlock(long type, memType size, memType window_start, memType window_end, memType align_mask, resPtr avoid) This function tries to find a block range of size size and type type in a window bound by window_start and win- dow_end with the alignment specified in align_mask. Optionally a list of resource ranges which should be avoided within the window can be supplied. On failure a zero-length range of type ResEnd will be returned. resRange xf86GetSparse(long type, memType fixed_bits, memType decode_mask, memType address_mask, resPtr avoid) This function is like the previous one, but attempts to find a sparse range instead of a block range. Here three values have to be specified: the address_mask which marks all bits of the mask part of the address, the decode_mask which masks out the bits which are hardcoded and are therefore not available for relocation and the values of the fixed bits. The function tries to find a base that satisfies the given condition. If the function fails it will return a zero range of type ResEnd. Optionally it might be passed a list of resource ranges to avoid. Some PCI devices are broken in the sense that they return invalid size infor- mation for a certain resource. In this case the driver can supply the cor- rect size and make sure that the resource range allocated for the card is large enough to hold the address range decoded by the card. The function xf86FixPciResource() can be used to do this: Bool xf86FixPciResource(int entityIndex, unsigned int prt, CARD32 alignment, long type) This function fixes a PCI resource allocation. The prt parameter contains the number of the PCI base register that needs to be fixed (0-5, and 6 for the BIOS base reg- ister). The size is specified by the alignment. Since PCI resources need to span an integral range of size 2^n, the alignment also specifies the number of addresses that will be decoded. If the driver specifies a type mask it can override the default type for PCI resources which is ResShared. The resource broker needs to know that to find a matching resource range. This function should be called before calling xf86RegisterResources(). The return value is TRUE when the function succeeds. Bool xf86CheckPciMemBase(pciVideoPtr pPci, memType base) This function checks that the memory base address speci- fied matches one of the PCI base address register values for the given PCI device. This is mostly used to check that an externally provided base address (e.g., from a config file) matches an actual value allocated to a device. The driver may replace the generic access control functions for an entity. This is done with the xf86SetAccessFuncs(): void xf86SetAccessFuncs(EntityInfoPtr pEnt, xf86SetAccessFuncPtr funcs, xf86SetAccessFuncPtr oldFuncs) with: typedef struct { xf86AccessPtr mem; xf86AccessPtr io; xf86AccessPtr io_mem; } xf86SetAccessFuncRec, *xf86SetAccessFuncPtr; The driver can pass three functions: one for I/O access, one for memory access and one for combined memory and I/O access. If the memory access and combined access func- tions are identical the common level assumes that the memory access cannot be controlled independently of I/O access, if the I/O access function and the combined access functions are the same it is assumed that I/O can not be controlled independently. If memory and I/O have to be controlled together all three values should be the same. If a non NULL value is passed as third argument it is interpreted as an address where to store the old access record. If the third argument is NULL it will be assumed that the generic access should be enabled before replacing the access functions. Otherwise it will be disabled. The driver may enable them itself using the returned values. It should do this from its replacement access functions as the generic access may be disabled by the common level on certain occasions. If replacement functions are specified they must control all resources of the specific type registered for the entity. To find out if a specific resource range conflicts with another resource the xf86ChkConflict() function may be used: memType xf86ChkConflict(resRange *rgp, int entityIndex) This function checks if the resource range rgp of for the specified entity conflicts with with another resource. If a conflict is found, the address of the start of the conflict is returned. The return value is zero when there is no conflict. The OPERATING state properties of previously registered fixed resources can be set with the xf86SetOperatingState() function: resPtr xf86SetOperatingState(resList list, int entityIndex, int mask) This function is used to set the status of a resource during OPERATING state. list holds a list to which mask is to be applied. The parameter mask may have the value ResUnusedOpr and ResDisableOpr. The first one should be used if a resource isn't used by the driver during OPER- ATING state although it is decoded by the device, while the latter one indicates that the resource is not decoded during OPERATING state. Note that the resource ranges have to match those specified during registration. If a range has been specified starting at A and ending at B and suppose C us a value satisfying A < C < B one may not specify the resource range (A,B) by splitting it into two ranges (A,C) and (C,B). The following two functions are provided for special cases: void xf86RemoveEntityFromScreen(ScrnInfoPtr pScrn, int entityIndex) This function may be used to remove an entity from a screen. This only makes sense if a screen has more than one entity assigned or the screen is to be deleted. No test is made if the screen has any entities left. void xf86DeallocateResourcesForEntity(int entityIndex, long type) This function deallocates all resources of a given type registered for a certain entity from the resource broker list. 9.3.3 ScreenInit Phase All that is required in this phase is to setup the RAC flags. Note that it is also permissible to set these flags up in the PreInit phase. The RAC flags are held in the racIoFlags and racMemFlags fields of the ScrnInfoRec for each screen. They specify which graphics operations might require the use of shared resources. This can be specified separately for memory and I/O resources. The available flags are defined in rac/xf86RAC.h. They are: RAC_FB for framebuffer operations (including hw acceleration) RAC_CURSOR for Cursor operations (??? I'm not sure if we need this for SW cur- sor it depends on which level the sw cursor is drawn) RAC_COLORMAP for colormap operations RAC_VIEWPORT for the call to ChipAdjustFrame() The flags are ORed together. 10. Config file ``Option'' entries Option entries are permitted in most sections and subsections of the config file. There are two forms of option entries: Option "option-name" A boolean option. Option "option-name" "option-value" An option with an arbitrary value. The option entries are handled by the parser, and a list of the parsed options is included with each of the appropriate data structures that the drivers have access to. The data structures used to hold the option informa- tion are opaque to the driver, and a driver must not access the option data directly. Instead, the common layer provides a set of functions that may be used to access, check and manipulate the option data. First, the low level option handling functions. In most cases drivers would not need to use these directly. pointer xf86FindOption(pointer options, const char *name) Takes a list of options and an option name, and returns a handle for the first option entry in the list matching the name. Returns NULL if no match is found. char *xf86FindOptionValue(pointer options, const char *name) Takes a list of options and an option name, and returns the value associated with the first option entry in the list matching the name. If the matching option has no value, an empty string ("") is returned. Returns NULL if no match is found. void xf86MarkOptionUsed(pointer option) Takes a handle for an option, and marks that option as used. void xf86MarkOptionUsedByName(pointer options, const char *name) Takes a list of options and an option name and marks the first option entry in the list matching the name as used. Next, the higher level functions that most drivers would use. void xf86CollectOptions(ScrnInfoPtr pScrn, pointer extraOpts) Collect the options from each of the config file sections used by the screen (pScrn) and return the merged list as pScrn->options. This function requires that pScrn->conf- Screen, pScrn->display, pScrn->monitor, pScrn->numEnti- ties, and pScrn->entityList are initialised. extraOpts may optionally be set to an additional list of options to be combined with the others. The order of precedence for options is extraOpts, display, confScreen, monitor, device. void xf86ProcessOptions(int scrnIndex, pointer options, OptionInfoPtr optinfo) Processes a list of options according to the information in the array of OptionInfoRecs (optinfo). The resulting information is stored in the value fields of the appro- priate optinfo entries. The found fields are set to TRUE when an option with a value of the correct type if found, and FALSE otherwise. The type field is used to determine the expected value type for each option. Each option in the list of options for which there is a name match (but not necessarily a value type match) is marked as used. Warning messages are printed when option values don't match the types specified in the optinfo data. NOTE: If this function is called before a driver's screen number is known (e.g., from the ChipProbe() function) a scrnIndex value of -1 should be used. NOTE 2: Given that this function stores into the Option- InfoRecs pointed to by optinfo, the caller should ensure the OptionInfoRecs are (re-)initialised before the call, especially if the caller expects to use the predefined option values as defaults. The OptionInfoRec is defined as follows: typedef struct { double freq; int units; } OptFrequency; typedef union { unsigned long num; char * str; double realnum; Bool bool; OptFrequency freq; } ValueUnion; typedef enum { OPTV_NONE = 0, OPTV_INTEGER, OPTV_STRING, /* a non-empty string */ OPTV_ANYSTR, /* Any string, including an empty one */ OPTV_REAL, OPTV_BOOLEAN, OPTV_FREQ } OptionValueType; typedef enum { OPTUNITS_HZ = 1, OPTUNITS_KHZ, OPTUNITS_MHZ } OptFreqUnits; typedef struct { int token; const char* name; OptionValueType type; ValueUnion value; Bool found; } OptionInfoRec, *OptionInfoPtr; OPTV_FREQ can be used for options values that are fre- quencies. These values are a floating point number with an optional unit name appended. The unit name can be one of "Hz", "kHz", "k", "MHz", "M". The multiplier associ- ated with the unit is stored in freq.units, and the scaled frequency is stored in freq.freq. When no unit is specified, freq.units is set to 0, and freq.freq is unscaled. Typical usage is to setup an array of OptionInfoRecs with all fields initialised. The value and found fields get set by xf86ProcessOptions(). For cases where the value parsing is more complex, the driver should specify OPTV_STRING, and parse the string itself. An example of using this option handling is included in the Sample Driver (section 20., page 1) section. void xf86ShowUnusedOptions(int scrnIndex, pointer options) Prints out warning messages for each option in the list of options that isn't marked as used. This is intended to show options that the driver hasn't recognised. It would normally be called near the end of the Chip- ScreenInit() function, but only when serverGenera- tion == 1. OptionInfoPtr xf86TokenToOptinfo(const OptionInfoRec *table, int token) Returns a pointer to the OptionInfoRec in table with a token field matching token. Returns NULL if no match is found. Bool xf86IsOptionSet(const OptionInfoRec *table, int token) Returns the found field of the OptionInfoRec in table with a token field matching token. This can be used for options of all types. Note that for options of type OPTV_BOOLEAN, it isn't sufficient to check this to deter- mine the value of the option. Returns FALSE if no match is found. char *xf86GetOptValString(const OptionInfoRec *table, int token) Returns the value.str field of the OptionInfoRec in table with a token field matching token. Returns NULL if no match is found. Bool xf86GetOptValInteger(const OptionInfoRec *table, int token, int *value) Returns via *value the value.num field of the OptionIn- foRec in table with a token field matching token. *value is only changed when a match is found so it can be safely initialised with a default prior to calling this func- tion. The function return value is as for xf86IsOption- Set(). Bool xf86GetOptValULong(const OptionInfoRec *table, int token, unsigned long *value) Like xf86GetOptValInteger(), except the value is treated as an unsigned long. Bool xf86GetOptValReal(const OptionInfoRec *table, int token, double *value) Like xf86GetOptValInteger(), except that value.realnum is used. Bool xf86GetOptValFreq(const OptionInfoRec *table, int token, OptFreqUnits expectedUnits, double *value) Like xf86GetOptValInteger(), except that the value.freq data is returned. The frequency value is scaled to the units indicated by expectedUnits. The scaling is exact when the units were specified explicitly in the option's value. Otherwise, the expectedUnits field is used as a hint when doing the scaling. In this case, values larger than 1000 are assumed to have be specified in the next smallest units. For example, if the Option value is "10000" and expectedUnits is OPTUNITS_MHZ, the value returned is 10. Bool xf86GetOptValBool(const OptionInfoRec *table, int token, Bool *value) This function is used to check boolean options (OPTV_BOOLEAN). If the function return value is FALSE, it means the option wasn't set. Otherwise *value is set to the boolean value indicated by the option's value. No option value is interpreted as TRUE. Option values mean- ing TRUE are "1", "yes", "on", "true", and option values meaning FALSE are "0", "no", "off", "false". Option names both with the "no" prefix in their names, and with that prefix removed are also checked and handled in the obvious way. *value is not changed when the option isn't present. It should normally be set to a default value before calling this function. Bool xf86ReturnOptValBool(const OptionInfoRec *table, int token, Bool def) This function is used to check boolean options (OPTV_BOOLEAN). If the option is set, its value is returned. If the options is not set, the default value specified by def is returned. The option interpretation is the same as for xf86GetOptValBool(). int xf86NameCmp(const char *s1, const char *s2) This function should be used when comparing strings from the config file with expected values. It works like str- cmp(), but is not case sensitive and space, tab, and `_' characters are ignored in the comparison. The use of this function isn't restricted to parsing option values. It may be used anywhere where this functionality required. 11. Modules, Drivers, Include Files and Interface Issues NOTE: this section is incomplete. 11.1 Include files The following include files are typically required by video drivers: All drivers should include these: "xf86.h" "xf86_OSproc.h" "xf86_ansic.h" "xf86Resources.h" Wherever inb/outb (and related things) are used the following should be included: "compiler.h" Note: in drivers, this must be included after "xf86_ansic.h". Drivers that need to access PCI vendor/device definitions need this: "xf86PciInfo.h" Drivers that need to access the PCI config space need this: "xf86Pci.h" Drivers that initialise a SW cursor need this: "mipointer.h" All drivers implementing backing store need this: "mibstore.h" All drivers using the mi colourmap code need this: "micmap.h" If a driver uses the vgahw module, it needs this: "vgaHW.h" Drivers supporting VGA or Hercules monochrome screens need: "xf1bpp.h" Drivers supporting VGA or EGC 16-colour screens need: "xf4bpp.h" Drivers using cfb need: #define PSZ 8 #include "cfb.h" #undef PSZ Drivers supporting bpp 16, 24 or 32 with cfb need one or more of: "cfb16.h" "cfb24.h" "cfb32.h" If a driver uses XAA, it needs this: "xaa.h" If a driver uses the fb manager, it needs this: "xf86fbman.h" Non-driver modules should include "xf86_ansic.h" to get the correct wrapping of ANSI C/libc functions. All modules must NOT include any system include files, or the following: "xf86Priv.h" "xf86Privstr.h" "xf86_OSlib.h" "Xos.h" In addition, "xf86_libc.h" must not be included explicitly. It is included implicitly by "xf86_ansic.h". 12. Offscreen Memory Manager Management of offscreen video memory may be handled by the XFree86 frame- buffer manager. Once the offscreen memory manager is running, drivers or extensions may allocate, free or resize areas of offscreen video memory using the following functions (definitions taken from xf86fbman.h): typedef struct _FBArea { ScreenPtr pScreen; BoxRec box; int granularity; void (*MoveAreaCallback)(struct _FBArea*, struct _FBArea*) void (*RemoveAreaCallback)(struct _FBArea*) DevUnion devPrivate; } FBArea, *FBAreaPtr; typedef void (*MoveAreaCallbackProcPtr)(FBAreaPtr from, FBAreaPtr to) typedef void (*RemoveAreaCallbackProcPtr)(FBAreaPtr) FBAreaPtr xf86AllocateOffscreenArea ( ScreenPtr pScreen, int width, int height, int granularity, MoveAreaCallbackProcPtr MoveAreaCallback, RemoveAreaCallbackProcPtr RemoveAreaCallback, pointer privData ) void xf86FreeOffscreenArea (FBAreaPtr area) Bool xf86ResizeOffscreenArea ( FBAreaPtr area int w, int h ) The function: Bool xf86FBManagerRunning(ScreenPtr pScreen) can be used by an extension to check if the driver has initialized the memory manager. The manager is not available if this returns FALSE and the func- tions above will all fail. xf86AllocateOffscreenArea() can be used to request a rectangle of dimensions width x height (in pixels) from unused offscreen memory. granularity speci- fies that the leftmost edge of the rectangle must lie on some multiple of granularity pixels. A granularity of zero means the same thing as a granu- larity of one - no alignment preference. A MoveAreaCallback can be provided to notify the requester when the offscreen area is moved. If no MoveArea- Callback is supplied then the area is considered to be immovable. The priv- Data field will be stored in the manager's internal structure for that allo- cated area and will be returned to the requester in the FBArea passed via the MoveAreaCallback. An optional RemoveAreaCallback is provided. If the driver provides this it indicates that the area should be allocated with a lower priority. Such an area may be removed when a higher priority request (one that doesn't have a RemoveAreaCallback) is made. When this function is called, the driver will have an opportunity to do whatever cleanup it needs to do to deal with the loss of the area, but it must finish its cleanup before the function exits since the offscreen memory manager will free the area immediately after. xf86AllocateOffscreenArea() returns NULL if it was unable to allocate the requested area. When no longer needed, areas should be freed with xf86Free- OffscreenArea(). xf86ResizeOffscreenArea() resizes an existing FBArea. xf86ResizeOff- screenArea() returns TRUE if the resize was successful. If xf86ResizeOff- screenArea() returns FALSE, the original FBArea is left unmodified. Resizing an area maintains the area's original granularity, devPrivate, and MoveArea- Callback. xf86ResizeOffscreenArea() has considerably less overhead than freeing the old area then reallocating the new size, so it should be used whenever possible. The function: Bool xf86QueryLargestOffscreenArea( ScreenPtr pScreen, int *width, int *height, int granularity, int preferences, int priority ) is provided to query the width and height of the largest single FBArea allo- catable given a particular priority. preferences can be one of the following to indicate whether width, height or area should be considered when determin- ing which is the largest single FBArea available. FAVOR_AREA_THEN_WIDTH FAVOR_AREA_THEN_HEIGHT FAVOR_WIDTH_THEN_AREA FAVOR_HEIGHT_THEN_AREA priority is one of the following: PRIORITY_LOW Return the largest block available without stealing any- one else's space. This corresponds to the priority of allocating a FBArea when a RemoveAreaCallback is pro- vided. PRIORITY_NORMAL Return the largest block available if it is acceptable to steal a lower priority area from someone. This corre- sponds to the priority of allocating a FBArea without providing a RemoveAreaCallback. PRIORITY_EXTREME Return the largest block available if all FBAreas that aren't locked down were expunged from memory first. This corresponds to any allocation made directly after a call to xf86PurgeUnlockedOffscreenAreas(). The function: Bool xf86PurgeUnlockedOffscreenAreas(ScreenPtr pScreen) is provided as an extreme method to free up offscreen memory. This will remove all removable FBArea allocations. Initialization of the XFree86 framebuffer manager is done via Bool xf86InitFBManager(ScreenPtr pScreen, BoxPtr FullBox) FullBox represents the area of the framebuffer that the manager is allowed to manage. This is typically a box with a width of pScrn->displayWidth and a height of as many lines as can be fit within the total video memory, however, the driver can reserve areas at the extremities by passing a smaller area to the manager. xf86InitFBManager() must be called before XAA is initialized since XAA uses the manager for it's pixmap cache. An alternative function is provided to allow the driver to initialize the framebuffer manager with a Region rather than a box. Bool xf86InitFBManagerRegion(ScreenPtr pScreen, RegionPtr FullRegion) xf86InitFBManagerRegion(), unlike xf86InitFBManager(), does not remove the area used for the visible screen so that area should not be included in the region passed to the function. xf86InitFBManagerRegion() is useful when non- contiguous areas are available to be managed, and is required when multiple framebuffers are stored in video memory (as in the case where an overlay of a different depth is stored as a second framebuffer in offscreen memory). 13. Colormap Handling A generic colormap handling layer is provided within the XFree86 common layer. This layer takes care of most of the details, and only requires a function from the driver that loads the hardware palette when required. To use the colormap layer, a driver calls the xf86HandleColormaps() function. Bool xf86HandleColormaps(ScreenPtr pScreen, int maxColors, int sigRGBbits, LoadPaletteFuncPtr loadPalette, SetOverscanFuncPtr setOverscan, unsigned int flags) This function must be called after the default colormap has been initialised. The pScrn->gamma field must also be initialised, preferably by calling xf86SetGamma(). maxColors is the number of entries in the palette. sigRGBbits is the size in bits of each color component in the DAC's palette. loadPalette is a driver-provided function for loading a colormap into the hardware, and is described below. setOverscan is an optional function that may be provided when the overscan color is an index from the standard LUT and when it needs to be adjusted to keep it as close to black as possible. The setOverscan function programs the overscan index. It shouldn't nor- mally be used for depths other than 8. setOverscan should be set to NULL when it isn't needed. flags may be set to the following (which may be ORed together): CMAP_PALETTED_TRUECOLOR the TrueColor visual is paletted and is just a special case of DirectColor. This flag is only valid for bpp > 8. CMAP_RELOAD_ON_MODE_SWITCH reload the colormap automatically after mode switches. This is useful for when the driver is resetting the hardware during mode switches and corrupting or erasing the hardware palette. CMAP_LOAD_EVEN_IF_OFFSCREEN reload the colormap even if the screen is switched out of the server's VC. The palette is not reloaded when the screen is switched back in, nor after mode switches. This is use- ful when the driver needs to keep track of palette changes. The colormap layer normally reloads the palette after VT enters so it is not necessary for the driver to save and restore the palette when switching VTs. The driver must, however, still save the initial palette during server start up and restore it during server exit. void LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices, LOCO *colors, VisualPtr pVisual) LoadPalette() is a driver-provided function for loading a colormap into hardware. colors is the array of RGB val- ues that represent the full colormap. indices is a list of index values into the colors array. These indices indicate the entries that need to be updated. numColors is the number of the indices to be updated. void SetOverscan(ScrnInfoPtr pScrn, int overscan) SetOverscan() is a driver-provided function for program- ming the overscan index. As described above, it is nor- mally only appropriate for LUT modes where all colormap entries are available for the display, but where one of them is also used for the overscan (typically 8bpp for VGA compatible LUTs). It isn't required in cases where the overscan area is never visible. 14. DPMS Extension Support code for the DPMS extension is included in the XFree86 common layer. This code provides an interface between the main extension code, and a means for drivers to initialise DPMS when they support it. One function is avail- able to drivers to do this initialisation, and it is always available, even when the DPMS extension is not supported by the core server (in which case it returns a failure result). Bool xf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags) This function registers a driver's DPMS level programming function set. It also checks pScrn->options for the "dpms" option, and when present marks DPMS as being enabled for that screen. The set function is called whenever the DPMS level changes, and is used to program the requested level. flags is currently not used, and should be 0. If the initialisation fails for any reason, including when there is no DPMS support in the core server, the function returns FALSE. Drivers that implement DPMS support must provide the following function, that gets called when the DPMS level is changed: void ChipDPMSSet(ScrnInfoPtr pScrn, int level, int flags) Program the DPMS level specified by level. Valid values of level are DPMSModeOn, DPMSModeStandby, DPMSModeSus- pend, DPMSModeOff. These values are defined in "exten- sions/dpms.h". 15. DGA Extension Drivers can support the XFree86 Direct Graphics Architecture (DGA) by filling out a structure of function pointers and a list of modes and passing them to DGAInit. Bool DGAInit(ScreenPtr pScreen, DGAFunctionPtr funcs, DGAModePtr modes, int num) /** The DGAModeRec **/ typedef struct { int num; DisplayModePtr mode; int flags; int imageWidth; int imageHeight; int pixmapWidth; int pixmapHeight; int bytesPerScanline; int byteOrder; int depth; int bitsPerPixel; unsigned long red_mask; unsigned long green_mask; unsigned long blue_mask; int viewportWidth; int viewportHeight; int xViewportStep; int yViewportStep; int maxViewportX; int maxViewportY; int viewportFlags; int offset; unsigned char *address; int reserved1; int reserved2; } DGAModeRec, *DGAModePtr; num Can be ignored. The DGA DDX will assign these numbers. mode A pointer to the DisplayModeRec for this mode. flags The following flags are defined and may be OR'd together: DGA_CONCURRENT_ACCESS Indicates that the driver supports concurrent graphics accelerator and linear framebuffer access. DGA_FILL_RECT DGA_BLIT_RECT DGA_BLIT_RECT_TRANS Indicates that the driver supports the FillRect, BlitRect or BlitTran- sRect functions in this mode. DGA_PIXMAP_AVAILABLE Indicates that Xlib may be used on the framebuffer. This flag will usu- ally be set unless the driver wishes to prohibit this for some reason. DGA_INTERLACED DGA_DOUBLESCAN Indicates that these are interlaced or double scan modes. imageWidth imageHeight These are the dimensions of the linear frame- buffer accessible by the client. pixmapWidth pixmapHeight These are the dimensions of the area of the framebuffer accessible by the graphics acceler- ator. bytesPerScanline Pitch of the framebuffer in bytes. byteOrder Usually the same as pScrn->imageByteOrder. depth The depth of the framebuffer in this mode. bitsPerPixel The number of bits per pixel in this mode. red_mask green_mask blue_mask The RGB masks for this mode, if applicable. viewportWidth viewportHeight Dimensions of the visible part of the frame- buffer. Usually mode->HDisplay and mode->VDis- play. xViewportStep yViewportStep The granularity of x and y viewport positions that the driver supports in this mode. maxViewportX maxViewportY The maximum viewport position supported by the driver in this mode. viewportFlags The following may be OR'd together: DGA_FLIP_IMMEDIATE The driver supports immediate view- port changes. DGA_FLIP_RETRACE The driver supports viewport changes at retrace. offset The offset into the linear framebuffer that corresponds to pixel (0,0) for this mode. address The virtual address of the framebuffer as mapped by the driver. This is needed when DGA_PIXMAP_AVAILABLE is set. /** The DGAFunctionRec **/ typedef struct { Bool (*OpenFramebuffer)( ScrnInfoPtr pScrn, char **name, unsigned int *mem, unsigned int *size, unsigned int *offset, unsigned int *extra ); void (*CloseFramebuffer)(ScrnInfoPtr pScrn); Bool (*SetMode)(ScrnInfoPtr pScrn, DGAModePtr pMode); void (*SetViewport)(ScrnInfoPtr pScrn, int x, int y, int flags); int (*GetViewport)(ScrnInfoPtr pScrn); void (*Sync)(ScrnInfoPtr); void (*FillRect)( ScrnInfoPtr pScrn, int x, int y, int w, int h, unsigned long color ); void (*BlitRect)( ScrnInfoPtr pScrn, int srcx, int srcy, int w, int h, int dstx, int dsty ); void (*BlitTransRect)( ScrnInfoPtr pScrn, int srcx, int srcy, int w, int h, int dstx, int dsty, unsigned long color ); } DGAFunctionRec, *DGAFunctionPtr; Bool OpenFramebuffer (pScrn, name, mem, size, offset, extra) OpenFramebuffer() should pass the client everything it needs to know to be able to open the framebuffer. These parameters are OS specific and their meanings are to be interpreted by an OS specific client library. name The name of the device to open or NULL if there is no special device to open. A NULL name tells the client that it should open whatever device one would usually open to access physi- cal memory. mem The physical address of the start of the frame- buffer, or the mmap(2) offset into the device designated by name. This is actually a pointer to two consecutive 32-bit values. Regardless of hardware architecture, the first of these is to be set to the low-order 32 bits of the address or offset, and the second is to be set to the high-order 32 bits. size The size of the framebuffer in bytes. offset Any offset into the device, if applicable. flags Any additional information that the client may need. Currently, only the DGA_NEED_ROOT flag is defined. void CloseFramebuffer (pScrn) CloseFramebuffer() merely informs the driver (if it even cares) that client no longer needs to access the frame- buffer directly. This function is optional. Bool SetMode (pScrn, pMode) SetMode() tells the driver to initialize the mode passed to it. If pMode is NULL, then the driver should restore the original pre-DGA mode. void SetViewport (pScrn, x, y, flags) SetViewport() tells the driver to make the upper left- hand corner of the visible screen correspond to coordi- nate (x,y) on the framebuffer. Flags currently defined are: DGA_FLIP_IMMEDIATE The viewport change should occur immediately. DGA_FLIP_RETRACE The viewport change should occur at the verti- cal retrace, but this function should return sooner if possible. The (x,y) locations will be passed as the client speci- fied them, however, the driver is expected to round these locations down to the next supported location as speci- fied by the xViewportStep and yViewportStep for the cur- rent mode. int GetViewport (pScrn) GetViewport() gets the current page flip status. Set bits in the returned int correspond to viewport change requests still pending. For instance, set bit zero if the last SetViewport request is still pending, bit one if the one before that is still pending, etc. void Sync (pScrn) This function should ensure that any graphics accelerator operations have finished. This function should not return until the graphics accelerator is idle. void FillRect (pScrn, x, y, w, h, color) This optional function should fill a rectangle w x h located at (x,y) in the given color. void BlitRect (pScrn, srcx, srcy, w, h, dstx, dsty) This optional function should copy an area w x h located at (srcx,srcy) to location (dstx,dsty). This function will need to handle copy directions as appropriate. void BlitTransRect (pScrn, srcx, srcy, w, h, dstx, dsty, color) This optional function is the same as BlitRect except that pixels in the source corresponding to the color key color should be skipped. 16. The XFree86 X Video Extension (Xv) Device Dependent Layer XFree86 offers the X Video Extension which allows clients to treat video as any another primitive and ``Put'' video into drawables. By default, the extension reports no video adaptors as being available since the DDX layer has not been initialized. The driver can initialize the DDX layer by filling out one or more XF86VideoAdaptorRecs as described later in this document and passing a list of XF86VideoAdaptorPtr pointers to the following function: Bool xf86XVScreenInit( ScreenPtr pScreen, XF86VideoAdaptorPtr *adaptPtrs, int num) After doing this, the extension will report video adaptors as being avail- able, providing the data in their respective XF86VideoAdaptorRecs was valid. xf86XVScreenInit() copies data from the structure passed to it so the driver may free it after the initialization. At the moment, the DDX only supports rendering into Window drawables. Pixmap rendering will be supported after a sufficient survey of suitable hardware is completed. The XF86VideoAdaptorRec: typedef struct { unsigned int type; int flags; char *name; int nEncodings; XF86VideoEncodingPtr pEncodings; int nFormats; XF86VideoFormatPtr pFormats; int nPorts; DevUnion *pPortPrivates; int nAttributes; XF86AttributePtr pAttributes; int nImages; XF86ImagePtr pImages; PutVideoFuncPtr PutVideo; PutStillFuncPtr PutStill; GetVideoFuncPtr GetVideo; GetStillFuncPtr GetStill; StopVideoFuncPtr StopVideo; SetPortAttributeFuncPtr SetPortAttribute; GetPortAttributeFuncPtr GetPortAttribute; QueryBestSizeFuncPtr QueryBestSize; PutImageFuncPtr PutImage; QueryImageAttributesFuncPtr QueryImageAttributes; } XF86VideoAdaptorRec, *XF86VideoAdaptorPtr; Each adaptor will have its own XF86VideoAdaptorRec. The fields are as follows: type This can be any of the following flags OR'd together. XvInputMask XvOutputMask These refer to the target drawable and are sim- ilar to a Window's class. XvInputMask indicates that the adaptor can put video into a drawable. XvOutputMask indicates that the adaptor can get video from a drawable. XvVideoMask XvStillMask XvImageMask These indicate that the adaptor supports video, still or image primitives respectively. XvWindowMask XvPixmapMask These indicate the types of drawables the adap- tor is capable of rendering into. At the moment, Pixmap rendering is not supported and the XvPixmapMask flag is ignored. flags Currently, the following flags are defined: VIDEO_NO_CLIPPING This indicates that the video adaptor does not support clipping. The driver will never receive ``Put'' requests where less than the entire area determined by drw_x, drw_y, drw_w and drw_h is visible. This flag does not apply to ``Get'' requests. Hardware that is inca- pable of clipping ``Gets'' may punt or get the extents of the clipping region passed to it. VIDEO_INVERT_CLIPLIST This indicates that the video driver requires the clip list to contain the regions which are obscured rather than the regions which are are visible. VIDEO_OVERLAID_STILLS Implementing PutStill for hardware that does video as an overlay can be awkward since it's unclear how long to leave the video up for. When this flag is set, StopVideo will be called whenever the destination gets clipped or moved so that the still can be left up until then. VIDEO_OVERLAID_IMAGES Same as VIDEO_OVERLAID_STILLS but for images. VIDEO_CLIP_TO_VIEWPORT Indicates that the clip region passed to the driver functions should be clipped to the visi- ble portion of the screen in the case where the viewport is smaller than the virtual desktop. name The name of the adaptor. nEncodings pEncodings The number of encodings the adaptor is capable of and pointer to the XF86VideoEncodingRec array. The XF86VideoEncodingRec is described later on. For drivers that only support XvImages there should be an encoding named "XV_IMAGE" and the width and height should specify the maximum size source image supported. nFormats pFormats The number of formats the adaptor is capable of and pointer to the XF86VideoFormatRec array. The XF86Video- FormatRec is described later on. nPorts pPortPrivates The number of ports is the number of separate data streams which the adaptor can handle simultaneously. If you have more than one port, the adaptor is expected to be able to render into more than one window at a time. pPortPrivates is an array of pointers or ints - one for each port. A port's private data will be passed to the driver any time the port is requested to do something like put the video or stop the video. In the case where there may be many ports, this enables the driver to know which port the request is intended for. Most commonly, this will contain a pointer to the data structure con- taining information about the port. In Xv, all ports on a particular adaptor are expected to be identical in their functionality. nAttributes pAttributes The number of attributes recognized by the adaptor and a pointer to the array of XF86AttributeRecs. The XF86AttributeRec is described later on. nImages pImages The number of XF86ImageRecs supported by the adaptor and a pointer to the array of XF86ImageRecs. The XF86ImageRec is described later on. PutVideo PutStill GetVideo GetStill StopVideo SetPortAttribute Get- PortAttribute QueryBestSize PutImage QueryImageAttributes These functions define the DDX->driver interface. In each case, the pointer data is passed to the driver. This is the port private for that port as described above. All fields are required except under the follow- ing conditions: 1. PutVideo, PutStill and the image routines PutImage and QueryImageAttributes are not required when the adaptor type does not contain XvInputMask. 2. GetVideo and GetStill are not required when the adaptor type does not contain XvOutputMask. 3. GetVideo and PutVideo are not required when the adaptor type does not contain XvVideoMask. 4. GetStill and PutStill are not required when the adaptor type does not contain XvStillMask. 5. PutImage and QueryImageAttributes are not required when the adaptor type does not contain XvImageMask. With the exception of QueryImageAttributes, these func- tions should return Success if the operation was com- pleted successfully. They can return XvBadAlloc other- wise. QueryImageAttributes returns the size of the XvIm- age queried. If the VIDEO_NO_CLIPPING flag is set, the clipBoxes may be ignored by the driver. ClipBoxes is an X-Y banded region identical to those used throughout the server. The clipBoxes represent the visible portions of the area determined by drw_x, drw_y, drw_w and drw_h in the Get/Put function. The boxes are in screen coordinates, are guaranteed not to overlap and an empty region will never be passed. If the driver has specified VIDEO_INVERT_CLIPLIST, clipBoxes will indicate the areas of the primitive which are obscured rather than the areas visible. typedef int (* PutVideoFuncPtr)( ScrnInfoPtr pScrn, short vid_x, short vid_y, short drw_x, short drw_y, short vid_w, short vid_h, short drw_w, short drw_h, RegionPtr clipBoxes, pointer data ) This indicates that the driver should take a subsection vid_w by vid_h at location (vid_x,vid_y) from the video stream and direct it into the rectangle drw_w by drw_h at location (drw_x,drw_y) on the screen, scaling as neces- sary. Due to the large variations in capabilities of the various hardware expected to be used with this extension, it is not expected that all hardware will be able to do this exactly as described. In that case the driver should just do ``the best it can,'' scaling as closely to the target rectangle as it can without rendering outside of it. In the worst case, the driver can opt to just not turn on the video. typedef int (* PutStillFuncPtr)( ScrnInfoPtr pScrn, short vid_x, short vid_y, short drw_x, short drw_y, short vid_w, short vid_h, short drw_w, short drw_h, RegionPtr clipBoxes, pointer data ) This is same as PutVideo except that the driver should place only one frame from the stream on the screen. typedef int (* GetVideoFuncPtr)( ScrnInfoPtr pScrn, short vid_x, short vid_y, short drw_x, short drw_y, short vid_w, short vid_h, short drw_w, short drw_h, RegionPtr clipBoxes, pointer data ) This is same as PutVideo except that the driver gets video from the screen and outputs it. The driver should do the best it can to get the requested dimensions cor- rect without reading from an area larger than requested. typedef int (* GetStillFuncPtr)( ScrnInfoPtr pScrn, short vid_x, short vid_y, short drw_x, short drw_y, short vid_w, short vid_h, short drw_w, short drw_h, RegionPtr clipBoxes, pointer data ) This is the same as GetVideo except that the driver should place only one frame from the screen into the out- put stream. typedef void (* StopVideoFuncPtr)(ScrnInfoPtr pScrn, pointer data, Bool cleanup) This indicates the driver should stop displaying the video. This is used to stop both input and output video. The cleanup field indicates that the video is being stopped because the client requested it to stop or because the server is exiting the current VT. In that case the driver should deallocate any offscreen memory areas (if there are any) being used to put the video to the screen. If cleanup is not set, the video is being stopped temporarily due to clipping or moving of the win- dow, etc... and video will likely be restarted soon so the driver should not deallocate any offscreen areas associated with that port. typedef int (* SetPortAttributeFuncPtr)(ScrnInfoPtr pScrn, Atom attribute,INT32 value, pointer data) typedef int (* GetPortAttributeFuncPtr)(ScrnInfoPtr pScrn, Atom attribute,INT32 *value, pointe