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 
127 static int gVIAEntityIndex = -1;
128 
129 typedef struct {
130     CARD8   SR[256];
131     CARD8   CR[256];
132 } VIARegRec, *VIARegPtr;
133 
134 /*
135  * variables that need to be shared among different screens.
136  */
137 typedef struct {
138     Bool b3DRegsInitialized;
139 } ViaSharedRec, *ViaSharedPtr;
140 
141 enum dri_type {
142 	DRI_NONE,
143 	DRI_1,
144 	DRI_2
145 };
146 
147 #ifdef HAVE_DRI
148 
149 #define VIA_XVMC_MAX_BUFFERS 2
150 #define VIA_XVMC_MAX_CONTEXTS 4
151 #define VIA_XVMC_MAX_SURFACES 20
152 
153 typedef struct {
154     struct buffer_object *memory_ref;
155     unsigned long offsets[VIA_XVMC_MAX_BUFFERS];
156 } ViaXvMCSurfacePriv;
157 
158 typedef struct {
159     drm_context_t drmCtx;
160 } ViaXvMCContextPriv;
161 
162 typedef struct {
163     XID contexts[VIA_XVMC_MAX_CONTEXTS];
164     XID surfaces[VIA_XVMC_MAX_SURFACES];
165     ViaXvMCSurfacePriv *sPrivs[VIA_XVMC_MAX_SURFACES];
166     ViaXvMCContextPriv *cPrivs[VIA_XVMC_MAX_CONTEXTS];
167     int nContexts,nSurfaces;
168     drm_handle_t mmioBase,fbBase,sAreaBase;
169     unsigned sAreaSize;
170     drmAddress sAreaAddr;
171     unsigned activePorts;
172 }ViaXvMC, *ViaXvMCPtr;
173 
174 #endif
175 
176 typedef struct _twodContext {
177     CARD32 mode;
178     CARD32 cmd;
179     CARD32 fgColor;
180     CARD32 bgColor;
181     CARD32 pattern0;
182     CARD32 pattern1;
183     CARD32 patternAddr;
184     CARD32 keyControl;
185     unsigned srcOffset;
186     unsigned srcPitch;
187     unsigned Bpp;
188     unsigned bytesPPShift;
189     Bool clipping;
190     Bool dashed;
191     int clipX1;
192     int clipX2;
193     int clipY1;
194     int clipY2;
195 } ViaTwodContext;
196 
197 typedef struct _VIA {
198     VIARegRec           SavedReg;
199     int                 Bpp, Bpl;
200 
201     Bool                KMS;
202     Bool                FirstInit;
203     unsigned long       videoRambytes;
204     int                 FBFreeStart;
205     int                 FBFreeEnd;
206     int                 driSize;
207     int                 maxDriSize;
208     struct buffer_object *vq_bo;
209     int                 VQStart;
210     int                 VQEnd;
211 
212     /* These are physical addresses. */
213     unsigned long       FrameBufferBase;
214     unsigned long       MmioBase;
215 
216     /* These are linear addresses. */
217     unsigned char*      MapBase;
218     unsigned char*      MpegMapBase;
219     unsigned char*      BltBase;
220     unsigned char*      MapBaseDense;
221     unsigned char*      FBBase;
222     CARD8               MemClk;
223 
224     /* Here are all the Options */
225     Bool                VQEnable;
226     Bool                NoAccel;
227     Bool                shadowFB;
228     Rotation            rotate;
229     int                 agpMem;
230 
231 	CreateScreenResourcesProcPtr CreateScreenResources;
232     CloseScreenProcPtr  CloseScreen;
233 #ifdef HAVE_PCIACCESS
234     struct pci_device  *PciInfo;
235 #else
236     pciVideoPtr         PciInfo;
237     PCITAG PciTag;
238 #endif
239     int                 Chipset;
240     int                 ChipId;
241     int                 ChipRev;
242     int                 EntityIndex;
243 
244     /* Support for shadowFB and rotation */
245     unsigned char*      ShadowPtr;
246 
247     /* Support for EXA acceleration */
248     ViaTwodContext      td;
249     Via3DState          v3d;
250     Via3DState          *lastToUpload;
251     ViaCommandBuffer    cb;
252     int                 accelMarker;
253     struct buffer_object *exa_sync_bo;
254     struct buffer_object *exaMem;
255     CARD32              markerOffset;
256     void               *markerBuf;
257     CARD32              curMarker;
258     CARD32              lastMarkerRead;
259     Bool                agpDMA;
260     Bool                nPOT[VIA_NUM_TEXUNITS];
261     const unsigned     *HqvCmeRegs;
262     ExaDriverPtr        exaDriverPtr;
263     ExaOffscreenArea   *exa_scratch;
264     unsigned int        exa_scratch_next;
265     Bool                useEXA;
266     void               *maskP;
267     CARD32              maskFormat;
268     Bool                componentAlpha;
269     void               *srcP;
270     CARD32              srcFormat;
271     unsigned            scratchOffset;
272     int                 exaScratchSize;
273     char *              scratchAddr;
274     Bool                noComposite;
275     struct buffer_object *scratchBuffer;
276 #ifdef HAVE_DRI
277     struct buffer_object *texAGPBuffer;
278     char *              dBounce;
279 #endif
280 
281     /* Rotation */
282     Bool    RandRRotation;
283 
284     /* BIOS Info Ptr */
285     VIABIOSInfoPtr      pBIOSInfo;
286 
287     /* VIA Technologies NanoBook reference design.
288        Examples include Everex CloudBook and Sylvania g netbook.
289        It is also called FIC CE260 and CE261 by its ODM (Original
290        Design Manufacturer) name. */
291     Bool                isVIANanoBook;
292 
293     /* OLPC XO-1.5 */
294     Bool                IsOLPCXO15;
295 
296     /* I2C & DDC */
297     I2CBusPtr           pI2CBus1;
298     I2CBusPtr           pI2CBus2;
299     I2CBusPtr           pI2CBus3;
300 
301     /* MHS */
302     Bool                IsSecondary;
303     Bool                HasSecondary;
304     Bool                SAMM;
305 
306     drmmode_rec         drmmode;
307     enum dri_type       directRenderingType;
308 #ifdef HAVE_DRI
309     Bool                XvMCEnabled;
310     DRIInfoPtr          pDRIInfo;
311     int                 numVisualConfigs;
312     __GLXvisualConfig*	pVisualConfigs;
313     VIAConfigPrivPtr	pVisualConfigsPriv;
314     drm_handle_t        agpHandle;
315     drm_handle_t        registerHandle;
316     drm_handle_t        frameBufferHandle;
317     unsigned long       agpAddr;
318     drmAddress          agpMappedAddr;
319     unsigned int        agpSize;
320     Bool                IsPCI;
321     ViaXvMC             xvmc;
322     int                 drmVerMajor;
323     int                 drmVerMinor;
324     int                 drmVerPL;
325     struct buffer_object *driOffScreenMem;
326     void *              driOffScreenSave;
327 #endif
328     Bool                DRIIrqEnable;
329     Bool                agpEnable;
330     Bool                dma2d;
331     Bool                dmaXV;
332 
333     /* Video */
334     int                 VideoEngine;
335     swovRec             swov;
336     CARD32              VideoStatus;
337     VIAHWDiff           HWDiff;
338     unsigned long       dwV1, dwV3;
339     unsigned long       dwFrameNum;
340 
341     CARD32*             VidRegBuffer; /* Temporary buffer for video overlay registers. */
342     unsigned long       VidRegCursor; /* Write cursor for VidRegBuffer. */
343 
344     unsigned long       old_dwUseExtendedFIFO;
345 
346     ViaSharedPtr        sharedData;
347     Bool                useDmaBlit;
348 
349     void                *displayMap;
350     CARD32              displayOffset;
351 
352     CARD8               I2CDevices;	/* Option */
353 
354 #ifdef HAVE_DEBUG
355     Bool                disableXvBWCheck;
356     Bool                DumpVGAROM;
357     Bool                PrintVGARegs;
358     Bool                PrintTVRegs;
359     Bool                I2CScan;
360 #endif /* HAVE_DEBUG */
361 
362     video_via_regs*     VideoRegs;
363 
364     /* Keeping track of the number of analog VGA connectors. */
365     unsigned int        numberVGA;
366 
367     /* Keeping track of the number of DVI connectors. */
368     unsigned int        numberDVI;
369 
370     /* Keeping track of the number of FP (Flat Panel) connectors. */
371     unsigned int        numberFP;
372 
373     /* Shadow copy of CR3B through CR3F. */
374     CARD8       originalCR3B, originalCR3C, originalCR3D,
375                 originalCR3E, originalCR3F;
376 } VIARec, *VIAPtr;
377 
378 #define VIAPTR(p) ((VIAPtr)((p)->driverPrivate))
379 
380 typedef struct
381 {
382     Bool IsDRIEnabled;
383 
384     Bool HasSecondary;
385     Bool BypassSecondary;
386     /*These two registers are used to make sure the CRTC2 is
387       restored before CRTC_EXT, otherwise it could lead to blank screen.*/
388     Bool IsSecondaryRestored;
389     Bool RestorePrimary;
390 
391     ScrnInfoPtr pSecondaryScrn;
392     ScrnInfoPtr pPrimaryScrn;
393 } VIAEntRec, *VIAEntPtr;
394 
395 
396 /* In via_display.c. */
397 extern const xf86CrtcFuncsRec iga1_crtc_funcs;
398 extern const xf86CrtcFuncsRec iga2_crtc_funcs;
399 
400 /* In via_exa.c. */
401 Bool viaInitExa(ScreenPtr pScreen);
402 Bool viaAccelSetMode(int bpp, ViaTwodContext * tdc);
403 void viaAccelSync(ScrnInfoPtr);
404 void viaExitAccel(ScreenPtr);
405 void viaFinishInitAccel(ScreenPtr);
406 Bool viaOrder(CARD32 val, CARD32 * shift);
407 CARD32 viaBitExpandHelper(CARD32 pixel, CARD32 bits);
408 Bool viaCheckUpload(ScrnInfoPtr pScrn, Via3DState * v3d);
409 void viaPixelARGB8888(unsigned format, void *pixelP, CARD32 * argb8888);
410 Bool viaExpandablePixel(int format);
411 void viaAccelFillPixmap(ScrnInfoPtr, unsigned long, unsigned long,
412 			int, int, int, int, int, unsigned long);
413 void viaAccelTextureBlit(ScrnInfoPtr, unsigned long, unsigned, unsigned,
414 			 unsigned, unsigned, unsigned, unsigned,
415 			 unsigned long, unsigned, unsigned,
416 			 unsigned, unsigned, int);
417 #ifdef VIA_DEBUG_COMPOSITE
418 void viaExaCompositePictDesc(PicturePtr pict, char *string, int n);
419 void viaExaPrintCompositeInfo(char *info, CARD8 op, PicturePtr pSrc, PicturePtr pMask,
420                             PicturePtr pDst);
421 #endif
422 
423 /* In via_exa_h2.c */
424 Bool viaExaPrepareSolid_H2(PixmapPtr pPixmap, int alu, Pixel planeMask,
425                         Pixel fg);
426 void viaExaSolid_H2(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
427 void viaExaDoneSolidCopy_H2(PixmapPtr pPixmap);
428 Bool viaExaPrepareCopy_H2(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
429                             int xdir, int ydir, int alu, Pixel planeMask);
430 void viaExaCopy_H2(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
431                     int dstY, int width, int height);
432 Bool viaExaCheckComposite_H2(int op, PicturePtr pSrcPicture,
433                             PicturePtr pMaskPicture, PicturePtr pDstPicture);
434 Bool viaExaPrepareComposite_H2(int op, PicturePtr pSrcPicture,
435                             PicturePtr pMaskPicture, PicturePtr pDstPicture,
436                             PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
437 void viaExaComposite_H2(PixmapPtr pDst, int srcX, int srcY,
438                         int maskX, int maskY, int dstX, int dstY,
439                         int width, int height);
440 int viaAccelMarkSync_H2(ScreenPtr);
441 
442 /* In via_exa_h6.c */
443 Bool viaExaPrepareSolid_H6(PixmapPtr pPixmap, int alu, Pixel planeMask,
444                         Pixel fg);
445 void viaExaSolid_H6(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);
446 void viaExaDoneSolidCopy_H6(PixmapPtr pPixmap);
447 Bool viaExaPrepareCopy_H6(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
448                             int xdir, int ydir, int alu, Pixel planeMask);
449 void viaExaCopy_H6(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX,
450                     int dstY, int width, int height);
451 Bool viaExaCheckComposite_H6(int op, PicturePtr pSrcPicture,
452                             PicturePtr pMaskPicture, PicturePtr pDstPicture);
453 Bool viaExaPrepareComposite_H6(int op, PicturePtr pSrcPicture,
454                             PicturePtr pMaskPicture, PicturePtr pDstPicture,
455                             PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst);
456 void viaExaComposite_H6(PixmapPtr pDst, int srcX, int srcY,
457                         int maskX, int maskY, int dstX, int dstY,
458                         int width, int height);
459 int viaAccelMarkSync_H6(ScreenPtr);
460 
461 /* In via_xv.c */
462 void viaInitVideo(ScreenPtr pScreen);
463 void viaExitVideo(ScrnInfoPtr pScrn);
464 void viaSaveVideo(ScrnInfoPtr pScrn);
465 void viaRestoreVideo(ScrnInfoPtr pScrn);
466 void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
467 		      Bool reset);
468 
469 
470 /* In via_xv.c */
471 void viaInitVideo(ScreenPtr pScreen);
472 void viaExitVideo(ScrnInfoPtr pScrn);
473 void viaSaveVideo(ScrnInfoPtr pScrn);
474 void viaRestoreVideo(ScrnInfoPtr pScrn);
475 void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
476 		      Bool reset);
477 void VIAVidAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
478 
479 /* In via_memcpy.c */
480 typedef void (*vidCopyFunc)(unsigned char *, const unsigned char *,
481                             int, int, int, int);
482 extern vidCopyFunc viaVidCopyInit( char *copyType, ScreenPtr pScreen );
483 
484 /* In via_xwmc.c */
485 
486 #ifdef HAVE_DRI
487 /* Basic init and exit functions */
488 void ViaInitXVMC(ScreenPtr pScreen);
489 void ViaCleanupXVMC(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr *XvAdaptors, int XvAdaptorCount);
490 int viaXvMCInitXv(ScrnInfoPtr pScrn, XF86VideoAdaptorPtr XvAdapt);
491 
492 /* Returns the size of the fake Xv Image used as XvMC command buffer to the X server*/
493 unsigned long viaXvMCPutImageSize(ScrnInfoPtr pScrn);
494 #endif
495 
496 /* via_i2c.c */
497 void ViaI2CInit(ScrnInfoPtr pScrn);
498 Bool xf86I2CMaskByte(I2CDevPtr d, I2CByte subaddr,
499                         I2CByte value, I2CByte mask);
500 
501 #ifdef HAVE_DRI
502 Bool VIADRI1ScreenInit(ScreenPtr pScreen);
503 void VIADRICloseScreen(ScreenPtr pScreen);
504 Bool VIADRIFinishScreenInit(ScreenPtr pScreen);
505 void VIADRIRingBufferCleanup(ScrnInfoPtr pScrn);
506 Bool VIADRIRingBufferInit(ScrnInfoPtr pScrn);
507 void viaDRIOffscreenRestore(ScrnInfoPtr pScrn);
508 void viaDRIOffscreenSave(ScrnInfoPtr pScrn);
509 Bool VIADRIBufferInit(ScrnInfoPtr pScrn);
510 
511 #endif /* HAVE_DRI */
512 
513 int viaOffScreenLinear(struct buffer_object *obj, ScrnInfoPtr pScrn, unsigned long size);
514 void viaShowCursor(ScrnInfoPtr pScrn);
515 void viaHideCursor(ScrnInfoPtr pScrn);
516 Bool viaHWCursorInit(ScreenPtr pScreen);
517 void ViaDisplaySetStreamOnCRT(ScrnInfoPtr pScrn, Bool primary);
518 void ViaDisplaySetStreamOnDFP(ScrnInfoPtr pScrn, Bool primary);
519 
520 #endif /* _VIA_DRIVER_H_ */
521