1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif
5
6#include "xf86.h"
7#include "xf86_OSproc.h"
8#include "xf86Pci.h"
9#include "ct_driver.h"
10#include "dgaproc.h"
11
12
13static Bool CHIPS_OpenFramebuffer(ScrnInfoPtr, char **, unsigned char **,
14					int *, int *, int *);
15static Bool CHIPS_SetMode(ScrnInfoPtr, DGAModePtr);
16static int  CHIPS_GetViewport(ScrnInfoPtr);
17static void CHIPS_SetViewport(ScrnInfoPtr, int, int, int);
18#ifdef HAVE_XAA_H
19static void CHIPS_FillRect(ScrnInfoPtr, int, int, int, int, unsigned long);
20static void CHIPS_BlitRect(ScrnInfoPtr, int, int, int, int, int, int);
21#if 0
22static void CHIPS_BlitTransRect(ScrnInfoPtr, int, int, int, int, int, int,
23					unsigned long);
24#endif
25#endif
26
27static
28DGAFunctionRec CHIPS_DGAFuncs = {
29   CHIPS_OpenFramebuffer,
30   NULL,
31   CHIPS_SetMode,
32   CHIPS_SetViewport,
33   CHIPS_GetViewport,
34#ifdef HAVE_XAA_H
35   CHIPSSync,
36   CHIPS_FillRect,
37   CHIPS_BlitRect,
38#if 0
39   CHIPS_BlitTransRect
40#else
41   NULL
42#endif
43#else
44   NULL, NULL, NULL, NULL
45#endif
46};
47
48static
49DGAFunctionRec CHIPS_MMIODGAFuncs = {
50   CHIPS_OpenFramebuffer,
51   NULL,
52   CHIPS_SetMode,
53   CHIPS_SetViewport,
54   CHIPS_GetViewport,
55#ifdef HAVE_XAA_H
56   CHIPSMMIOSync,
57   CHIPS_FillRect,
58   CHIPS_BlitRect,
59#if 0
60   CHIPS_BlitTransRect
61#else
62   NULL
63#endif
64#else
65   NULL, NULL, NULL, NULL
66#endif
67};
68
69static
70DGAFunctionRec CHIPS_HiQVDGAFuncs = {
71   CHIPS_OpenFramebuffer,
72   NULL,
73   CHIPS_SetMode,
74   CHIPS_SetViewport,
75   CHIPS_GetViewport,
76#ifdef HAVE_XAA_H
77   CHIPSHiQVSync,
78   CHIPS_FillRect,
79   CHIPS_BlitRect,
80#if 0
81   CHIPS_BlitTransRect
82#else
83   NULL
84#endif
85#else
86   NULL, NULL, NULL, NULL
87#endif
88};
89
90
91Bool
92CHIPSDGAInit(ScreenPtr pScreen)
93{
94   ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
95   CHIPSPtr cPtr = CHIPSPTR(pScrn);
96   DGAModePtr modes = NULL, newmodes = NULL, currentMode;
97   DisplayModePtr pMode, firstMode;
98   int Bpp = pScrn->bitsPerPixel >> 3;
99   int num = 0;
100   Bool oneMore;
101   int imlines =  (pScrn->videoRam * 1024) /
102      (pScrn->displayWidth * (pScrn->bitsPerPixel >> 3));
103
104   pMode = firstMode = pScrn->modes;
105
106   while(pMode) {
107
108	if(0 /*pScrn->displayWidth != pMode->HDisplay*/) {
109	    newmodes = realloc(modes, (num + 2) * sizeof(DGAModeRec));
110	    oneMore = TRUE;
111	} else {
112	    newmodes = realloc(modes, (num + 1) * sizeof(DGAModeRec));
113	    oneMore = FALSE;
114	}
115
116	if(!newmodes) {
117	   free(modes);
118	   return FALSE;
119	}
120	modes = newmodes;
121
122SECOND_PASS:
123
124	currentMode = modes + num;
125	num++;
126
127	currentMode->mode = pMode;
128	currentMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
129#ifdef HAVE_XAA_H
130	if(cPtr->Flags & ChipsAccelSupport)
131	   currentMode->flags |= (cPtr->Flags & ChipsAccelSupport)
132	     ? (DGA_FILL_RECT | DGA_BLIT_RECT) : 0;
133#endif
134	if(pMode->Flags & V_DBLSCAN)
135	   currentMode->flags |= DGA_DOUBLESCAN;
136	if(pMode->Flags & V_INTERLACE)
137	   currentMode->flags |= DGA_INTERLACED;
138	currentMode->byteOrder = pScrn->imageByteOrder;
139	currentMode->depth = pScrn->depth;
140	currentMode->bitsPerPixel = pScrn->bitsPerPixel;
141	currentMode->red_mask = pScrn->mask.red;
142	currentMode->green_mask = pScrn->mask.green;
143	currentMode->blue_mask = pScrn->mask.blue;
144	currentMode->visualClass = (Bpp == 1) ? PseudoColor : TrueColor;
145	currentMode->viewportWidth = pMode->HDisplay;
146	currentMode->viewportHeight = pMode->VDisplay;
147	currentMode->xViewportStep = 1;
148	currentMode->yViewportStep = 1;
149 	currentMode->viewportFlags = DGA_FLIP_RETRACE | DGA_FLIP_IMMEDIATE;
150	currentMode->offset = 0;
151	currentMode->address = cPtr->FbBase;
152
153	if(oneMore) { /* first one is narrow width */
154	    currentMode->bytesPerScanline = ((pMode->HDisplay * Bpp) + 3) & ~3L;
155	    currentMode->imageWidth = pMode->HDisplay;
156	    currentMode->imageHeight =  imlines;
157	    currentMode->pixmapWidth = currentMode->imageWidth;
158	    currentMode->pixmapHeight = currentMode->imageHeight;
159	    currentMode->maxViewportX = currentMode->imageWidth -
160					currentMode->viewportWidth;
161	    /* this might need to get clamped to some maximum */
162	    currentMode->maxViewportY = currentMode->imageHeight -
163					currentMode->viewportHeight;
164	    oneMore = FALSE;
165	    goto SECOND_PASS;
166	} else {
167	    currentMode->bytesPerScanline =
168			((pScrn->displayWidth * Bpp) + 3) & ~3L;
169	    currentMode->imageWidth = pScrn->displayWidth;
170	    currentMode->imageHeight =  imlines;
171	    currentMode->pixmapWidth = currentMode->imageWidth;
172	    currentMode->pixmapHeight = currentMode->imageHeight;
173	    currentMode->maxViewportX = currentMode->imageWidth -
174					currentMode->viewportWidth;
175	    /* this might need to get clamped to some maximum */
176	    currentMode->maxViewportY = currentMode->imageHeight -
177					currentMode->viewportHeight;
178	}
179
180	pMode = pMode->next;
181	if(pMode == firstMode)
182	   break;
183   }
184
185   cPtr->numDGAModes = num;
186   cPtr->DGAModes = modes;
187
188   if (IS_HiQV(cPtr)) {
189	return DGAInit(pScreen, &CHIPS_HiQVDGAFuncs, modes, num);
190   } else {
191	if(!cPtr->UseMMIO) {
192	    return DGAInit(pScreen, &CHIPS_DGAFuncs, modes, num);
193	} else {
194	    return DGAInit(pScreen, &CHIPS_MMIODGAFuncs, modes, num);
195	}
196   }
197}
198
199
200static Bool
201CHIPS_SetMode(
202   ScrnInfoPtr pScrn,
203   DGAModePtr pMode
204){
205   static int OldDisplayWidth[MAXSCREENS];
206   int index = pScrn->pScreen->myNum;
207
208   CHIPSPtr cPtr = CHIPSPTR(pScrn);
209
210   if (!pMode) { /* restore the original mode */
211	/* put the ScreenParameters back */
212       if (cPtr->DGAactive) {
213           pScrn->displayWidth = OldDisplayWidth[index];
214	   pScrn->EnterVT(VT_FUNC_ARGS);
215
216	   cPtr->DGAactive = FALSE;
217       }
218   } else {
219	if(!cPtr->DGAactive) {  /* save the old parameters */
220	    OldDisplayWidth[index] = pScrn->displayWidth;
221	    pScrn->LeaveVT(VT_FUNC_ARGS);
222	    cPtr->DGAactive = TRUE;
223	}
224
225	pScrn->displayWidth = pMode->bytesPerScanline /
226			      (pMode->bitsPerPixel >> 3);
227
228        CHIPSSwitchMode(SWITCH_MODE_ARGS(pScrn, pMode->mode));
229   }
230
231   return TRUE;
232}
233
234
235
236static int
237CHIPS_GetViewport(
238  ScrnInfoPtr pScrn
239){
240    CHIPSPtr cPtr = CHIPSPTR(pScrn);
241
242    return cPtr->DGAViewportStatus;
243}
244
245static void
246CHIPS_SetViewport(
247   ScrnInfoPtr pScrn,
248   int x, int y,
249   int flags
250   ){
251    vgaHWPtr hwp = VGAHWPTR(pScrn);
252    CHIPSPtr cPtr = CHIPSPTR(pScrn);
253
254    if (flags & DGA_FLIP_RETRACE) {
255	while ((hwp->readST01(hwp)) & 0x08){};
256	while (!((hwp->readST01(hwp)) & 0x08)){};
257    }
258
259    CHIPSAdjustFrame(ADJUST_FRAME_ARGS(pScrn, x, y));
260    cPtr->DGAViewportStatus = 0;  /* CHIPSAdjustFrame loops until finished */
261}
262
263#ifdef HAVE_XAA_H
264static void
265CHIPS_FillRect (
266   ScrnInfoPtr pScrn,
267   int x, int y, int w, int h,
268   unsigned long color
269){
270    CHIPSPtr cPtr = CHIPSPTR(pScrn);
271
272    if(cPtr->AccelInfoRec) {
273	(*cPtr->AccelInfoRec->SetupForSolidFill)(pScrn, color, GXcopy, ~0);
274	(*cPtr->AccelInfoRec->SubsequentSolidFillRect)(pScrn, x, y, w, h);
275	SET_SYNC_FLAG(cPtr->AccelInfoRec);
276    }
277}
278
279static void
280CHIPS_BlitRect(
281   ScrnInfoPtr pScrn,
282   int srcx, int srcy,
283   int w, int h,
284   int dstx, int dsty
285){
286    CHIPSPtr cPtr = CHIPSPTR(pScrn);
287
288    if(cPtr->AccelInfoRec) {
289	int xdir = ((srcx < dstx) && (srcy == dsty)) ? -1 : 1;
290	int ydir = (srcy < dsty) ? -1 : 1;
291
292	(*cPtr->AccelInfoRec->SetupForScreenToScreenCopy)(
293		pScrn, xdir, ydir, GXcopy, ~0, -1);
294	(*cPtr->AccelInfoRec->SubsequentScreenToScreenCopy)(
295		pScrn, srcx, srcy, dstx, dsty, w, h);
296	SET_SYNC_FLAG(cPtr->AccelInfoRec);
297    }
298}
299
300#if 0
301static void
302CHIPS_BlitTransRect(
303   ScrnInfoPtr pScrn,
304   int srcx, int srcy,
305   int w, int h,
306   int dstx, int dsty,
307   unsigned long color
308){
309  /* this one should be separate since the XAA function would
310     prohibit usage of ~0 as the key */
311}
312#endif
313#endif
314static Bool
315CHIPS_OpenFramebuffer(
316   ScrnInfoPtr pScrn,
317   char **name,
318   unsigned char **mem,
319   int *size,
320   int *offset,
321   int *flags
322){
323    CHIPSPtr cPtr = CHIPSPTR(pScrn);
324
325    *name = NULL; 		/* no special device */
326    *mem = (unsigned char*)cPtr->FbAddress;
327    *size = cPtr->FbMapSize;
328    *offset = 0;
329    *flags = DGA_NEED_ROOT;
330
331    return TRUE;
332}
333