1 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/via/via_dri.c,v 1.4 2003/09/24 02:43:30 dawes Exp $ */
3 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
4 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 #include "xf86_OSproc.h"
28 #include "xf86_ansic.h"
31 #include "xf86PciInfo.h"
34 #define _XF86DRI_SERVER_
35 #include "GL/glxtokens.h"
52 #include "via_context.h"
54 #include "via_driver.h"
55 #include "via_common.h"
58 static void VIAEnableMMIO(DRIDriverContext
* ctx
);
59 static void VIADisableMMIO(DRIDriverContext
* ctx
);
60 static void VIADisableExtendedFIFO(DRIDriverContext
*ctx
);
61 static void VIAEnableExtendedFIFO(DRIDriverContext
*ctx
);
62 static void VIAInitialize2DEngine(DRIDriverContext
*ctx
);
63 static void VIAInitialize3DEngine(DRIDriverContext
*ctx
);
65 static int VIADRIScreenInit(DRIDriverContext
* ctx
);
66 static void VIADRICloseScreen(DRIDriverContext
* ctx
);
67 static int VIADRIFinishScreenInit(DRIDriverContext
* ctx
);
69 /* _SOLO : missing macros normally defined by X code */
70 #define xf86DrvMsg(a, b, ...) fprintf(stderr, __VA_ARGS__)
71 #define MMIO_IN8(base, addr) ((*(((volatile uint8_t*)base)+(addr)))+0)
72 #define MMIO_OUT8(base, addr, val) ((*(((volatile uint8_t*)base)+(addr)))=((uint8_t)val))
73 #define MMIO_OUT16(base, addr, val) ((*(volatile uint16_t*)(((uint8_t*)base)+(addr)))=((uint16_t)val))
77 #define AGP_PAGE_SIZE 4096
78 #define AGP_PAGES 8192
79 #define AGP_SIZE (AGP_PAGE_SIZE * AGP_PAGES)
80 #define AGP_CMDBUF_PAGES 256
81 #define AGP_CMDBUF_SIZE (AGP_PAGE_SIZE * AGP_CMDBUF_PAGES)
83 static char VIAKernelDriverName
[] = "via";
84 static char VIAClientDriverName
[] = "via";
86 static int VIADRIAgpInit(const DRIDriverContext
*ctx
, VIAPtr pVia
);
87 static int VIADRIPciInit(DRIDriverContext
* ctx
, VIAPtr pVia
);
88 static int VIADRIFBInit(DRIDriverContext
* ctx
, VIAPtr pVia
);
89 static int VIADRIKernelInit(DRIDriverContext
* ctx
, VIAPtr pVia
);
90 static int VIADRIMapInit(DRIDriverContext
* ctx
, VIAPtr pVia
);
92 static int VIADRIAgpInit(const DRIDriverContext
*ctx
, VIAPtr pVia
)
94 unsigned long agp_phys
;
97 pVIADRI
= pVia
->devPrivate
;
100 if (drmAgpAcquire(pVia
->drmFD
) < 0) {
101 xf86DrvMsg(pScreen
->myNum
, X_ERROR
, "[drm] drmAgpAcquire failed %d\n", errno
);
105 if (drmAgpEnable(pVia
->drmFD
, drmAgpGetMode(pVia
->drmFD
)&~0x0) < 0) {
106 xf86DrvMsg(pScreen
->myNum
, X_ERROR
, "[drm] drmAgpEnable failed\n");
110 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] drmAgpEnabled succeeded\n");
112 if (drmAgpAlloc(pVia
->drmFD
, AGP_SIZE
, 0, &agp_phys
, &pVia
->agpHandle
) < 0) {
113 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
114 "[drm] drmAgpAlloc failed\n");
115 drmAgpRelease(pVia
->drmFD
);
119 if (drmAgpBind(pVia
->drmFD
, pVia
->agpHandle
, 0) < 0) {
120 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
121 "[drm] drmAgpBind failed\n");
122 drmAgpFree(pVia
->drmFD
, pVia
->agpHandle
);
123 drmAgpRelease(pVia
->drmFD
);
128 pVia
->agpSize
= AGP_SIZE
;
129 pVia
->agpAddr
= drmAgpBase(pVia
->drmFD
);
130 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
131 "[drm] agpAddr = 0x%08lx\n",pVia
->agpAddr
);
133 pVIADRI
->agp
.size
= pVia
->agpSize
;
134 if (drmAddMap(pVia
->drmFD
, (drm_handle_t
)0,
135 pVIADRI
->agp
.size
, DRM_AGP
, 0,
136 &pVIADRI
->agp
.handle
) < 0) {
137 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
138 "[drm] Failed to map public agp area\n");
139 pVIADRI
->agp
.size
= 0;
142 /* Map AGP from kernel to Xserver - Not really needed */
143 drmMap(pVia
->drmFD
, pVIADRI
->agp
.handle
,pVIADRI
->agp
.size
,
144 (drmAddressPtr
)&agpaddr
);
147 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
148 "[drm] agpBase = 0x%08lx\n", pVia
->agpBase
);
149 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
150 "[drm] agpAddr = 0x%08lx\n", pVia
->agpAddr
);
152 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
153 "[drm] agpSize = 0x%08lx\n", pVia
->agpSize
);
154 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
155 "[drm] agp physical addr = 0x%08lx\n", agp_phys
);
157 drmVIAAgpInit(pVia
->drmFD
, 0, AGP_SIZE
);
162 static int VIADRIFBInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
164 int FBSize
= pVia
->FBFreeEnd
-pVia
->FBFreeStart
;
165 int FBOffset
= pVia
->FBFreeStart
;
166 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
167 pVIADRI
->fbOffset
= FBOffset
;
168 pVIADRI
->fbSize
= pVia
->videoRambytes
;
170 if (drmVIAFBInit(pVia
->drmFD
, FBOffset
, FBSize
) < 0) {
171 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,"[drm] failed to init frame buffer area\n");
175 xf86DrvMsg(pScreen
->myNum
, X_INFO
,"[drm] FBFreeStart= 0x%08lx FBFreeEnd= 0x%08lx FBSize= 0x%08lx\n", pVia
->FBFreeStart
, pVia
->FBFreeEnd
, FBSize
);
180 static int VIADRIPciInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
185 static int VIADRIScreenInit(DRIDriverContext
* ctx
)
187 VIAPtr pVia
= VIAPTR(ctx
);
192 ctx
->shared
.SAREASize
= ((sizeof(drm_sarea_t
) + 0xfff) & 0x1000);
194 if (sizeof(drm_sarea_t
)+sizeof(VIASAREAPriv
) > SAREA_MAX
) {
195 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
196 "Data does not fit in SAREA\n");
199 ctx
->shared
.SAREASize
= SAREA_MAX
;
202 ctx
->drmFD
= drmOpen(VIAKernelDriverName
, NULL
);
203 if (ctx
->drmFD
< 0) {
204 fprintf(stderr
, "[drm] drmOpen failed\n");
207 pVia
->drmFD
= ctx
->drmFD
;
209 err
= drmSetBusid(ctx
->drmFD
, ctx
->pciBusID
);
211 fprintf(stderr
, "[drm] drmSetBusid failed (%d, %s), %s\n",
212 ctx
->drmFD
, ctx
->pciBusID
, strerror(-err
));
216 err
= drmAddMap(ctx
->drmFD
, 0, ctx
->shared
.SAREASize
, DRM_SHM
,
217 DRM_CONTAINS_LOCK
, &ctx
->shared
.hSAREA
);
219 fprintf(stderr
, "[drm] drmAddMap failed\n");
222 fprintf(stderr
, "[drm] added %d byte SAREA at 0x%08lx\n",
223 ctx
->shared
.SAREASize
, ctx
->shared
.hSAREA
);
225 if (drmMap(ctx
->drmFD
,
227 ctx
->shared
.SAREASize
,
228 (drmAddressPtr
)(&ctx
->pSAREA
)) < 0)
230 fprintf(stderr
, "[drm] drmMap failed\n");
233 memset(ctx
->pSAREA
, 0, ctx
->shared
.SAREASize
);
234 fprintf(stderr
, "[drm] mapped SAREA 0x%08lx to %p, size %d\n",
235 ctx
->shared
.hSAREA
, ctx
->pSAREA
, ctx
->shared
.SAREASize
);
237 /* Need to AddMap the framebuffer and mmio regions here:
239 if (drmAddMap(ctx
->drmFD
,
240 (drm_handle_t
)ctx
->FBStart
,
248 &ctx
->shared
.hFrameBuffer
) < 0)
250 fprintf(stderr
, "[drm] drmAddMap framebuffer failed\n");
254 fprintf(stderr
, "[drm] framebuffer handle = 0x%08lx\n",
255 ctx
->shared
.hFrameBuffer
);
257 pVIADRI
= (VIADRIPtr
) CALLOC(sizeof(VIADRIRec
));
259 drmClose(ctx
->drmFD
);
262 pVia
->devPrivate
= pVIADRI
;
263 ctx
->driverClientMsg
= pVIADRI
;
264 ctx
->driverClientMsgSize
= sizeof(*pVIADRI
);
266 pVia
->IsPCI
= !VIADRIAgpInit(ctx
, pVia
);
269 VIADRIPciInit(ctx
, pVia
);
270 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] use pci.\n" );
273 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] use agp.\n" );
275 if (!(VIADRIFBInit(ctx
, pVia
))) {
276 VIADRICloseScreen(ctx
);
277 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "[dri] frame buffer initialize fial .\n" );
281 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] frame buffer initialized.\n" );
283 /* DRIScreenInit doesn't add all the common mappings. Add additional mappings here. */
284 if (!VIADRIMapInit(ctx
, pVia
)) {
285 VIADRICloseScreen(ctx
);
288 pVIADRI
->regs
.size
= VIA_MMIO_REGSIZE
;
289 pVIADRI
->regs
.map
= 0;
290 pVIADRI
->regs
.handle
= pVia
->registerHandle
;
291 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] mmio Registers = 0x%08lx\n",
292 pVIADRI
->regs
.handle
);
294 /*pVIADRI->drixinerama = pVia->drixinerama;*/
295 /*=* John Sheng [2003.12.9] Tuxracer & VQ *=*/
296 pVIADRI
->VQEnable
= pVia
->VQEnable
;
298 if (drmMap(pVia
->drmFD
,
299 pVIADRI
->regs
.handle
,
301 (drmAddress
*)&pVia
->MapBase
) != 0)
303 VIADRICloseScreen(ctx
);
307 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] mmio mapped.\n" );
309 return VIADRIFinishScreenInit(ctx
);
313 VIADRICloseScreen(DRIDriverContext
* ctx
)
315 VIAPtr pVia
= VIAPTR(ctx
);
316 VIADRIPtr pVIADRI
=(VIADRIPtr
)pVia
->devPrivate
;
319 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Unmapping MMIO registers\n");
320 drmUnmap(pVia
->MapBase
, pVIADRI
->regs
.size
);
324 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Freeing agp memory\n");
325 drmAgpFree(pVia
->drmFD
, pVia
->agpHandle
);
326 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Releasing agp module\n");
327 drmAgpRelease(pVia
->drmFD
);
332 VIADRIFinishScreenInit(DRIDriverContext
* ctx
)
334 VIAPtr pVia
= VIAPTR(ctx
);
338 err
= drmCreateContext(ctx
->drmFD
, &ctx
->serverContext
);
340 fprintf(stderr
, "%s: drmCreateContext failed %d\n", __FUNCTION__
, err
);
344 DRM_LOCK(ctx
->drmFD
, ctx
->pSAREA
, ctx
->serverContext
, 0);
347 if (!VIADRIKernelInit(ctx
, pVia
)) {
348 VIADRICloseScreen(ctx
);
351 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[dri] kernel data initialized.\n");
353 /* set SAREA value */
355 VIASAREAPriv
*saPriv
;
357 saPriv
=(VIASAREAPriv
*)(((char*)ctx
->pSAREA
) +
358 sizeof(drm_sarea_t
));
360 memset(saPriv
, 0, sizeof(*saPriv
));
361 saPriv
->CtxOwner
= -1;
363 pVIADRI
=(VIADRIPtr
)pVia
->devPrivate
;
364 pVIADRI
->deviceID
=pVia
->Chipset
;
365 pVIADRI
->width
=ctx
->shared
.virtualWidth
;
366 pVIADRI
->height
=ctx
->shared
.virtualHeight
;
367 pVIADRI
->mem
=ctx
->shared
.fbSize
;
368 pVIADRI
->bytesPerPixel
= (ctx
->bpp
+7) / 8;
369 pVIADRI
->sarea_priv_offset
= sizeof(drm_sarea_t
);
371 pVIADRI
->scrnX
=pVIADRI
->width
;
372 pVIADRI
->scrnY
=pVIADRI
->height
;
377 /* Initialize the kernel data structures. */
378 static int VIADRIKernelInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
381 memset(&drmInfo
, 0, sizeof(drmVIAInit
));
382 drmInfo
.sarea_priv_offset
= sizeof(drm_sarea_t
);
383 drmInfo
.fb_offset
= pVia
->FrameBufferBase
;
384 drmInfo
.mmio_offset
= pVia
->registerHandle
;
386 drmInfo
.agpAddr
= (uint32_t)NULL
;
388 drmInfo
.agpAddr
= (uint32_t)pVia
->agpAddr
;
390 if (drmVIAInitMAP(pVia
->drmFD
, &drmInfo
) < 0) return GL_FALSE
;
394 /* Add a map for the MMIO registers */
395 static int VIADRIMapInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
399 if (drmAddMap(pVia
->drmFD
, pVia
->MmioBase
, VIA_MMIO_REGSIZE
,
400 DRM_REGISTERS
, flags
, &pVia
->registerHandle
) < 0) {
404 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
405 "[drm] register handle = 0x%08lx\n", pVia
->registerHandle
);
410 const __GLcontextModes __glModes
[] =
412 /* 32 bit, RGBA Depth=16 Stencil=8 */
413 {.rgbMode
= GL_TRUE
, .colorIndexMode
= GL_FALSE
, .doubleBufferMode
= GL_TRUE
, .stereoMode
= GL_FALSE
,
414 .haveAccumBuffer
= GL_FALSE
, .haveDepthBuffer
= GL_TRUE
, .haveStencilBuffer
= GL_TRUE
,
415 .redBits
= 8, .greenBits
= 8, .blueBits
= 8, .alphaBits
= 8,
416 .redMask
= 0xff0000, .greenMask
= 0xff00, .blueMask
= 0xff, .alphaMask
= 0xff000000,
417 .rgbBits
= 32, .indexBits
= 0,
418 .accumRedBits
= 0, .accumGreenBits
= 0, .accumBlueBits
= 0, .accumAlphaBits
= 0,
419 .depthBits
= 16, .stencilBits
= 8,
420 .numAuxBuffers
= 0, .level
= 0, .pixmapMode
= GL_TRUE
, },
423 /* 16 bit, RGB Depth=16 */
424 {.rgbMode
= GL_TRUE
, .colorIndexMode
= GL_FALSE
, .doubleBufferMode
= GL_TRUE
, .stereoMode
= GL_FALSE
,
425 .haveAccumBuffer
= GL_FALSE
, .haveDepthBuffer
= GL_TRUE
, .haveStencilBuffer
= GL_FALSE
,
426 .redBits
= 5, .greenBits
= 6, .blueBits
= 5, .alphaBits
= 0,
427 .redMask
= 0xf800, .greenMask
= 0x07e0, .blueMask
= 0x001f, .alphaMask
= 0x0,
428 .rgbBits
= 16, .indexBits
= 0,
429 .accumRedBits
= 0, .accumGreenBits
= 0, .accumBlueBits
= 0, .accumAlphaBits
= 0,
430 .depthBits
= 16, .stencilBits
= 0,
431 .numAuxBuffers
= 0, .level
= 0, .pixmapMode
= GL_TRUE
, },
435 static int viaInitContextModes(const DRIDriverContext
*ctx
,
436 int *numModes
, const __GLcontextModes
**modes
)
438 *numModes
= sizeof(__glModes
)/sizeof(__glModes
[0]);
439 *modes
= &__glModes
[0];
443 static int viaValidateMode(const DRIDriverContext
*ctx
)
445 VIAPtr pVia
= VIAPTR(ctx
);
450 static int viaPostValidateMode(const DRIDriverContext
*ctx
)
452 VIAPtr pVia
= VIAPTR(ctx
);
457 static void VIAEnableMMIO(DRIDriverContext
* ctx
)
459 /*vgaHWPtr hwp = VGAHWPTR(ctx);*/
460 VIAPtr pVia
= VIAPTR(ctx
);
464 if (xf86IsPrimaryPci(pVia
->PciInfo
)) {
465 /* If we are primary card, we still use std vga port. If we use
466 * MMIO, system will hang in vgaHWSave when our card used in
467 * PLE and KLE (integrated Trident MVP4)
469 vgaHWSetStdFuncs(hwp
);
472 vgaHWSetMmioFuncs(hwp
, pVia
->MapBase
, 0x8000);
477 VGAOUT8(0x3c3, val
| 0x01);
479 VGAOUT8(0x3c2, val
| 0x01);
481 /* Unlock Extended IO Space */
482 VGAOUT8(0x3c4, 0x10);
483 VGAOUT8(0x3c5, 0x01);
486 if(!pVia
->IsSecondary
) {
487 VGAOUT8(0x3c4, 0x1a);
490 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "primary val = %x\n", val
);
492 VGAOUT8(0x3c5, val
| 0x68);
495 VGAOUT8(0x3c4, 0x1a);
498 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "secondary val = %x\n", val
);
500 VGAOUT8(0x3c5, val
| 0x38);
503 /* Unlock CRTC registers */
504 VGAOUT8(0x3d4, 0x47);
505 VGAOUT8(0x3d5, 0x00);
510 static void VIADisableMMIO(DRIDriverContext
* ctx
)
512 VIAPtr pVia
= VIAPTR(ctx
);
515 VGAOUT8(0x3c4, 0x1a);
517 VGAOUT8(0x3c5, val
& 0x97);
522 static void VIADisableExtendedFIFO(DRIDriverContext
*ctx
)
524 VIAPtr pVia
= VIAPTR(ctx
);
525 uint32_t dwGE230
, dwGE298
;
527 /* Cause of exit XWindow will dump back register value, others chipset no
528 * need to set extended fifo value */
529 if (pVia
->Chipset
== VIA_CLE266
&& pVia
->ChipRev
< 15 &&
530 (ctx
->shared
.virtualWidth
> 1024 || pVia
->HasSecondary
)) {
531 /* Turn off Extend FIFO */
533 dwGE298
= VIAGETREG(0x298);
534 VIASETREG(0x298, dwGE298
| 0x20000000);
536 dwGE230
= VIAGETREG(0x230);
537 VIASETREG(0x230, dwGE230
& ~0x00200000);
539 dwGE298
= VIAGETREG(0x298);
540 VIASETREG(0x298, dwGE298
& ~0x20000000);
544 static void VIAEnableExtendedFIFO(DRIDriverContext
*ctx
)
546 VIAPtr pVia
= VIAPTR(ctx
);
548 uint32_t dwGE230
, dwGE298
;
550 switch (pVia
->Chipset
) {
552 if (pVia
->ChipRev
> 14) { /* For 3123Cx */
553 if (pVia
->HasSecondary
) { /* SAMM or DuoView case */
554 if (ctx
->shared
.virtualWidth
>= 1024)
557 VGAOUT8(0x3C4, 0x16);
558 bRegTemp
= VGAIN8(0x3C5);
561 VGAOUT8(0x3C5, bRegTemp
);
563 VGAOUT8(0x3C4, 0x17);
564 bRegTemp
= VGAIN8(0x3C5);
567 VGAOUT8(0x3C5, bRegTemp
);
568 pVia
->EnableExtendedFIFO
= GL_TRUE
;
571 else /* Single view or Simultaneoue case */
573 if (ctx
->shared
.virtualWidth
> 1024)
576 VGAOUT8(0x3C4, 0x16);
577 bRegTemp
= VGAIN8(0x3C5);
580 VGAOUT8(0x3C5, bRegTemp
);
582 VGAOUT8(0x3C4, 0x17);
583 bRegTemp
= VGAIN8(0x3C5);
586 VGAOUT8(0x3C5, bRegTemp
);
587 pVia
->EnableExtendedFIFO
= GL_TRUE
;
591 VGAOUT8(0x3C4, 0x18);
592 bRegTemp
= VGAIN8(0x3C5);
595 bRegTemp
|= 0x40; /* force the preq always higher than treq */
596 VGAOUT8(0x3C5, bRegTemp
);
598 else { /* for 3123Ax */
599 if (ctx
->shared
.virtualWidth
> 1024 || pVia
->HasSecondary
) {
600 /* Turn on Extend FIFO */
602 dwGE298
= VIAGETREG(0x298);
603 VIASETREG(0x298, dwGE298
| 0x20000000);
605 dwGE230
= VIAGETREG(0x230);
606 VIASETREG(0x230, dwGE230
| 0x00200000);
608 dwGE298
= VIAGETREG(0x298);
609 VIASETREG(0x298, dwGE298
& ~0x20000000);
612 VGAOUT8(0x3C4, 0x16);
613 bRegTemp
= VGAIN8(0x3C5);
616 /* bRegTemp |= 0x10; */
617 VGAOUT8(0x3C5, bRegTemp
);
619 VGAOUT8(0x3C4, 0x17);
620 bRegTemp
= VGAIN8(0x3C5);
623 /*bRegTemp |= 0x1F;*/
624 VGAOUT8(0x3C5, bRegTemp
);
626 VGAOUT8(0x3C4, 0x18);
627 bRegTemp
= VGAIN8(0x3C5);
630 bRegTemp
|= 0x40; /* force the preq always higher than treq */
631 VGAOUT8(0x3C5, bRegTemp
);
632 pVia
->EnableExtendedFIFO
= GL_TRUE
;
637 if (pVia
->HasSecondary
) { /* SAMM or DuoView case */
638 if ((ctx
->shared
.virtualWidth
>= 1600) &&
639 (pVia
->MemClk
<= VIA_MEM_DDR200
)) {
640 /* enable CRT extendded FIFO */
641 VGAOUT8(0x3C4, 0x17);
642 VGAOUT8(0x3C5, 0x1C);
643 /* revise second display queue depth and read threshold */
644 VGAOUT8(0x3C4, 0x16);
645 bRegTemp
= VGAIN8(0x3C5);
647 bRegTemp
= (bRegTemp
) | (0x09);
648 VGAOUT8(0x3C5, bRegTemp
);
651 /* enable CRT extendded FIFO */
652 VGAOUT8(0x3C4, 0x17);
654 /* revise second display queue depth and read threshold */
655 VGAOUT8(0x3C4, 0x16);
656 bRegTemp
= VGAIN8(0x3C5);
658 bRegTemp
= (bRegTemp
) | (0x1C);
659 VGAOUT8(0x3C5, bRegTemp
);
662 VGAOUT8(0x3C4, 0x18);
663 bRegTemp
= VGAIN8(0x3C5);
666 bRegTemp
|= 0x40; /* force the preq always higher than treq */
667 VGAOUT8(0x3C5, bRegTemp
);
668 pVia
->EnableExtendedFIFO
= GL_TRUE
;
671 if ( (ctx
->shared
.virtualWidth
> 1024) && (ctx
->shared
.virtualWidth
<= 1280) )
673 /* enable CRT extendded FIFO */
674 VGAOUT8(0x3C4, 0x17);
675 VGAOUT8(0x3C5, 0x3F);
676 /* revise second display queue depth and read threshold */
677 VGAOUT8(0x3C4, 0x16);
678 bRegTemp
= VGAIN8(0x3C5);
680 bRegTemp
= (bRegTemp
) | (0x17);
681 VGAOUT8(0x3C5, bRegTemp
);
682 pVia
->EnableExtendedFIFO
= GL_TRUE
;
684 else if ((ctx
->shared
.virtualWidth
> 1280))
686 /* enable CRT extendded FIFO */
687 VGAOUT8(0x3C4, 0x17);
688 VGAOUT8(0x3C5, 0x3F);
689 /* revise second display queue depth and read threshold */
690 VGAOUT8(0x3C4, 0x16);
691 bRegTemp
= VGAIN8(0x3C5);
693 bRegTemp
= (bRegTemp
) | (0x1C);
694 VGAOUT8(0x3C5, bRegTemp
);
695 pVia
->EnableExtendedFIFO
= GL_TRUE
;
699 /* enable CRT extendded FIFO */
700 VGAOUT8(0x3C4, 0x17);
701 VGAOUT8(0x3C5, 0x3F);
702 /* revise second display queue depth and read threshold */
703 VGAOUT8(0x3C4, 0x16);
704 bRegTemp
= VGAIN8(0x3C5);
706 bRegTemp
= (bRegTemp
) | (0x10);
707 VGAOUT8(0x3C5, bRegTemp
);
710 VGAOUT8(0x3C4, 0x18);
711 bRegTemp
= VGAIN8(0x3C5);
714 bRegTemp
|= 0x40; /* force the preq always higher than treq */
715 VGAOUT8(0x3C5, bRegTemp
);
719 /*=* R1 Display FIFO depth (384 /8 -1 -> 0xbf) SR17[7:0] (8bits) *=*/
720 VGAOUT8(0x3c4, 0x17);
721 VGAOUT8(0x3c5, 0xbf);
723 /*=* R2 Display fetch datum threshold value (328/4 -> 0x52)
724 SR16[5:0], SR16[7] (7bits) *=*/
725 VGAOUT8(0x3c4, 0x16);
726 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
727 bRegTemp
|= (0x52 & 0x3F);
728 bRegTemp
|= ((0x52 & 0x40) << 1);
729 VGAOUT8(0x3c5, bRegTemp
);
731 /*=* R3 Switch to the highest agent threshold value (74 -> 0x4a)
732 SR18[5:0], SR18[7] (7bits) *=*/
733 VGAOUT8(0x3c4, 0x18);
734 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
735 bRegTemp
|= (0x4a & 0x3F);
736 bRegTemp
|= ((0x4a & 0x40) << 1);
737 VGAOUT8(0x3c5, bRegTemp
);
739 /*=* R4 Fetch Number for a scan line (unit: 8 bytes)
740 SR1C[7:0], SR1D[1:0] (10bits) *=*/
741 wRegTemp
= (pBIOSInfo
->offsetWidthByQWord
>> 1) + 4;
742 VGAOUT8(0x3c4, 0x1c);
743 VGAOUT8(0x3c5, (uint8_t)(wRegTemp
& 0xFF));
744 VGAOUT8(0x3c4, 0x1d);
745 bRegTemp
= VGAIN8(0x3c5) & ~0x03;
746 VGAOUT8(0x3c5, bRegTemp
| ((wRegTemp
& 0x300) >> 8));
748 if (ctx
->shared
.virtualWidth
>= 1400 && ctx
->bpp
== 32)
750 /*=* Max. length for a request SR22[4:0] (64/4 -> 0x10) *=*/
751 VGAOUT8(0x3c4, 0x22);
752 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
753 VGAOUT8(0x3c5, bRegTemp
| 0x10);
757 /*=* Max. length for a request SR22[4:0]
758 (128/4 -> over flow 0x0) *=*/
759 VGAOUT8(0x3c4, 0x22);
760 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
761 VGAOUT8(0x3c5, bRegTemp
);
765 /*=* R1 Display FIFO depth (96-1 -> 0x5f) SR17[7:0] (8bits) *=*/
766 VGAOUT8(0x3c4, 0x17);
767 VGAOUT8(0x3c5, 0x5f);
769 /*=* R2 Display fetch datum threshold value (32 -> 0x20)
770 SR16[5:0], SR16[7] (7bits) *=*/
771 VGAOUT8(0x3c4, 0x16);
772 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
773 bRegTemp
|= (0x20 & 0x3F);
774 bRegTemp
|= ((0x20 & 0x40) << 1);
775 VGAOUT8(0x3c5, bRegTemp
);
777 /*=* R3 Switch to the highest agent threshold value (16 -> 0x10)
778 SR18[5:0], SR18[7] (7bits) *=*/
779 VGAOUT8(0x3c4, 0x18);
780 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
781 bRegTemp
|= (0x10 & 0x3F);
782 bRegTemp
|= ((0x10 & 0x40) << 1);
783 VGAOUT8(0x3c5, bRegTemp
);
785 /*=* R4 Fetch Number for a scan line (unit: 8 bytes)
786 SR1C[7:0], SR1D[1:0] (10bits) *=*/
787 wRegTemp
= (pBIOSInfo
->offsetWidthByQWord
>> 1) + 4;
788 VGAOUT8(0x3c4, 0x1c);
789 VGAOUT8(0x3c5, (uint8_t)(wRegTemp
& 0xFF));
790 VGAOUT8(0x3c4, 0x1d);
791 bRegTemp
= VGAIN8(0x3c5) & ~0x03;
792 VGAOUT8(0x3c5, bRegTemp
| ((wRegTemp
& 0x300) >> 8));
794 if (ctx
->shared
.virtualWidth
>= 1400 && ctx
->bpp
== 32)
796 /*=* Max. length for a request SR22[4:0] (64/4 -> 0x10) *=*/
797 VGAOUT8(0x3c4, 0x22);
798 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
799 VGAOUT8(0x3c5, bRegTemp
| 0x10);
803 /*=* Max. length for a request SR22[4:0] (0x1F) *=*/
804 VGAOUT8(0x3c4, 0x22);
805 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
806 VGAOUT8(0x3c5, bRegTemp
| 0x1F);
814 static void VIAInitialize2DEngine(DRIDriverContext
*ctx
)
816 VIAPtr pVia
= VIAPTR(ctx
);
817 uint32_t dwVQStartAddr
, dwVQEndAddr
;
818 uint32_t dwVQLen
, dwVQStartL
, dwVQEndL
, dwVQStartEndH
;
821 /* init 2D engine regs to reset 2D engine */
822 VIASETREG(0x04, 0x0);
823 VIASETREG(0x08, 0x0);
824 VIASETREG(0x0c, 0x0);
825 VIASETREG(0x10, 0x0);
826 VIASETREG(0x14, 0x0);
827 VIASETREG(0x18, 0x0);
828 VIASETREG(0x1c, 0x0);
829 VIASETREG(0x20, 0x0);
830 VIASETREG(0x24, 0x0);
831 VIASETREG(0x28, 0x0);
832 VIASETREG(0x2c, 0x0);
833 VIASETREG(0x30, 0x0);
834 VIASETREG(0x34, 0x0);
835 VIASETREG(0x38, 0x0);
836 VIASETREG(0x3c, 0x0);
837 VIASETREG(0x40, 0x0);
841 /* Init AGP and VQ regs */
842 VIASETREG(0x43c, 0x00100000);
843 VIASETREG(0x440, 0x00000000);
844 VIASETREG(0x440, 0x00333004);
845 VIASETREG(0x440, 0x60000000);
846 VIASETREG(0x440, 0x61000000);
847 VIASETREG(0x440, 0x62000000);
848 VIASETREG(0x440, 0x63000000);
849 VIASETREG(0x440, 0x64000000);
850 VIASETREG(0x440, 0x7D000000);
852 VIASETREG(0x43c, 0xfe020000);
853 VIASETREG(0x440, 0x00000000);
855 if (pVia
->VQStart
!= 0) {
857 dwVQStartAddr
= pVia
->VQStart
;
858 dwVQEndAddr
= pVia
->VQEnd
;
859 dwVQStartL
= 0x50000000 | (dwVQStartAddr
& 0xFFFFFF);
860 dwVQEndL
= 0x51000000 | (dwVQEndAddr
& 0xFFFFFF);
861 dwVQStartEndH
= 0x52000000 | ((dwVQStartAddr
& 0xFF000000) >> 24) |
862 ((dwVQEndAddr
& 0xFF000000) >> 16);
863 dwVQLen
= 0x53000000 | (VIA_VQ_SIZE
>> 3);
865 VIASETREG(0x43c, 0x00fe0000);
866 VIASETREG(0x440, 0x080003fe);
867 VIASETREG(0x440, 0x0a00027c);
868 VIASETREG(0x440, 0x0b000260);
869 VIASETREG(0x440, 0x0c000274);
870 VIASETREG(0x440, 0x0d000264);
871 VIASETREG(0x440, 0x0e000000);
872 VIASETREG(0x440, 0x0f000020);
873 VIASETREG(0x440, 0x1000027e);
874 VIASETREG(0x440, 0x110002fe);
875 VIASETREG(0x440, 0x200f0060);
877 VIASETREG(0x440, 0x00000006);
878 VIASETREG(0x440, 0x40008c0f);
879 VIASETREG(0x440, 0x44000000);
880 VIASETREG(0x440, 0x45080c04);
881 VIASETREG(0x440, 0x46800408);
883 VIASETREG(0x440, dwVQStartEndH
);
884 VIASETREG(0x440, dwVQStartL
);
885 VIASETREG(0x440, dwVQEndL
);
886 VIASETREG(0x440, dwVQLen
);
890 VIASETREG(0x43c, 0x00fe0000);
891 VIASETREG(0x440, 0x00000004);
892 VIASETREG(0x440, 0x40008c0f);
893 VIASETREG(0x440, 0x44000000);
894 VIASETREG(0x440, 0x45080c04);
895 VIASETREG(0x440, 0x46800408);
902 dwGEMode
|= VIA_GEM_16bpp
;
905 dwGEMode
|= VIA_GEM_32bpp
;
907 dwGEMode
|= VIA_GEM_8bpp
;
912 switch (ctx
->shared
.virtualWidth
) {
914 dwGEMode
|= VIA_GEM_800
;
917 dwGEMode
|= VIA_GEM_1024
;
920 dwGEMode
|= VIA_GEM_1280
;
923 dwGEMode
|= VIA_GEM_1600
;
926 dwGEMode
|= VIA_GEM_2048
;
929 dwGEMode
|= VIA_GEM_640
;
936 /* Set BPP and Pitch */
937 VIASETREG(VIA_REG_GEMODE
, dwGEMode
);
939 /* Set Src and Dst base address and pitch, pitch is qword */
940 VIASETREG(VIA_REG_SRCBASE
, 0x0);
941 VIASETREG(VIA_REG_DSTBASE
, 0x0);
942 VIASETREG(VIA_REG_PITCH
, VIA_PITCH_ENABLE
|
943 ((ctx
->shared
.virtualWidth
* ctx
->bpp
>> 3) >> 3) |
944 (((ctx
->shared
.virtualWidth
* ctx
->bpp
>> 3) >> 3) << 16));
947 static int b3DRegsInitialized
= 0;
949 static void VIAInitialize3DEngine(DRIDriverContext
*ctx
)
951 VIAPtr pVia
= VIAPTR(ctx
);
954 if (!b3DRegsInitialized
)
957 VIASETREG(0x43C, 0x00010000);
959 for (i
= 0; i
<= 0x7D; i
++)
961 VIASETREG(0x440, (uint32_t) i
<< 24);
964 VIASETREG(0x43C, 0x00020000);
966 for (i
= 0; i
<= 0x94; i
++)
968 VIASETREG(0x440, (uint32_t) i
<< 24);
971 VIASETREG(0x440, 0x82400000);
973 VIASETREG(0x43C, 0x01020000);
976 for (i
= 0; i
<= 0x94; i
++)
978 VIASETREG(0x440, (uint32_t) i
<< 24);
981 VIASETREG(0x440, 0x82400000);
982 VIASETREG(0x43C, 0xfe020000);
984 for (i
= 0; i
<= 0x03; i
++)
986 VIASETREG(0x440, (uint32_t) i
<< 24);
989 VIASETREG(0x43C, 0x00030000);
991 for (i
= 0; i
<= 0xff; i
++)
995 VIASETREG(0x43C, 0x00100000);
996 VIASETREG(0x440, 0x00333004);
997 VIASETREG(0x440, 0x10000002);
998 VIASETREG(0x440, 0x60000000);
999 VIASETREG(0x440, 0x61000000);
1000 VIASETREG(0x440, 0x62000000);
1001 VIASETREG(0x440, 0x63000000);
1002 VIASETREG(0x440, 0x64000000);
1004 VIASETREG(0x43C, 0x00fe0000);
1006 if (pVia
->ChipRev
>= 3 )
1007 VIASETREG(0x440,0x40008c0f);
1009 VIASETREG(0x440,0x4000800f);
1011 VIASETREG(0x440,0x44000000);
1012 VIASETREG(0x440,0x45080C04);
1013 VIASETREG(0x440,0x46800408);
1014 VIASETREG(0x440,0x50000000);
1015 VIASETREG(0x440,0x51000000);
1016 VIASETREG(0x440,0x52000000);
1017 VIASETREG(0x440,0x53000000);
1019 b3DRegsInitialized
= 1;
1020 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
,
1021 "3D Engine has been initialized.\n");
1024 VIASETREG(0x43C,0x00fe0000);
1025 VIASETREG(0x440,0x08000001);
1026 VIASETREG(0x440,0x0A000183);
1027 VIASETREG(0x440,0x0B00019F);
1028 VIASETREG(0x440,0x0C00018B);
1029 VIASETREG(0x440,0x0D00019B);
1030 VIASETREG(0x440,0x0E000000);
1031 VIASETREG(0x440,0x0F000000);
1032 VIASETREG(0x440,0x10000000);
1033 VIASETREG(0x440,0x11000000);
1034 VIASETREG(0x440,0x20000000);
1038 WaitIdleCLE266(VIAPtr pVia
)
1044 while (!(VIAGETREG(VIA_REG_STATUS
) & VIA_VR_QUEUE_BUSY
) && (loop
++ < MAXLOOP
))
1047 while ((VIAGETREG(VIA_REG_STATUS
) &
1048 (VIA_CMD_RGTR_BUSY
| VIA_2D_ENG_BUSY
| VIA_3D_ENG_BUSY
)) &&
1052 return loop
>= MAXLOOP
;
1055 static int viaInitFBDev(DRIDriverContext
*ctx
)
1057 VIAPtr pVia
= CALLOC(sizeof(*pVia
));
1059 ctx
->driverPrivate
= (void *)pVia
;
1061 switch (ctx
->chipset
) {
1062 case PCI_CHIP_CLE3122
:
1063 case PCI_CHIP_CLE3022
:
1064 pVia
->Chipset
= VIA_CLE266
;
1066 case PCI_CHIP_VT7205
:
1067 case PCI_CHIP_VT3205
:
1068 pVia
->Chipset
= VIA_KM400
;
1070 case PCI_CHIP_VT3204
:
1071 pVia
->Chipset
= VIA_K8M800
;
1073 case PCI_CHIP_VT3259
:
1074 pVia
->Chipset
= VIA_PM800
;
1077 xf86DrvMsg(0, X_ERROR
, "VIA: Unknown device ID (0x%x)\n", ctx
->chipset
);
1080 /* _SOLO TODO XXX need to read ChipRev too */
1083 pVia
->videoRambytes
= ctx
->shared
.fbSize
;
1084 pVia
->MmioBase
= ctx
->MMIOStart
;
1085 pVia
->FrameBufferBase
= ctx
->FBStart
& 0xfc000000;
1087 pVia
->FBFreeStart
= ctx
->shared
.virtualWidth
* ctx
->cpp
*
1088 ctx
->shared
.virtualHeight
;
1089 pVia
->FBFreeEnd
= pVia
->videoRambytes
;
1091 if (!VIADRIScreenInit(ctx
))
1096 /* Get video memory clock. */
1097 VGAOUT8(0x3D4, 0x3D);
1098 pVia
->MemClk
= (VGAIN8(0x3D5) & 0xF0) >> 4;
1099 xf86DrvMsg(0, X_INFO
, "[dri] MemClk (0x%x)\n", pVia
->MemClk
);
1101 /* 3D rendering has noise if not enabled. */
1102 VIAEnableExtendedFIFO(ctx
);
1104 VIAInitialize2DEngine(ctx
);
1106 /* Must disable MMIO or 3D won't work. */
1107 VIADisableMMIO(ctx
);
1109 VIAInitialize3DEngine(ctx
);
1114 static void viaHaltFBDev(DRIDriverContext
*ctx
)
1116 drmUnmap( ctx
->pSAREA
, ctx
->shared
.SAREASize
);
1117 drmClose(ctx
->drmFD
);
1119 if (ctx
->driverPrivate
) {
1120 free(ctx
->driverPrivate
);
1121 ctx
->driverPrivate
= 0;
1125 static int viaEngineShutdown(const DRIDriverContext
*ctx
)
1130 static int viaEngineRestore(const DRIDriverContext
*ctx
)
1135 const struct DRIDriverRec __driDriver
=
1137 viaInitContextModes
,
1139 viaPostValidateMode
,