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 u_int8_t*)base)+(addr)))+0)
72 #define MMIO_OUT8(base, addr, val) ((*(((volatile u_int8_t*)base)+(addr)))=((u_int8_t)val))
73 #define MMIO_OUT16(base, addr, val) ((*(volatile u_int16_t*)(((u_int8_t*)base)+(addr)))=((u_int16_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 512
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 void VIADRIIrqInit( DRIDriverContext
*ctx
)
94 VIAPtr pVia
= VIAPTR(ctx
);
95 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
97 pVIADRI
->irqEnabled
= drmGetInterruptFromBusID(pVia
->drmFD
,
102 if ((drmCtlInstHandler(pVia
->drmFD
, pVIADRI
->irqEnabled
))) {
103 xf86DrvMsg(pScreen
->myNum
, X_WARNING
,
104 "[drm] Failure adding irq handler. "
105 "Falling back to irq-free operation.\n");
106 pVIADRI
->irqEnabled
= 0;
109 if (pVIADRI
->irqEnabled
)
110 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
111 "[drm] Irq handler installed, using IRQ %d.\n",
112 pVIADRI
->irqEnabled
);
115 static void VIADRIIrqExit( DRIDriverContext
*ctx
) {
116 VIAPtr pVia
= VIAPTR(ctx
);
117 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
119 if (pVIADRI
->irqEnabled
) {
120 if (drmCtlUninstHandler(pVia
->drmFD
)) {
121 xf86DrvMsg(pScreen
-myNum
, X_INFO
,"[drm] Irq handler uninstalled.\n");
123 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
124 "[drm] Could not uninstall irq handler.\n");
129 static void VIADRIRingBufferCleanup(DRIDriverContext
*ctx
)
131 VIAPtr pVia
= VIAPTR(ctx
);
132 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
133 drm_via_dma_init_t ringBufInit
;
135 if (pVIADRI
->ringBufActive
) {
136 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
137 "[drm] Cleaning up DMA ring-buffer.\n");
138 ringBufInit
.func
= VIA_CLEANUP_DMA
;
139 if (drmCommandWrite(pVia
->drmFD
, DRM_VIA_DMA_INIT
, &ringBufInit
,
140 sizeof(ringBufInit
))) {
141 xf86DrvMsg(pScreen
->myNum
, X_WARNING
,
142 "[drm] Failed to clean up DMA ring-buffer: %d\n", errno
);
144 pVIADRI
->ringBufActive
= 0;
148 static int VIADRIRingBufferInit(DRIDriverContext
*ctx
)
150 VIAPtr pVia
= VIAPTR(ctx
);
151 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
152 drm_via_dma_init_t ringBufInit
;
153 drmVersionPtr drmVer
;
155 pVIADRI
->ringBufActive
= 0;
157 if (NULL
== (drmVer
= drmGetVersion(pVia
->drmFD
))) {
161 if (((drmVer
->version_major
<= 1) && (drmVer
->version_minor
<= 3))) {
166 * Info frome code-snippet on DRI-DEVEL list; Erdi Chen.
169 switch (pVia
->ChipId
) {
170 case PCI_CHIP_VT3259
:
171 ringBufInit
.reg_pause_addr
= 0x40c;
174 ringBufInit
.reg_pause_addr
= 0x418;
178 ringBufInit
.offset
= pVia
->agpSize
;
179 ringBufInit
.size
= AGP_CMDBUF_SIZE
;
180 ringBufInit
.func
= VIA_INIT_DMA
;
181 if (drmCommandWrite(pVia
->drmFD
, DRM_VIA_DMA_INIT
, &ringBufInit
,
182 sizeof(ringBufInit
))) {
183 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
184 "[drm] Failed to initialize DMA ring-buffer: %d\n", errno
);
187 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
188 "[drm] Initialized AGP ring-buffer, size 0x%lx at AGP offset 0x%lx.\n",
189 ringBufInit
.size
, ringBufInit
.offset
);
191 pVIADRI
->ringBufActive
= 1;
195 static int VIADRIAgpInit(const DRIDriverContext
*ctx
, VIAPtr pVia
)
197 unsigned long agp_phys
;
198 unsigned int agpaddr
;
200 pVIADRI
= pVia
->devPrivate
;
203 if (drmAgpAcquire(pVia
->drmFD
) < 0) {
204 xf86DrvMsg(pScreen
->myNum
, X_ERROR
, "[drm] drmAgpAcquire failed %d\n", errno
);
208 if (drmAgpEnable(pVia
->drmFD
, drmAgpGetMode(pVia
->drmFD
)&~0x0) < 0) {
209 xf86DrvMsg(pScreen
->myNum
, X_ERROR
, "[drm] drmAgpEnable failed\n");
213 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] drmAgpEnabled succeeded\n");
215 if (drmAgpAlloc(pVia
->drmFD
, AGP_SIZE
, 0, &agp_phys
, &pVia
->agpHandle
) < 0) {
216 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
217 "[drm] drmAgpAlloc failed\n");
218 drmAgpRelease(pVia
->drmFD
);
222 if (drmAgpBind(pVia
->drmFD
, pVia
->agpHandle
, 0) < 0) {
223 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
224 "[drm] drmAgpBind failed\n");
225 drmAgpFree(pVia
->drmFD
, pVia
->agpHandle
);
226 drmAgpRelease(pVia
->drmFD
);
232 * Place the ring-buffer last in the AGP region, and restrict the
233 * public map not to include the buffer for security reasons.
236 pVia
->agpSize
= AGP_SIZE
- AGP_CMDBUF_SIZE
;
237 pVia
->agpAddr
= drmAgpBase(pVia
->drmFD
);
238 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
239 "[drm] agpAddr = 0x%08lx\n",pVia
->agpAddr
);
241 pVIADRI
->agp
.size
= pVia
->agpSize
;
242 if (drmAddMap(pVia
->drmFD
, (drm_handle_t
)0,
243 pVIADRI
->agp
.size
, DRM_AGP
, 0,
244 &pVIADRI
->agp
.handle
) < 0) {
245 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
246 "[drm] Failed to map public agp area\n");
247 pVIADRI
->agp
.size
= 0;
250 /* Map AGP from kernel to Xserver - Not really needed */
251 drmMap(pVia
->drmFD
, pVIADRI
->agp
.handle
,pVIADRI
->agp
.size
,
252 (drmAddressPtr
)&agpaddr
);
255 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
256 "[drm] agpBase = 0x%08lx\n", pVia
->agpBase
);
257 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
258 "[drm] agpAddr = 0x%08lx\n", pVia
->agpAddr
);
260 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
261 "[drm] agpSize = 0x%08lx\n", pVia
->agpSize
);
262 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
263 "[drm] agp physical addr = 0x%08lx\n", agp_phys
);
268 agp
.size
= AGP_SIZE
-AGP_CMDBUF_SIZE
;
269 if (drmCommandWrite(pVia
->drmFD
, DRM_VIA_AGP_INIT
, &agp
,
270 sizeof(drm_via_agp_t
)) < 0) {
271 drmUnmap(&agpaddr
,pVia
->agpSize
);
272 drmRmMap(pVia
->drmFD
,pVIADRI
->agp
.handle
);
273 drmAgpUnbind(pVia
->drmFD
, pVia
->agpHandle
);
274 drmAgpFree(pVia
->drmFD
, pVia
->agpHandle
);
275 drmAgpRelease(pVia
->drmFD
);
283 static int VIADRIFBInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
285 int FBSize
= pVia
->FBFreeEnd
-pVia
->FBFreeStart
;
286 int FBOffset
= pVia
->FBFreeStart
;
287 VIADRIPtr pVIADRI
= pVia
->devPrivate
;
288 pVIADRI
->fbOffset
= FBOffset
;
289 pVIADRI
->fbSize
= pVia
->videoRambytes
;
293 fb
.offset
= FBOffset
;
296 if (drmCommandWrite(pVia
->drmFD
, DRM_VIA_FB_INIT
, &fb
,
297 sizeof(drm_via_fb_t
)) < 0) {
298 xf86DrvMsg(pScreen
->myNum
, X_ERROR
,
299 "[drm] failed to init frame buffer area\n");
302 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
303 "[drm] FBFreeStart= 0x%08x FBFreeEnd= 0x%08x "
305 pVia
->FBFreeStart
, pVia
->FBFreeEnd
, FBSize
);
311 static int VIADRIPciInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
316 static int VIADRIScreenInit(DRIDriverContext
* ctx
)
318 VIAPtr pVia
= VIAPTR(ctx
);
323 ctx
->shared
.SAREASize
= ((sizeof(drm_sarea_t
) + 0xfff) & 0x1000);
325 if (sizeof(drm_sarea_t
)+sizeof(VIASAREAPriv
) > SAREA_MAX
) {
326 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
327 "Data does not fit in SAREA\n");
330 ctx
->shared
.SAREASize
= SAREA_MAX
;
333 ctx
->drmFD
= drmOpen(VIAKernelDriverName
, NULL
);
334 if (ctx
->drmFD
< 0) {
335 fprintf(stderr
, "[drm] drmOpen failed\n");
338 pVia
->drmFD
= ctx
->drmFD
;
340 err
= drmSetBusid(ctx
->drmFD
, ctx
->pciBusID
);
342 fprintf(stderr
, "[drm] drmSetBusid failed (%d, %s), %s\n",
343 ctx
->drmFD
, ctx
->pciBusID
, strerror(-err
));
347 err
= drmAddMap(ctx
->drmFD
, 0, ctx
->shared
.SAREASize
, DRM_SHM
,
348 DRM_CONTAINS_LOCK
, &ctx
->shared
.hSAREA
);
350 fprintf(stderr
, "[drm] drmAddMap failed\n");
353 fprintf(stderr
, "[drm] added %d byte SAREA at 0x%08lx\n",
354 ctx
->shared
.SAREASize
, ctx
->shared
.hSAREA
);
356 if (drmMap(ctx
->drmFD
,
358 ctx
->shared
.SAREASize
,
359 (drmAddressPtr
)(&ctx
->pSAREA
)) < 0)
361 fprintf(stderr
, "[drm] drmMap failed\n");
364 memset(ctx
->pSAREA
, 0, ctx
->shared
.SAREASize
);
365 fprintf(stderr
, "[drm] mapped SAREA 0x%08lx to %p, size %d\n",
366 ctx
->shared
.hSAREA
, ctx
->pSAREA
, ctx
->shared
.SAREASize
);
368 /* Need to AddMap the framebuffer and mmio regions here:
370 if (drmAddMap(ctx
->drmFD
,
371 (drm_handle_t
)ctx
->FBStart
,
379 &ctx
->shared
.hFrameBuffer
) < 0)
381 fprintf(stderr
, "[drm] drmAddMap framebuffer failed\n");
385 fprintf(stderr
, "[drm] framebuffer handle = 0x%08lx\n",
386 ctx
->shared
.hFrameBuffer
);
388 pVIADRI
= (VIADRIPtr
) CALLOC(sizeof(VIADRIRec
));
390 drmClose(ctx
->drmFD
);
393 pVia
->devPrivate
= pVIADRI
;
394 ctx
->driverClientMsg
= pVIADRI
;
395 ctx
->driverClientMsgSize
= sizeof(*pVIADRI
);
397 pVia
->IsPCI
= !VIADRIAgpInit(ctx
, pVia
);
400 VIADRIPciInit(ctx
, pVia
);
401 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] use pci.\n" );
404 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] use agp.\n" );
406 if (!(VIADRIFBInit(ctx
, pVia
))) {
407 VIADRICloseScreen(ctx
);
408 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "[dri] frame buffer initialize fail .\n" );
412 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] frame buffer initialized.\n" );
414 /* DRIScreenInit doesn't add all the common mappings. Add additional mappings here. */
415 if (!VIADRIMapInit(ctx
, pVia
)) {
416 VIADRICloseScreen(ctx
);
419 pVIADRI
->regs
.size
= VIA_MMIO_REGSIZE
;
420 pVIADRI
->regs
.map
= 0;
421 pVIADRI
->regs
.handle
= pVia
->registerHandle
;
422 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] mmio Registers = 0x%08lx\n",
423 pVIADRI
->regs
.handle
);
425 /*pVIADRI->drixinerama = pVia->drixinerama;*/
426 /*=* John Sheng [2003.12.9] Tuxracer & VQ *=*/
427 pVIADRI
->VQEnable
= pVia
->VQEnable
;
429 if (drmMap(pVia
->drmFD
,
430 pVIADRI
->regs
.handle
,
432 (drmAddress
*)&pVia
->MapBase
) != 0)
434 VIADRICloseScreen(ctx
);
438 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "[dri] mmio mapped.\n" );
440 return VIADRIFinishScreenInit(ctx
);
444 VIADRICloseScreen(DRIDriverContext
* ctx
)
446 VIAPtr pVia
= VIAPTR(ctx
);
447 VIADRIPtr pVIADRI
=(VIADRIPtr
)pVia
->devPrivate
;
449 VIADRIRingBufferCleanup(ctx
);
452 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Unmapping MMIO registers\n");
453 drmUnmap(pVia
->MapBase
, pVIADRI
->regs
.size
);
457 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Freeing agp memory\n");
458 drmAgpFree(pVia
->drmFD
, pVia
->agpHandle
);
459 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[drm] Releasing agp module\n");
460 drmAgpRelease(pVia
->drmFD
);
464 if (pVia
->DRIIrqEnable
)
470 VIADRIFinishScreenInit(DRIDriverContext
* ctx
)
472 VIAPtr pVia
= VIAPTR(ctx
);
476 err
= drmCreateContext(ctx
->drmFD
, &ctx
->serverContext
);
478 fprintf(stderr
, "%s: drmCreateContext failed %d\n", __FUNCTION__
, err
);
482 DRM_LOCK(ctx
->drmFD
, ctx
->pSAREA
, ctx
->serverContext
, 0);
485 if (!VIADRIKernelInit(ctx
, pVia
)) {
486 VIADRICloseScreen(ctx
);
489 xf86DrvMsg(pScreen
->myNum
, X_INFO
, "[dri] kernel data initialized.\n");
491 /* set SAREA value */
493 VIASAREAPriv
*saPriv
;
495 saPriv
=(VIASAREAPriv
*)(((char*)ctx
->pSAREA
) +
496 sizeof(drm_sarea_t
));
498 memset(saPriv
, 0, sizeof(*saPriv
));
499 saPriv
->CtxOwner
= -1;
501 pVIADRI
=(VIADRIPtr
)pVia
->devPrivate
;
502 pVIADRI
->deviceID
=pVia
->Chipset
;
503 pVIADRI
->width
=ctx
->shared
.virtualWidth
;
504 pVIADRI
->height
=ctx
->shared
.virtualHeight
;
505 pVIADRI
->mem
=ctx
->shared
.fbSize
;
506 pVIADRI
->bytesPerPixel
= (ctx
->bpp
+7) / 8;
507 pVIADRI
->sarea_priv_offset
= sizeof(drm_sarea_t
);
509 pVIADRI
->scrnX
=pVIADRI
->width
;
510 pVIADRI
->scrnY
=pVIADRI
->height
;
514 if (pVia
->DRIIrqEnable
)
518 pVIADRI
->ringBufActive
= 0;
519 VIADRIRingBufferInit(ctx
);
524 /* Initialize the kernel data structures. */
525 static int VIADRIKernelInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
527 drm_via_init_t drmInfo
;
528 memset(&drmInfo
, 0, sizeof(drm_via_init_t
));
529 drmInfo
.sarea_priv_offset
= sizeof(drm_sarea_t
);
530 drmInfo
.func
= VIA_INIT_MAP
;
531 drmInfo
.fb_offset
= pVia
->FrameBufferBase
;
532 drmInfo
.mmio_offset
= pVia
->registerHandle
;
534 drmInfo
.agpAddr
= (u_int32_t
)NULL
;
536 drmInfo
.agpAddr
= (u_int32_t
)pVia
->agpAddr
;
538 if ((drmCommandWrite(pVia
->drmFD
, DRM_VIA_MAP_INIT
,&drmInfo
,
539 sizeof(drm_via_init_t
))) < 0)
544 /* Add a map for the MMIO registers */
545 static int VIADRIMapInit(DRIDriverContext
* ctx
, VIAPtr pVia
)
549 if (drmAddMap(pVia
->drmFD
, pVia
->MmioBase
, VIA_MMIO_REGSIZE
,
550 DRM_REGISTERS
, flags
, &pVia
->registerHandle
) < 0) {
554 xf86DrvMsg(pScreen
->myNum
, X_INFO
,
555 "[drm] register handle = 0x%08lx\n", pVia
->registerHandle
);
560 static int viaValidateMode(const DRIDriverContext
*ctx
)
562 VIAPtr pVia
= VIAPTR(ctx
);
567 static int viaPostValidateMode(const DRIDriverContext
*ctx
)
569 VIAPtr pVia
= VIAPTR(ctx
);
574 static void VIAEnableMMIO(DRIDriverContext
* ctx
)
576 /*vgaHWPtr hwp = VGAHWPTR(ctx);*/
577 VIAPtr pVia
= VIAPTR(ctx
);
581 if (xf86IsPrimaryPci(pVia
->PciInfo
)) {
582 /* If we are primary card, we still use std vga port. If we use
583 * MMIO, system will hang in vgaHWSave when our card used in
584 * PLE and KLE (integrated Trident MVP4)
586 vgaHWSetStdFuncs(hwp
);
589 vgaHWSetMmioFuncs(hwp
, pVia
->MapBase
, 0x8000);
594 VGAOUT8(0x3c3, val
| 0x01);
596 VGAOUT8(0x3c2, val
| 0x01);
598 /* Unlock Extended IO Space */
599 VGAOUT8(0x3c4, 0x10);
600 VGAOUT8(0x3c5, 0x01);
603 if(!pVia
->IsSecondary
) {
604 VGAOUT8(0x3c4, 0x1a);
607 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "primary val = %x\n", val
);
609 VGAOUT8(0x3c5, val
| 0x68);
612 VGAOUT8(0x3c4, 0x1a);
615 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "secondary val = %x\n", val
);
617 VGAOUT8(0x3c5, val
| 0x38);
620 /* Unlock CRTC registers */
621 VGAOUT8(0x3d4, 0x47);
622 VGAOUT8(0x3d5, 0x00);
627 static void VIADisableMMIO(DRIDriverContext
* ctx
)
629 VIAPtr pVia
= VIAPTR(ctx
);
632 VGAOUT8(0x3c4, 0x1a);
634 VGAOUT8(0x3c5, val
& 0x97);
639 static void VIADisableExtendedFIFO(DRIDriverContext
*ctx
)
641 VIAPtr pVia
= VIAPTR(ctx
);
642 u_int32_t dwGE230
, dwGE298
;
644 /* Cause of exit XWindow will dump back register value, others chipset no
645 * need to set extended fifo value */
646 if (pVia
->Chipset
== VIA_CLE266
&& pVia
->ChipRev
< 15 &&
647 (ctx
->shared
.virtualWidth
> 1024 || pVia
->HasSecondary
)) {
648 /* Turn off Extend FIFO */
650 dwGE298
= VIAGETREG(0x298);
651 VIASETREG(0x298, dwGE298
| 0x20000000);
653 dwGE230
= VIAGETREG(0x230);
654 VIASETREG(0x230, dwGE230
& ~0x00200000);
656 dwGE298
= VIAGETREG(0x298);
657 VIASETREG(0x298, dwGE298
& ~0x20000000);
661 static void VIAEnableExtendedFIFO(DRIDriverContext
*ctx
)
663 VIAPtr pVia
= VIAPTR(ctx
);
665 u_int32_t dwGE230
, dwGE298
;
667 switch (pVia
->Chipset
) {
669 if (pVia
->ChipRev
> 14) { /* For 3123Cx */
670 if (pVia
->HasSecondary
) { /* SAMM or DuoView case */
671 if (ctx
->shared
.virtualWidth
>= 1024)
674 VGAOUT8(0x3C4, 0x16);
675 bRegTemp
= VGAIN8(0x3C5);
678 VGAOUT8(0x3C5, bRegTemp
);
680 VGAOUT8(0x3C4, 0x17);
681 bRegTemp
= VGAIN8(0x3C5);
684 VGAOUT8(0x3C5, bRegTemp
);
685 pVia
->EnableExtendedFIFO
= GL_TRUE
;
688 else /* Single view or Simultaneoue case */
690 if (ctx
->shared
.virtualWidth
> 1024)
693 VGAOUT8(0x3C4, 0x16);
694 bRegTemp
= VGAIN8(0x3C5);
697 VGAOUT8(0x3C5, bRegTemp
);
699 VGAOUT8(0x3C4, 0x17);
700 bRegTemp
= VGAIN8(0x3C5);
703 VGAOUT8(0x3C5, bRegTemp
);
704 pVia
->EnableExtendedFIFO
= GL_TRUE
;
708 VGAOUT8(0x3C4, 0x18);
709 bRegTemp
= VGAIN8(0x3C5);
712 bRegTemp
|= 0x40; /* force the preq always higher than treq */
713 VGAOUT8(0x3C5, bRegTemp
);
715 else { /* for 3123Ax */
716 if (ctx
->shared
.virtualWidth
> 1024 || pVia
->HasSecondary
) {
717 /* Turn on Extend FIFO */
719 dwGE298
= VIAGETREG(0x298);
720 VIASETREG(0x298, dwGE298
| 0x20000000);
722 dwGE230
= VIAGETREG(0x230);
723 VIASETREG(0x230, dwGE230
| 0x00200000);
725 dwGE298
= VIAGETREG(0x298);
726 VIASETREG(0x298, dwGE298
& ~0x20000000);
729 VGAOUT8(0x3C4, 0x16);
730 bRegTemp
= VGAIN8(0x3C5);
733 /* bRegTemp |= 0x10; */
734 VGAOUT8(0x3C5, bRegTemp
);
736 VGAOUT8(0x3C4, 0x17);
737 bRegTemp
= VGAIN8(0x3C5);
740 /*bRegTemp |= 0x1F;*/
741 VGAOUT8(0x3C5, bRegTemp
);
743 VGAOUT8(0x3C4, 0x18);
744 bRegTemp
= VGAIN8(0x3C5);
747 bRegTemp
|= 0x40; /* force the preq always higher than treq */
748 VGAOUT8(0x3C5, bRegTemp
);
749 pVia
->EnableExtendedFIFO
= GL_TRUE
;
754 if (pVia
->HasSecondary
) { /* SAMM or DuoView case */
755 if ((ctx
->shared
.virtualWidth
>= 1600) &&
756 (pVia
->MemClk
<= VIA_MEM_DDR200
)) {
757 /* enable CRT extendded FIFO */
758 VGAOUT8(0x3C4, 0x17);
759 VGAOUT8(0x3C5, 0x1C);
760 /* revise second display queue depth and read threshold */
761 VGAOUT8(0x3C4, 0x16);
762 bRegTemp
= VGAIN8(0x3C5);
764 bRegTemp
= (bRegTemp
) | (0x09);
765 VGAOUT8(0x3C5, bRegTemp
);
768 /* enable CRT extendded FIFO */
769 VGAOUT8(0x3C4, 0x17);
771 /* revise second display queue depth and read threshold */
772 VGAOUT8(0x3C4, 0x16);
773 bRegTemp
= VGAIN8(0x3C5);
775 bRegTemp
= (bRegTemp
) | (0x1C);
776 VGAOUT8(0x3C5, bRegTemp
);
779 VGAOUT8(0x3C4, 0x18);
780 bRegTemp
= VGAIN8(0x3C5);
783 bRegTemp
|= 0x40; /* force the preq always higher than treq */
784 VGAOUT8(0x3C5, bRegTemp
);
785 pVia
->EnableExtendedFIFO
= GL_TRUE
;
788 if ( (ctx
->shared
.virtualWidth
> 1024) && (ctx
->shared
.virtualWidth
<= 1280) )
790 /* enable CRT extendded FIFO */
791 VGAOUT8(0x3C4, 0x17);
792 VGAOUT8(0x3C5, 0x3F);
793 /* revise second display queue depth and read threshold */
794 VGAOUT8(0x3C4, 0x16);
795 bRegTemp
= VGAIN8(0x3C5);
797 bRegTemp
= (bRegTemp
) | (0x17);
798 VGAOUT8(0x3C5, bRegTemp
);
799 pVia
->EnableExtendedFIFO
= GL_TRUE
;
801 else if ((ctx
->shared
.virtualWidth
> 1280))
803 /* enable CRT extendded FIFO */
804 VGAOUT8(0x3C4, 0x17);
805 VGAOUT8(0x3C5, 0x3F);
806 /* revise second display queue depth and read threshold */
807 VGAOUT8(0x3C4, 0x16);
808 bRegTemp
= VGAIN8(0x3C5);
810 bRegTemp
= (bRegTemp
) | (0x1C);
811 VGAOUT8(0x3C5, bRegTemp
);
812 pVia
->EnableExtendedFIFO
= GL_TRUE
;
816 /* enable CRT extendded FIFO */
817 VGAOUT8(0x3C4, 0x17);
818 VGAOUT8(0x3C5, 0x3F);
819 /* revise second display queue depth and read threshold */
820 VGAOUT8(0x3C4, 0x16);
821 bRegTemp
= VGAIN8(0x3C5);
823 bRegTemp
= (bRegTemp
) | (0x10);
824 VGAOUT8(0x3C5, bRegTemp
);
827 VGAOUT8(0x3C4, 0x18);
828 bRegTemp
= VGAIN8(0x3C5);
831 bRegTemp
|= 0x40; /* force the preq always higher than treq */
832 VGAOUT8(0x3C5, bRegTemp
);
836 /*=* R1 Display FIFO depth (384 /8 -1 -> 0xbf) SR17[7:0] (8bits) *=*/
837 VGAOUT8(0x3c4, 0x17);
838 VGAOUT8(0x3c5, 0xbf);
840 /*=* R2 Display fetch datum threshold value (328/4 -> 0x52)
841 SR16[5:0], SR16[7] (7bits) *=*/
842 VGAOUT8(0x3c4, 0x16);
843 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
844 bRegTemp
|= (0x52 & 0x3F);
845 bRegTemp
|= ((0x52 & 0x40) << 1);
846 VGAOUT8(0x3c5, bRegTemp
);
848 /*=* R3 Switch to the highest agent threshold value (74 -> 0x4a)
849 SR18[5:0], SR18[7] (7bits) *=*/
850 VGAOUT8(0x3c4, 0x18);
851 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
852 bRegTemp
|= (0x4a & 0x3F);
853 bRegTemp
|= ((0x4a & 0x40) << 1);
854 VGAOUT8(0x3c5, bRegTemp
);
856 /*=* R4 Fetch Number for a scan line (unit: 8 bytes)
857 SR1C[7:0], SR1D[1:0] (10bits) *=*/
858 wRegTemp
= (pBIOSInfo
->offsetWidthByQWord
>> 1) + 4;
859 VGAOUT8(0x3c4, 0x1c);
860 VGAOUT8(0x3c5, (u_int8_t
)(wRegTemp
& 0xFF));
861 VGAOUT8(0x3c4, 0x1d);
862 bRegTemp
= VGAIN8(0x3c5) & ~0x03;
863 VGAOUT8(0x3c5, bRegTemp
| ((wRegTemp
& 0x300) >> 8));
865 if (ctx
->shared
.virtualWidth
>= 1400 && ctx
->bpp
== 32)
867 /*=* Max. length for a request SR22[4:0] (64/4 -> 0x10) *=*/
868 VGAOUT8(0x3c4, 0x22);
869 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
870 VGAOUT8(0x3c5, bRegTemp
| 0x10);
874 /*=* Max. length for a request SR22[4:0]
875 (128/4 -> over flow 0x0) *=*/
876 VGAOUT8(0x3c4, 0x22);
877 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
878 VGAOUT8(0x3c5, bRegTemp
);
882 /*=* R1 Display FIFO depth (96-1 -> 0x5f) SR17[7:0] (8bits) *=*/
883 VGAOUT8(0x3c4, 0x17);
884 VGAOUT8(0x3c5, 0x5f);
886 /*=* R2 Display fetch datum threshold value (32 -> 0x20)
887 SR16[5:0], SR16[7] (7bits) *=*/
888 VGAOUT8(0x3c4, 0x16);
889 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
890 bRegTemp
|= (0x20 & 0x3F);
891 bRegTemp
|= ((0x20 & 0x40) << 1);
892 VGAOUT8(0x3c5, bRegTemp
);
894 /*=* R3 Switch to the highest agent threshold value (16 -> 0x10)
895 SR18[5:0], SR18[7] (7bits) *=*/
896 VGAOUT8(0x3c4, 0x18);
897 bRegTemp
= VGAIN8(0x3c5) & ~0xBF;
898 bRegTemp
|= (0x10 & 0x3F);
899 bRegTemp
|= ((0x10 & 0x40) << 1);
900 VGAOUT8(0x3c5, bRegTemp
);
902 /*=* R4 Fetch Number for a scan line (unit: 8 bytes)
903 SR1C[7:0], SR1D[1:0] (10bits) *=*/
904 wRegTemp
= (pBIOSInfo
->offsetWidthByQWord
>> 1) + 4;
905 VGAOUT8(0x3c4, 0x1c);
906 VGAOUT8(0x3c5, (u_int8_t
)(wRegTemp
& 0xFF));
907 VGAOUT8(0x3c4, 0x1d);
908 bRegTemp
= VGAIN8(0x3c5) & ~0x03;
909 VGAOUT8(0x3c5, bRegTemp
| ((wRegTemp
& 0x300) >> 8));
911 if (ctx
->shared
.virtualWidth
>= 1400 && ctx
->bpp
== 32)
913 /*=* Max. length for a request SR22[4:0] (64/4 -> 0x10) *=*/
914 VGAOUT8(0x3c4, 0x22);
915 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
916 VGAOUT8(0x3c5, bRegTemp
| 0x10);
920 /*=* Max. length for a request SR22[4:0] (0x1F) *=*/
921 VGAOUT8(0x3c4, 0x22);
922 bRegTemp
= VGAIN8(0x3c5) & ~0x1F;
923 VGAOUT8(0x3c5, bRegTemp
| 0x1F);
931 static void VIAInitialize2DEngine(DRIDriverContext
*ctx
)
933 VIAPtr pVia
= VIAPTR(ctx
);
934 u_int32_t dwVQStartAddr
, dwVQEndAddr
;
935 u_int32_t dwVQLen
, dwVQStartL
, dwVQEndL
, dwVQStartEndH
;
938 /* init 2D engine regs to reset 2D engine */
939 VIASETREG(0x04, 0x0);
940 VIASETREG(0x08, 0x0);
941 VIASETREG(0x0c, 0x0);
942 VIASETREG(0x10, 0x0);
943 VIASETREG(0x14, 0x0);
944 VIASETREG(0x18, 0x0);
945 VIASETREG(0x1c, 0x0);
946 VIASETREG(0x20, 0x0);
947 VIASETREG(0x24, 0x0);
948 VIASETREG(0x28, 0x0);
949 VIASETREG(0x2c, 0x0);
950 VIASETREG(0x30, 0x0);
951 VIASETREG(0x34, 0x0);
952 VIASETREG(0x38, 0x0);
953 VIASETREG(0x3c, 0x0);
954 VIASETREG(0x40, 0x0);
958 /* Init AGP and VQ regs */
959 VIASETREG(0x43c, 0x00100000);
960 VIASETREG(0x440, 0x00000000);
961 VIASETREG(0x440, 0x00333004);
962 VIASETREG(0x440, 0x60000000);
963 VIASETREG(0x440, 0x61000000);
964 VIASETREG(0x440, 0x62000000);
965 VIASETREG(0x440, 0x63000000);
966 VIASETREG(0x440, 0x64000000);
967 VIASETREG(0x440, 0x7D000000);
969 VIASETREG(0x43c, 0xfe020000);
970 VIASETREG(0x440, 0x00000000);
972 if (pVia
->VQStart
!= 0) {
974 dwVQStartAddr
= pVia
->VQStart
;
975 dwVQEndAddr
= pVia
->VQEnd
;
976 dwVQStartL
= 0x50000000 | (dwVQStartAddr
& 0xFFFFFF);
977 dwVQEndL
= 0x51000000 | (dwVQEndAddr
& 0xFFFFFF);
978 dwVQStartEndH
= 0x52000000 | ((dwVQStartAddr
& 0xFF000000) >> 24) |
979 ((dwVQEndAddr
& 0xFF000000) >> 16);
980 dwVQLen
= 0x53000000 | (VIA_VQ_SIZE
>> 3);
982 VIASETREG(0x43c, 0x00fe0000);
983 VIASETREG(0x440, 0x080003fe);
984 VIASETREG(0x440, 0x0a00027c);
985 VIASETREG(0x440, 0x0b000260);
986 VIASETREG(0x440, 0x0c000274);
987 VIASETREG(0x440, 0x0d000264);
988 VIASETREG(0x440, 0x0e000000);
989 VIASETREG(0x440, 0x0f000020);
990 VIASETREG(0x440, 0x1000027e);
991 VIASETREG(0x440, 0x110002fe);
992 VIASETREG(0x440, 0x200f0060);
994 VIASETREG(0x440, 0x00000006);
995 VIASETREG(0x440, 0x40008c0f);
996 VIASETREG(0x440, 0x44000000);
997 VIASETREG(0x440, 0x45080c04);
998 VIASETREG(0x440, 0x46800408);
1000 VIASETREG(0x440, dwVQStartEndH
);
1001 VIASETREG(0x440, dwVQStartL
);
1002 VIASETREG(0x440, dwVQEndL
);
1003 VIASETREG(0x440, dwVQLen
);
1007 VIASETREG(0x43c, 0x00fe0000);
1008 VIASETREG(0x440, 0x00000004);
1009 VIASETREG(0x440, 0x40008c0f);
1010 VIASETREG(0x440, 0x44000000);
1011 VIASETREG(0x440, 0x45080c04);
1012 VIASETREG(0x440, 0x46800408);
1019 dwGEMode
|= VIA_GEM_16bpp
;
1022 dwGEMode
|= VIA_GEM_32bpp
;
1024 dwGEMode
|= VIA_GEM_8bpp
;
1029 switch (ctx
->shared
.virtualWidth
) {
1031 dwGEMode
|= VIA_GEM_800
;
1034 dwGEMode
|= VIA_GEM_1024
;
1037 dwGEMode
|= VIA_GEM_1280
;
1040 dwGEMode
|= VIA_GEM_1600
;
1043 dwGEMode
|= VIA_GEM_2048
;
1046 dwGEMode
|= VIA_GEM_640
;
1053 /* Set BPP and Pitch */
1054 VIASETREG(VIA_REG_GEMODE
, dwGEMode
);
1056 /* Set Src and Dst base address and pitch, pitch is qword */
1057 VIASETREG(VIA_REG_SRCBASE
, 0x0);
1058 VIASETREG(VIA_REG_DSTBASE
, 0x0);
1059 VIASETREG(VIA_REG_PITCH
, VIA_PITCH_ENABLE
|
1060 ((ctx
->shared
.virtualWidth
* ctx
->bpp
>> 3) >> 3) |
1061 (((ctx
->shared
.virtualWidth
* ctx
->bpp
>> 3) >> 3) << 16));
1064 static int b3DRegsInitialized
= 0;
1066 static void VIAInitialize3DEngine(DRIDriverContext
*ctx
)
1068 VIAPtr pVia
= VIAPTR(ctx
);
1071 if (!b3DRegsInitialized
)
1074 VIASETREG(0x43C, 0x00010000);
1076 for (i
= 0; i
<= 0x7D; i
++)
1078 VIASETREG(0x440, (u_int32_t
) i
<< 24);
1081 VIASETREG(0x43C, 0x00020000);
1083 for (i
= 0; i
<= 0x94; i
++)
1085 VIASETREG(0x440, (u_int32_t
) i
<< 24);
1088 VIASETREG(0x440, 0x82400000);
1090 VIASETREG(0x43C, 0x01020000);
1093 for (i
= 0; i
<= 0x94; i
++)
1095 VIASETREG(0x440, (u_int32_t
) i
<< 24);
1098 VIASETREG(0x440, 0x82400000);
1099 VIASETREG(0x43C, 0xfe020000);
1101 for (i
= 0; i
<= 0x03; i
++)
1103 VIASETREG(0x440, (u_int32_t
) i
<< 24);
1106 VIASETREG(0x43C, 0x00030000);
1108 for (i
= 0; i
<= 0xff; i
++)
1110 VIASETREG(0x440, 0);
1112 VIASETREG(0x43C, 0x00100000);
1113 VIASETREG(0x440, 0x00333004);
1114 VIASETREG(0x440, 0x10000002);
1115 VIASETREG(0x440, 0x60000000);
1116 VIASETREG(0x440, 0x61000000);
1117 VIASETREG(0x440, 0x62000000);
1118 VIASETREG(0x440, 0x63000000);
1119 VIASETREG(0x440, 0x64000000);
1121 VIASETREG(0x43C, 0x00fe0000);
1123 if (pVia
->ChipRev
>= 3 )
1124 VIASETREG(0x440,0x40008c0f);
1126 VIASETREG(0x440,0x4000800f);
1128 VIASETREG(0x440,0x44000000);
1129 VIASETREG(0x440,0x45080C04);
1130 VIASETREG(0x440,0x46800408);
1131 VIASETREG(0x440,0x50000000);
1132 VIASETREG(0x440,0x51000000);
1133 VIASETREG(0x440,0x52000000);
1134 VIASETREG(0x440,0x53000000);
1136 b3DRegsInitialized
= 1;
1137 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
,
1138 "3D Engine has been initialized.\n");
1141 VIASETREG(0x43C,0x00fe0000);
1142 VIASETREG(0x440,0x08000001);
1143 VIASETREG(0x440,0x0A000183);
1144 VIASETREG(0x440,0x0B00019F);
1145 VIASETREG(0x440,0x0C00018B);
1146 VIASETREG(0x440,0x0D00019B);
1147 VIASETREG(0x440,0x0E000000);
1148 VIASETREG(0x440,0x0F000000);
1149 VIASETREG(0x440,0x10000000);
1150 VIASETREG(0x440,0x11000000);
1151 VIASETREG(0x440,0x20000000);
1155 WaitIdleCLE266(VIAPtr pVia
)
1161 while (!(VIAGETREG(VIA_REG_STATUS
) & VIA_VR_QUEUE_BUSY
) && (loop
++ < MAXLOOP
))
1164 while ((VIAGETREG(VIA_REG_STATUS
) &
1165 (VIA_CMD_RGTR_BUSY
| VIA_2D_ENG_BUSY
| VIA_3D_ENG_BUSY
)) &&
1169 return loop
>= MAXLOOP
;
1172 static int viaInitFBDev(DRIDriverContext
*ctx
)
1174 VIAPtr pVia
= CALLOC(sizeof(*pVia
));
1176 ctx
->driverPrivate
= (void *)pVia
;
1178 switch (ctx
->chipset
) {
1179 case PCI_CHIP_CLE3122
:
1180 case PCI_CHIP_CLE3022
:
1181 pVia
->Chipset
= VIA_CLE266
;
1183 case PCI_CHIP_VT7205
:
1184 case PCI_CHIP_VT3205
:
1185 pVia
->Chipset
= VIA_KM400
;
1187 case PCI_CHIP_VT3204
:
1188 pVia
->Chipset
= VIA_K8M800
;
1190 case PCI_CHIP_VT3259
:
1191 pVia
->Chipset
= VIA_PM800
;
1194 xf86DrvMsg(0, X_ERROR
, "VIA: Unknown device ID (0x%x)\n", ctx
->chipset
);
1197 /* _SOLO TODO XXX need to read ChipRev too */
1200 pVia
->videoRambytes
= ctx
->shared
.fbSize
;
1201 pVia
->MmioBase
= ctx
->MMIOStart
;
1202 pVia
->FrameBufferBase
= ctx
->FBStart
& 0xfc000000;
1204 pVia
->FBFreeStart
= ctx
->shared
.virtualWidth
* ctx
->cpp
*
1205 ctx
->shared
.virtualHeight
;
1206 pVia
->FBFreeEnd
= pVia
->videoRambytes
;
1208 if (!VIADRIScreenInit(ctx
))
1213 /* Get video memory clock. */
1214 VGAOUT8(0x3D4, 0x3D);
1215 pVia
->MemClk
= (VGAIN8(0x3D5) & 0xF0) >> 4;
1216 xf86DrvMsg(0, X_INFO
, "[dri] MemClk (0x%x)\n", pVia
->MemClk
);
1218 /* 3D rendering has noise if not enabled. */
1219 VIAEnableExtendedFIFO(ctx
);
1221 VIAInitialize2DEngine(ctx
);
1223 /* Must disable MMIO or 3D won't work. */
1224 VIADisableMMIO(ctx
);
1226 VIAInitialize3DEngine(ctx
);
1231 static void viaHaltFBDev(DRIDriverContext
*ctx
)
1233 drmUnmap( ctx
->pSAREA
, ctx
->shared
.SAREASize
);
1234 drmClose(ctx
->drmFD
);
1236 if (ctx
->driverPrivate
) {
1237 free(ctx
->driverPrivate
);
1238 ctx
->driverPrivate
= 0;
1242 static int viaEngineShutdown(const DRIDriverContext
*ctx
)
1247 static int viaEngineRestore(const DRIDriverContext
*ctx
)
1252 const struct DRIDriverRec __driDriver
=
1255 viaPostValidateMode
,