via_driver.h revision 03bd066f
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 "xf86fbman.h"
56#include "xf86RandR12.h"
57#include "xf86cmap.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 "via_vt1632.h"
90
91#include "compat-api.h"
92#define VIA_AGP_UPL_SIZE    (1024*128)
93#define VIA_DMA_DL_SIZE     (1024*128)
94#define VIA_SCRATCH_SIZE    (4*1024*1024)
95
96/*
97 * Pixmap sizes below which we don't try to do hw accel.
98 */
99
100#define VIA_MIN_COMPOSITE   400
101#define VIA_MIN_UPLOAD 4000
102#define VIA_MIN_TEX_UPLOAD 200
103#define VIA_MIN_DOWNLOAD 200
104
105#define AGP_PAGE_SIZE 4096
106#define AGP_PAGES 8192
107#define AGP_SIZE (AGP_PAGE_SIZE * AGP_PAGES)
108
109#define DRIVER_NAME     "openchrome"
110#define VIA_VERSION     ((VIA_MAJOR_VERSION << 24) | (VIA_MINOR_VERSION << 16) | VIA_PATCHLEVEL)
111
112#define VIA_VQ_SIZE     (256 * 1024)
113
114#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
115#define VIA_RES_SHARED RES_SHARED_VGA
116#define VIA_RES_UNDEF RES_UNDEFINED
117#else
118#define VIA_RES_SHARED NULL
119#define VIA_RES_UNDEF NULL
120#endif
121
122/* Video Engines */
123#define VIDEO_ENGINE_UNK    0   /* Unknown video engine */
124#define VIDEO_ENGINE_CLE    1   /* CLE First generation video engine */
125#define VIDEO_ENGINE_CME    2   /* CME Second generation video engine */
126
127static int gVIAEntityIndex = -1;
128
129typedef struct {
130    CARD8   SR08, SR0A, SR0F;
131
132    /* Extended Sequencer Registers */
133    CARD8   SR10, SR11, SR12, SR13, SR14, SR15, SR16, SR17;
134    CARD8   SR18, SR19, SR1A, SR1B, SR1C, SR1D, SR1E, SR1F;
135    CARD8   SR20, SR21, SR22, SR23, SR24, SR25, SR26, SR27;
136    CARD8   SR28, SR29, SR2A, SR2B, SR2C, SR2D, SR2E, SR2F;
137    CARD8   SR30, SR31, SR32, SR33, SR34;
138    CARD8   SR40, SR41, SR42, SR43, SR44, SR45, SR46, SR47;
139    CARD8   SR48, SR49, SR4A, SR4B, SR4C, SR4D, SR4E, SR4F;
140
141    /* CRTC Registers */
142    CARD8   CR0C, CR0D;
143    CARD8   CR13;
144
145    /* IGA1 Registers */
146    CARD8   CR30, CR31, CR32, CR33, CR34, CR35, CR36, CR37;
147    CARD8   CR38, CR39, CR3A, CR3B, CR3C, CR3D, CR3E, CR3F;
148    CARD8   CR40, CR41, CR42, CR43, CR44, CR45, CR46, CR47;
149    CARD8   CR48;
150
151    /* IGA2 Registers */
152    CARD8   EXCR[0xFD - 0x50 + 1];
153
154} VIARegRec, *VIARegPtr;
155
156/*
157 * variables that need to be shared among different screens.
158 */
159typedef struct {
160    Bool b3DRegsInitialized;
161} ViaSharedRec, *ViaSharedPtr;
162
163enum dri_type {
164	DRI_NONE,
165	DRI_1,
166	DRI_2
167};
168
169#ifdef HAVE_DRI
170
171#define VIA_XVMC_MAX_BUFFERS 2
172#define VIA_XVMC_MAX_CONTEXTS 4
173#define VIA_XVMC_MAX_SURFACES 20
174
175typedef struct {
176    struct buffer_object *memory_ref;
177    unsigned long offsets[VIA_XVMC_MAX_BUFFERS];
178} ViaXvMCSurfacePriv;
179
180typedef struct {
181    drm_context_t drmCtx;
182} ViaXvMCContextPriv;
183
184typedef struct {
185    XID contexts[VIA_XVMC_MAX_CONTEXTS];
186    XID surfaces[VIA_XVMC_MAX_SURFACES];
187    ViaXvMCSurfacePriv *sPrivs[VIA_XVMC_MAX_SURFACES];
188    ViaXvMCContextPriv *cPrivs[VIA_XVMC_MAX_CONTEXTS];
189    int nContexts,nSurfaces;
190    drm_handle_t mmioBase,fbBase,sAreaBase;
191    unsigned sAreaSize;
192    drmAddress sAreaAddr;
193    unsigned activePorts;
194}ViaXvMC, *ViaXvMCPtr;
195
196#endif
197
198typedef struct _twodContext {
199    CARD32 mode;
200    CARD32 cmd;
201    CARD32 fgColor;
202    CARD32 bgColor;
203    CARD32 pattern0;
204    CARD32 pattern1;
205    CARD32 patternAddr;
206    CARD32 keyControl;
207    unsigned srcOffset;
208    unsigned srcPitch;
209    unsigned Bpp;
210    unsigned bytesPPShift;
211    Bool clipping;
212    Bool dashed;
213    int clipX1;
214    int clipX2;
215    int clipY1;
216    int clipY2;
217} ViaTwodContext;
218
219typedef struct _VIA {
220    VIARegRec           SavedReg;
221    int                 Bpp, Bpl;
222
223    Bool                KMS;
224    Bool                FirstInit;
225    unsigned long       videoRambytes;
226    int                 FBFreeStart;
227    int                 FBFreeEnd;
228    int                 driSize;
229    int                 maxDriSize;
230    struct buffer_object *vq_bo;
231    int                 VQStart;
232    int                 VQEnd;
233
234    /* These are physical addresses. */
235    unsigned long       FrameBufferBase;
236    unsigned long       MmioBase;
237
238    /* These are linear addresses. */
239    unsigned char*      MapBase;
240    unsigned char*      MpegMapBase;
241    unsigned char*      BltBase;
242    unsigned char*      MapBaseDense;
243    unsigned char*      FBBase;
244    CARD8               MemClk;
245
246    /* Here are all the Options */
247    Bool                VQEnable;
248    Bool                NoAccel;
249    Bool                shadowFB;
250    Rotation            rotate;
251    int                 agpMem;
252
253	CreateScreenResourcesProcPtr CreateScreenResources;
254    CloseScreenProcPtr  CloseScreen;
255#ifdef HAVE_PCIACCESS
256    struct pci_device  *PciInfo;
257#else
258    pciVideoPtr         PciInfo;
259    PCITAG PciTag;
260#endif
261    int                 Chipset;
262    int                 ChipId;
263    int                 ChipRev;
264    int                 EntityIndex;
265
266    /* Support for shadowFB and rotation */
267    unsigned char*      ShadowPtr;
268
269    /* Support for EXA acceleration */
270    ViaTwodContext      td;
271    Via3DState          v3d;
272    Via3DState          *lastToUpload;
273    ViaCommandBuffer    cb;
274    int                 accelMarker;
275    struct buffer_object *exa_sync_bo;
276    struct buffer_object *exaMem;
277    CARD32              markerOffset;
278    void               *markerBuf;
279    CARD32              curMarker;
280    CARD32              lastMarkerRead;
281    Bool                agpDMA;
282    Bool                nPOT[VIA_NUM_TEXUNITS];
283    const unsigned     *HqvCmeRegs;
284    ExaDriverPtr        exaDriverPtr;
285    ExaOffscreenArea   *exa_scratch;
286    unsigned int        exa_scratch_next;
287    Bool                useEXA;
288    void               *maskP;
289    CARD32              maskFormat;
290    Bool                componentAlpha;
291    void               *srcP;
292    CARD32              srcFormat;
293    unsigned            scratchOffset;
294    int                 exaScratchSize;
295    char *              scratchAddr;
296    Bool                noComposite;
297    struct buffer_object *scratchBuffer;
298#ifdef HAVE_DRI
299    struct buffer_object *texAGPBuffer;
300    char *              dBounce;
301#endif
302
303    /* Rotation */
304    Bool    RandRRotation;
305
306    /* BIOS Info Ptr */
307    VIABIOSInfoPtr      pBIOSInfo;
308
309    /* OLPC XO-1.5 */
310    Bool                IsOLPCXO15;
311
312    /* I2C & DDC */
313    I2CBusPtr           pI2CBus1;
314    I2CBusPtr           pI2CBus2;
315    I2CBusPtr           pI2CBus3;
316
317    /* MHS */
318    Bool                IsSecondary;
319    Bool                HasSecondary;
320    Bool                SAMM;
321
322    drmmode_rec         drmmode;
323    enum dri_type       directRenderingType;
324#ifdef HAVE_DRI
325    Bool                XvMCEnabled;
326    DRIInfoPtr          pDRIInfo;
327    int                 numVisualConfigs;
328    __GLXvisualConfig*	pVisualConfigs;
329    VIAConfigPrivPtr	pVisualConfigsPriv;
330    drm_handle_t        agpHandle;
331    drm_handle_t        registerHandle;
332    drm_handle_t        frameBufferHandle;
333    unsigned long       agpAddr;
334    drmAddress          agpMappedAddr;
335    unsigned int        agpSize;
336    Bool                IsPCI;
337    ViaXvMC             xvmc;
338    int                 drmVerMajor;
339    int                 drmVerMinor;
340    int                 drmVerPL;
341    struct buffer_object *driOffScreenMem;
342    void *              driOffScreenSave;
343#endif
344    Bool                DRIIrqEnable;
345    Bool                agpEnable;
346    Bool                dma2d;
347    Bool                dmaXV;
348
349    /* Video */
350    int                 VideoEngine;
351    swovRec             swov;
352    CARD32              VideoStatus;
353    VIAHWDiff           HWDiff;
354    unsigned long       dwV1, dwV3;
355    unsigned long       dwFrameNum;
356
357    CARD32*             VidRegBuffer; /* Temporary buffer for video overlay registers. */
358    unsigned long       VidRegCursor; /* Write cursor for VidRegBuffer. */
359
360    unsigned long       old_dwUseExtendedFIFO;
361
362    ViaSharedPtr        sharedData;
363    Bool                useDmaBlit;
364
365    void                *displayMap;
366    CARD32              displayOffset;
367
368    CARD8               I2CDevices;	/* Option */
369
370#ifdef HAVE_DEBUG
371    Bool                disableXvBWCheck;
372    Bool                DumpVGAROM;
373    Bool                PrintVGARegs;
374    Bool                PrintTVRegs;
375    Bool                I2CScan;
376#endif /* HAVE_DEBUG */
377
378    video_via_regs*     VideoRegs;
379} VIARec, *VIAPtr;
380
381#define VIAPTR(p) ((VIAPtr)((p)->driverPrivate))
382
383typedef struct
384{
385    Bool IsDRIEnabled;
386
387    Bool HasSecondary;
388    Bool BypassSecondary;
389    /*These two registers are used to make sure the CRTC2 is
390      restored before CRTC_EXT, otherwise it could lead to blank screen.*/
391    Bool IsSecondaryRestored;
392    Bool RestorePrimary;
393
394    ScrnInfoPtr pSecondaryScrn;
395    ScrnInfoPtr pPrimaryScrn;
396} VIAEntRec, *VIAEntPtr;
397
398
399/* In via_display.c. */
400const xf86CrtcFuncsRec iga1_crtc_funcs;
401const xf86CrtcFuncsRec iga2_crtc_funcs;
402
403/* In via_exa.c. */
404Bool viaInitExa(ScreenPtr pScreen);
405Bool viaAccelSetMode(int bpp, ViaTwodContext * tdc);
406void viaAccelSync(ScrnInfoPtr);
407void viaExitAccel(ScreenPtr);
408void viaFinishInitAccel(ScreenPtr);
409Bool viaOrder(CARD32 val, CARD32 * shift);
410CARD32 viaBitExpandHelper(CARD32 pixel, CARD32 bits);
411Bool viaCheckUpload(ScrnInfoPtr pScrn, Via3DState * v3d);
412void viaPixelARGB8888(unsigned format, void *pixelP, CARD32 * argb8888);
413Bool viaExpandablePixel(int format);
414void viaAccelFillPixmap(ScrnInfoPtr, unsigned long, unsigned long,
415			int, int, int, int, int, unsigned long);
416void viaAccelTextureBlit(ScrnInfoPtr, unsigned long, unsigned, unsigned,
417			 unsigned, unsigned, unsigned, unsigned,
418			 unsigned long, unsigned, unsigned,
419			 unsigned, unsigned, int);
420#ifdef VIA_DEBUG_COMPOSITE
421void viaExaCompositePictDesc(PicturePtr pict, char *string, int n);
422void viaExaPrintCompositeInfo(char *info, CARD8 op, PicturePtr pSrc, PicturePtr pMask,
423                            PicturePtr pDst);
424#endif
425
426/* In via_exa_h2.c */
427Bool viaExaPrepareSolid_H2(PixmapPtr pPixmap, int alu, Pixel planeMask,
428                        Pixel fg);
429void viaExaSolid_H2(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
430void viaExaDoneSolidCopy_H2(PixmapPtr pPixmap);
431Bool viaExaPrepareCopy_H2(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
432                            int xdir, int ydir, int alu, Pixel planeMask);
433void viaExaCopy_H2(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
434                    int dstY, int width, int height);
435Bool viaExaCheckComposite_H2(int op, PicturePtr pSrcPicture,
436                            PicturePtr pMaskPicture, PicturePtr pDstPicture);
437Bool viaExaPrepareComposite_H2(int op, PicturePtr pSrcPicture,
438                            PicturePtr pMaskPicture, PicturePtr pDstPicture,
439                            PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
440void viaExaComposite_H2(PixmapPtr pDst, int srcX, int srcY,
441                        int maskX, int maskY, int dstX, int dstY,
442                        int width, int height);
443int viaAccelMarkSync_H2(ScreenPtr);
444
445/* In via_exa_h6.c */
446Bool viaExaPrepareSolid_H6(PixmapPtr pPixmap, int alu, Pixel planeMask,
447                        Pixel fg);
448void viaExaSolid_H6(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
449void viaExaDoneSolidCopy_H6(PixmapPtr pPixmap);
450Bool viaExaPrepareCopy_H6(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
451                            int xdir, int ydir, int alu, Pixel planeMask);
452void viaExaCopy_H6(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
453                    int dstY, int width, int height);
454Bool viaExaCheckComposite_H6(int op, PicturePtr pSrcPicture,
455                            PicturePtr pMaskPicture, PicturePtr pDstPicture);
456Bool viaExaPrepareComposite_H6(int op, PicturePtr pSrcPicture,
457                            PicturePtr pMaskPicture, PicturePtr pDstPicture,
458                            PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
459void viaExaComposite_H6(PixmapPtr pDst, int srcX, int srcY,
460                        int maskX, int maskY, int dstX, int dstY,
461                        int width, int height);
462int viaAccelMarkSync_H6(ScreenPtr);
463
464/* In via_xv.c */
465void viaInitVideo(ScreenPtr pScreen);
466void viaExitVideo(ScrnInfoPtr pScrn);
467void viaSaveVideo(ScrnInfoPtr pScrn);
468void viaRestoreVideo(ScrnInfoPtr pScrn);
469void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
470		      Bool reset);
471
472
473/* In via_xv.c */
474void viaInitVideo(ScreenPtr pScreen);
475void viaExitVideo(ScrnInfoPtr pScrn);
476void viaSaveVideo(ScrnInfoPtr pScrn);
477void viaRestoreVideo(ScrnInfoPtr pScrn);
478void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
479		      Bool reset);
480void VIAVidAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
481
482/* In via_memcpy.c */
483typedef void (*vidCopyFunc)(unsigned char *, const unsigned char *,
484                            int, int, int, int);
485extern vidCopyFunc viaVidCopyInit( char *copyType, ScreenPtr pScreen );
486
487/* In via_xwmc.c */
488
489#ifdef HAVE_DRI
490/* Basic init and exit functions */
491void ViaInitXVMC(ScreenPtr pScreen);
492void ViaCleanupXVMC(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr *XvAdaptors, int XvAdaptorCount);
493int viaXvMCInitXv(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr XvAdapt);
494
495/* Returns the size of the fake Xv Image used as XvMC command buffer to the X server*/
496unsigned long viaXvMCPutImageSize(ScrnInfoPtr pScrn);
497#endif
498
499/* via_i2c.c */
500void ViaI2CInit(ScrnInfoPtr pScrn);
501
502#ifdef HAVE_DRI
503Bool VIADRI1ScreenInit(ScreenPtr pScreen);
504void VIADRICloseScreen(ScreenPtr pScreen);
505Bool VIADRIFinishScreenInit(ScreenPtr pScreen);
506void VIADRIRingBufferCleanup(ScrnInfoPtr pScrn);
507Bool VIADRIRingBufferInit(ScrnInfoPtr pScrn);
508void viaDRIOffscreenRestore(ScrnInfoPtr pScrn);
509void viaDRIOffscreenSave(ScrnInfoPtr pScrn);
510Bool VIADRIBufferInit(ScrnInfoPtr pScrn);
511
512#endif /* HAVE_DRI */
513
514int viaOffScreenLinear(struct buffer_object *obj, ScrnInfoPtr pScrn, unsigned long size);
515void viaShowCursor(ScrnInfoPtr pScrn);
516void viaHideCursor(ScrnInfoPtr pScrn);
517Bool viaHWCursorInit(ScreenPtr pScreen);
518void ViaDisplaySetStreamOnCRT(ScrnInfoPtr pScrn, Bool primary);
519void ViaDisplaySetStreamOnDFP(ScrnInfoPtr pScrn, Bool primary);
520
521#endif /* _VIA_DRIVER_H_ */
522