via_driver.h revision 7024fa79
1/*
2 * Copyright 2004-2005 The Unichrome Project  [unichrome.sf.net]
3 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
4 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef _VIA_DRIVER_H_
27#define _VIA_DRIVER_H_ 1
28
29//#define VIA_DEBUG_COMPOSITE 1
30#define HAVE_DEBUG 1
31
32#ifdef HAVE_DEBUG
33#define DEBUG(x) x
34#else
35#define DEBUG(x)
36#endif
37
38#include "vgaHW.h"
39#include "xf86.h"
40
41#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
42#include "xf86Resources.h"
43#endif
44
45#include "xf86Pci.h"
46#include "xf86_OSproc.h"
47#include "compiler.h"
48#include "xf86Cursor.h"
49#include "mipointer.h"
50#include "micmap.h"
51#include "fourcc.h"
52#include "fb.h"
53
54#include "xf86Crtc.h"
55#include "xf86RandR12.h"
56#include "xf86cmap.h"
57#include "vbe.h"
58
59#ifdef HAVE_DRI
60#define _XF86DRI_SERVER_
61#include "xf86dri.h"
62#include "sarea.h"
63#include "dri.h"
64#include "drm_fourcc.h"
65#include "GL/glxint.h"
66#include "via_dri.h"
67#include "via_drmclient.h"
68#include "via_drm.h"
69#endif
70#include "exa.h"
71#include "via_memmgr.h"
72
73#include "via_regs.h"
74#include "via_kms.h"
75#include "via_ums.h"
76#include "via_dmabuffer.h"
77#include "via_3d.h"
78#include "via_xv.h"
79#include "via_xv_overlay.h"
80#include "via_eng_regs.h"
81
82#ifdef HAVE_PCIACCESS
83#include <pciaccess.h>
84#else
85#include "xf86PciInfo.h"
86#endif
87#include <errno.h>
88
89#include "compat-api.h"
90#define VIA_AGP_UPL_SIZE    (1024*128)
91#define VIA_DMA_DL_SIZE     (1024*128)
92#define VIA_SCRATCH_SIZE    (4*1024*1024)
93
94/*
95 * Pixmap sizes below which we don't try to do hw accel.
96 */
97
98#define VIA_MIN_COMPOSITE   400
99#define VIA_MIN_UPLOAD 4000
100#define VIA_MIN_TEX_UPLOAD 200
101#define VIA_MIN_DOWNLOAD 200
102
103#define AGP_PAGE_SIZE 4096
104#define AGP_PAGES 8192
105#define AGP_SIZE (AGP_PAGE_SIZE * AGP_PAGES)
106
107#define DRIVER_NAME     "openchrome"
108#define VIA_VERSION     ((VIA_MAJOR_VERSION << 24) | (VIA_MINOR_VERSION << 16) | VIA_PATCHLEVEL)
109
110#define VIA_VQ_SIZE     (256 * 1024)
111
112#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
113#define VIA_RES_SHARED RES_SHARED_VGA
114#define VIA_RES_UNDEF RES_UNDEFINED
115#else
116#define VIA_RES_SHARED NULL
117#define VIA_RES_UNDEF NULL
118#endif
119
120/* Video Engines */
121#define VIDEO_ENGINE_UNK    0   /* Unknown video engine */
122#define VIDEO_ENGINE_CLE    1   /* CLE First generation video engine */
123#define VIDEO_ENGINE_CME    2   /* CME Second generation video engine */
124
125static int gVIAEntityIndex = -1;
126
127typedef struct {
128    CARD8   SR08, SR0A, SR0F;
129
130    /*   extended Sequencer registers */
131    CARD8   SR10, SR11, SR12, SR13,SR14,SR15,SR16;
132    CARD8   SR17, SR18, SR19, SR1A,SR1B,SR1C,SR1D,SR1E;
133    CARD8   SR1F, SR20, SR21, SR22,SR23,SR24,SR25,SR26;
134    CARD8   SR27, SR28, SR29, SR2A,SR2B,SR2C,SR2D,SR2E;
135    CARD8   SR2F, SR30, SR31, SR32,SR33,SR34,SR40,SR41;
136    CARD8   SR42, SR43, SR44, SR45,SR46,SR47,SR48,SR49;
137    CARD8   SR4A, SR4B, SR4C, SR4D;
138
139    /*   extended CRTC registers */
140    CARD8   CR0C, CR0D;
141    CARD8   CR13, CR30, CR31, CR32, CR33, CR34, CR35, CR36;
142    CARD8   CR37, CR38, CR39, CR3A, CR40, CR41, CR42, CR43;
143    CARD8   CR44, CR45, CR46, CR47, CR48, CR49, CR4A;
144    CARD8   CR97, CR99, CR9B, CR9F, CRA0, CRA1, CRA2;
145    CARD8   CRTCRegs[68];
146/*    CARD8   LCDRegs[0x40];*/
147
148    /* TMDS/LVDS Control */
149    CARD8   CRD2;
150} VIARegRec, *VIARegPtr;
151
152/*
153 * variables that need to be shared among different screens.
154 */
155typedef struct {
156    Bool b3DRegsInitialized;
157} ViaSharedRec, *ViaSharedPtr;
158
159enum dri_type {
160	DRI_NONE,
161	DRI_1,
162	DRI_2
163};
164
165#ifdef HAVE_DRI
166
167#define VIA_XVMC_MAX_BUFFERS 2
168#define VIA_XVMC_MAX_CONTEXTS 4
169#define VIA_XVMC_MAX_SURFACES 20
170
171typedef struct {
172    struct buffer_object *memory_ref;
173    unsigned long offsets[VIA_XVMC_MAX_BUFFERS];
174} ViaXvMCSurfacePriv;
175
176typedef struct {
177    drm_context_t drmCtx;
178} ViaXvMCContextPriv;
179
180typedef struct {
181    XID contexts[VIA_XVMC_MAX_CONTEXTS];
182    XID surfaces[VIA_XVMC_MAX_SURFACES];
183    ViaXvMCSurfacePriv *sPrivs[VIA_XVMC_MAX_SURFACES];
184    ViaXvMCContextPriv *cPrivs[VIA_XVMC_MAX_CONTEXTS];
185    int nContexts,nSurfaces;
186    drm_handle_t mmioBase,fbBase,sAreaBase;
187    unsigned sAreaSize;
188    drmAddress sAreaAddr;
189    unsigned activePorts;
190}ViaXvMC, *ViaXvMCPtr;
191
192#endif
193
194typedef struct _twodContext {
195    CARD32 mode;
196    CARD32 cmd;
197    CARD32 fgColor;
198    CARD32 bgColor;
199    CARD32 pattern0;
200    CARD32 pattern1;
201    CARD32 patternAddr;
202    CARD32 keyControl;
203    unsigned srcOffset;
204    unsigned srcPitch;
205    unsigned Bpp;
206    unsigned bytesPPShift;
207    Bool clipping;
208    Bool dashed;
209    int clipX1;
210    int clipX2;
211    int clipY1;
212    int clipY2;
213} ViaTwodContext;
214
215typedef struct{
216    /* textMode */
217    CARD8 *state, *pstate; /* SVGA state */
218    int statePage, stateSize, stateMode;
219
220    /* vbe version */
221    int major, minor;
222} ViaVbeModeInfo;
223
224typedef struct _VIA {
225    VIARegRec           SavedReg;
226    int                 Bpp, Bpl;
227
228    Bool                KMS;
229    Bool                FirstInit;
230    unsigned long       videoRambytes;
231    int                 FBFreeStart;
232    int                 FBFreeEnd;
233    int                 driSize;
234    int                 maxDriSize;
235    struct buffer_object *vq_bo;
236    int                 VQStart;
237    int                 VQEnd;
238
239    /* These are physical addresses. */
240    unsigned long       FrameBufferBase;
241    unsigned long       MmioBase;
242
243    /* These are linear addresses. */
244    unsigned char*      MapBase;
245    unsigned char*      MpegMapBase;
246    unsigned char*      BltBase;
247    unsigned char*      MapBaseDense;
248    unsigned char*      FBBase;
249    CARD8               MemClk;
250
251    /* Here are all the Options */
252    Bool                VQEnable;
253    Bool                NoAccel;
254    Bool                shadowFB;
255    Rotation            rotate;
256    Bool                vbeSR;
257    int                 agpMem;
258
259	CreateScreenResourcesProcPtr CreateScreenResources;
260    CloseScreenProcPtr  CloseScreen;
261#ifdef HAVE_PCIACCESS
262    struct pci_device  *PciInfo;
263#else
264    pciVideoPtr         PciInfo;
265    PCITAG PciTag;
266#endif
267    int                 Chipset;
268    int                 ChipId;
269    int                 ChipRev;
270    int                 EntityIndex;
271
272    /* vbe */
273    vbeInfoPtr          pVbe;
274    ViaVbeModeInfo      vbeMode;
275    Bool                useVBEModes;
276    Bool                useLegacyVBE;
277
278    /* Support for shadowFB and rotation */
279    unsigned char*      ShadowPtr;
280
281    /* Support for EXA acceleration */
282    ViaTwodContext      td;
283    Via3DState          v3d;
284    Via3DState          *lastToUpload;
285    ViaCommandBuffer    cb;
286    int                 accelMarker;
287    struct buffer_object *exa_sync_bo;
288    struct buffer_object *exaMem;
289    CARD32              markerOffset;
290    void               *markerBuf;
291    CARD32              curMarker;
292    CARD32              lastMarkerRead;
293    Bool                agpDMA;
294    Bool                nPOT[VIA_NUM_TEXUNITS];
295    const unsigned     *HqvCmeRegs;
296    ExaDriverPtr        exaDriverPtr;
297    ExaOffscreenArea   *exa_scratch;
298    unsigned int        exa_scratch_next;
299    Bool                useEXA;
300    void               *maskP;
301    CARD32              maskFormat;
302    Bool                componentAlpha;
303    void               *srcP;
304    CARD32              srcFormat;
305    unsigned            scratchOffset;
306    int                 exaScratchSize;
307    char *              scratchAddr;
308    Bool                noComposite;
309    struct buffer_object *scratchBuffer;
310#ifdef HAVE_DRI
311    struct buffer_object *texAGPBuffer;
312    char *              dBounce;
313#endif
314
315    /* Rotation */
316    Bool    RandRRotation;
317
318    /* BIOS Info Ptr */
319    VIABIOSInfoPtr      pBIOSInfo;
320    struct ViaCardIdStruct* Id;
321
322    /* I2C & DDC */
323    I2CBusPtr           pI2CBus1;
324    I2CBusPtr           pI2CBus2;
325    I2CBusPtr           pI2CBus3;
326
327    /* MHS */
328    Bool                IsSecondary;
329    Bool                HasSecondary;
330    Bool                SAMM;
331
332    drmmode_rec         drmmode;
333    enum dri_type       directRenderingType;
334#ifdef HAVE_DRI
335    Bool                XvMCEnabled;
336    DRIInfoPtr          pDRIInfo;
337    int                 numVisualConfigs;
338    __GLXvisualConfig*	pVisualConfigs;
339    VIAConfigPrivPtr	pVisualConfigsPriv;
340    drm_handle_t        agpHandle;
341    drm_handle_t        registerHandle;
342    drm_handle_t        frameBufferHandle;
343    unsigned long       agpAddr;
344    drmAddress          agpMappedAddr;
345    unsigned int        agpSize;
346    Bool                IsPCI;
347    ViaXvMC             xvmc;
348    int                 drmVerMajor;
349    int                 drmVerMinor;
350    int                 drmVerPL;
351    struct buffer_object *driOffScreenMem;
352    void *              driOffScreenSave;
353#endif
354    Bool                DRIIrqEnable;
355    Bool                agpEnable;
356    Bool                dma2d;
357    Bool                dmaXV;
358
359    CARD8               ActiveDevice;	/* Option */
360
361    /* Video */
362    int                 VideoEngine;
363    swovRec             swov;
364    CARD32              VideoStatus;
365    VIAHWDiff           HWDiff;
366    unsigned long       dwV1, dwV3;
367    unsigned long       dwFrameNum;
368
369    CARD32*             VidRegBuffer; /* Temporary buffer for video overlay registers. */
370    unsigned long       VidRegCursor; /* Write cursor for VidRegBuffer. */
371
372    unsigned long       old_dwUseExtendedFIFO;
373
374    ViaSharedPtr        sharedData;
375    Bool                useDmaBlit;
376
377    void                *displayMap;
378    CARD32              displayOffset;
379
380    CARD8               I2CDevices;	/* Option */
381
382#ifdef HAVE_DEBUG
383    Bool                disableXvBWCheck;
384    Bool                DumpVGAROM;
385    Bool                PrintVGARegs;
386    Bool                PrintTVRegs;
387    Bool                I2CScan;
388#endif /* HAVE_DEBUG */
389
390    Bool                UseLegacyModeSwitch;
391    video_via_regs*     VideoRegs;
392} VIARec, *VIAPtr;
393
394#define VIAPTR(p) ((VIAPtr)((p)->driverPrivate))
395
396typedef struct
397{
398    Bool IsDRIEnabled;
399
400    Bool HasSecondary;
401    Bool BypassSecondary;
402    /*These two registers are used to make sure the CRTC2 is
403      restored before CRTC_EXT, otherwise it could lead to blank screen.*/
404    Bool IsSecondaryRestored;
405    Bool RestorePrimary;
406
407    ScrnInfoPtr pSecondaryScrn;
408    ScrnInfoPtr pPrimaryScrn;
409} VIAEntRec, *VIAEntPtr;
410
411/* In via_exa.c. */
412Bool viaInitExa(ScreenPtr pScreen);
413Bool viaAccelSetMode(int bpp, ViaTwodContext * tdc);
414void viaAccelSync(ScrnInfoPtr);
415void viaExitAccel(ScreenPtr);
416void viaFinishInitAccel(ScreenPtr);
417Bool viaOrder(CARD32 val, CARD32 * shift);
418CARD32 viaBitExpandHelper(CARD32 pixel, CARD32 bits);
419Bool viaCheckUpload(ScrnInfoPtr pScrn, Via3DState * v3d);
420void viaPixelARGB8888(unsigned format, void *pixelP, CARD32 * argb8888);
421Bool viaExpandablePixel(int format);
422void viaAccelFillPixmap(ScrnInfoPtr, unsigned long, unsigned long,
423			int, int, int, int, int, unsigned long);
424void viaAccelTextureBlit(ScrnInfoPtr, unsigned long, unsigned, unsigned,
425			 unsigned, unsigned, unsigned, unsigned,
426			 unsigned long, unsigned, unsigned,
427			 unsigned, unsigned, int);
428#ifdef VIA_DEBUG_COMPOSITE
429void viaExaCompositePictDesc(PicturePtr pict, char *string, int n);
430void viaExaPrintCompositeInfo(char *info, CARD8 op, PicturePtr pSrc, PicturePtr pMask,
431                            PicturePtr pDst);
432#endif
433
434/* In via_exa_h2.c */
435Bool viaExaPrepareSolid_H2(PixmapPtr pPixmap, int alu, Pixel planeMask,
436                        Pixel fg);
437void viaExaSolid_H2(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
438void viaExaDoneSolidCopy_H2(PixmapPtr pPixmap);
439Bool viaExaPrepareCopy_H2(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
440                            int xdir, int ydir, int alu, Pixel planeMask);
441void viaExaCopy_H2(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
442                    int dstY, int width, int height);
443Bool viaExaCheckComposite_H2(int op, PicturePtr pSrcPicture,
444                            PicturePtr pMaskPicture, PicturePtr pDstPicture);
445Bool viaExaPrepareComposite_H2(int op, PicturePtr pSrcPicture,
446                            PicturePtr pMaskPicture, PicturePtr pDstPicture,
447                            PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
448void viaExaComposite_H2(PixmapPtr pDst, int srcX, int srcY,
449                        int maskX, int maskY, int dstX, int dstY,
450                        int width, int height);
451int viaAccelMarkSync_H2(ScreenPtr);
452
453/* In via_exa_h6.c */
454Bool viaExaPrepareSolid_H6(PixmapPtr pPixmap, int alu, Pixel planeMask,
455                        Pixel fg);
456void viaExaSolid_H6(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
457void viaExaDoneSolidCopy_H6(PixmapPtr pPixmap);
458Bool viaExaPrepareCopy_H6(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
459                            int xdir, int ydir, int alu, Pixel planeMask);
460void viaExaCopy_H6(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
461                    int dstY, int width, int height);
462Bool viaExaCheckComposite_H6(int op, PicturePtr pSrcPicture,
463                            PicturePtr pMaskPicture, PicturePtr pDstPicture);
464Bool viaExaPrepareComposite_H6(int op, PicturePtr pSrcPicture,
465                            PicturePtr pMaskPicture, PicturePtr pDstPicture,
466                            PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
467void viaExaComposite_H6(PixmapPtr pDst, int srcX, int srcY,
468                        int maskX, int maskY, int dstX, int dstY,
469                        int width, int height);
470int viaAccelMarkSync_H6(ScreenPtr);
471
472/* In via_xv.c */
473void viaInitVideo(ScreenPtr pScreen);
474void viaExitVideo(ScrnInfoPtr pScrn);
475void viaSaveVideo(ScrnInfoPtr pScrn);
476void viaRestoreVideo(ScrnInfoPtr pScrn);
477void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
478		      Bool reset);
479
480
481/* In via_xv.c */
482void viaInitVideo(ScreenPtr pScreen);
483void viaExitVideo(ScrnInfoPtr pScrn);
484void viaSaveVideo(ScrnInfoPtr pScrn);
485void viaRestoreVideo(ScrnInfoPtr pScrn);
486void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
487		      Bool reset);
488void VIAVidAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
489
490/* In via_memcpy.c */
491typedef void (*vidCopyFunc)(unsigned char *, const unsigned char *,
492                            int, int, int, int);
493extern vidCopyFunc viaVidCopyInit( char *copyType, ScreenPtr pScreen );
494
495/* In via_xwmc.c */
496
497#ifdef HAVE_DRI
498/* Basic init and exit functions */
499void ViaInitXVMC(ScreenPtr pScreen);
500void ViaCleanupXVMC(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr *XvAdaptors, int XvAdaptorCount);
501int viaXvMCInitXv(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr XvAdapt);
502
503/* Returns the size of the fake Xv Image used as XvMC command buffer to the X server*/
504unsigned long viaXvMCPutImageSize(ScrnInfoPtr pScrn);
505#endif
506
507/* via_i2c.c */
508void ViaI2CInit(ScrnInfoPtr pScrn);
509
510#ifdef HAVE_DRI
511Bool VIADRI1ScreenInit(ScreenPtr pScreen);
512void VIADRICloseScreen(ScreenPtr pScreen);
513Bool VIADRIFinishScreenInit(ScreenPtr pScreen);
514void VIADRIRingBufferCleanup(ScrnInfoPtr pScrn);
515Bool VIADRIRingBufferInit(ScrnInfoPtr pScrn);
516void viaDRIOffscreenRestore(ScrnInfoPtr pScrn);
517void viaDRIOffscreenSave(ScrnInfoPtr pScrn);
518Bool VIADRIBufferInit(ScrnInfoPtr pScrn);
519
520#endif /* HAVE_DRI */
521
522int viaOffScreenLinear(struct buffer_object *obj, ScrnInfoPtr pScrn, unsigned long size);
523void viaShowCursor(ScrnInfoPtr pScrn);
524void viaHideCursor(ScrnInfoPtr pScrn);
525Bool viaHWCursorInit(ScreenPtr pScreen);
526void ViaDisplaySetStreamOnCRT(ScrnInfoPtr pScrn, Bool primary);
527void ViaDisplaySetStreamOnDFP(ScrnInfoPtr pScrn, Bool primary);
528void ViaDisplaySetStreamOnDVO(ScrnInfoPtr pScrn, int port, Bool primary);
529void ViaDisplayEnableSimultaneous(ScrnInfoPtr pScrn);
530void ViaDisplayEnableCRT(ScrnInfoPtr pScrn);
531void ViaDisplayEnableDVO(ScrnInfoPtr pScrn, int port);
532
533#endif /* _VIA_DRIVER_H_ */
534