1/*
2 * Copyright 1996-1997  David J. McKay
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * DAVID J. MCKAY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23/* Hacked together from mga driver and 3.3.4 NVIDIA driver by Jarno Paananen
24   <jpaana@s2.org> */
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include "nv_const.h"
31#include "riva_include.h"
32
33#include "xf86int10.h"
34
35/*
36 * Forward definitions for the functions that make up the driver.
37 */
38/* Mandatory functions */
39static Bool    RivaPreInit(ScrnInfoPtr pScrn, int flags);
40static Bool    RivaScreenInit(SCREEN_INIT_ARGS_DECL);
41static Bool    RivaEnterVT(VT_FUNC_ARGS_DECL);
42static Bool    RivaEnterVTFBDev(VT_FUNC_ARGS_DECL);
43static void    RivaLeaveVT(VT_FUNC_ARGS_DECL);
44static Bool    RivaCloseScreen(CLOSE_SCREEN_ARGS_DECL);
45static Bool    RivaSaveScreen(ScreenPtr pScreen, int mode);
46
47/* Optional functions */
48static void    RivaFreeScreen(FREE_SCREEN_ARGS_DECL);
49static ModeStatus RivaValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode,
50				Bool verbose, int flags);
51
52/* Internally used functions */
53
54static Bool	RivaMapMem(ScrnInfoPtr pScrn);
55static Bool	RivaMapMemFBDev(ScrnInfoPtr pScrn);
56static Bool	RivaUnmapMem(ScrnInfoPtr pScrn);
57static void	RivaSave(ScrnInfoPtr pScrn);
58static void	RivaRestore(ScrnInfoPtr pScrn);
59static Bool	RivaModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
60
61
62typedef enum {
63    OPTION_SW_CURSOR,
64    OPTION_HW_CURSOR,
65    OPTION_NOACCEL,
66    OPTION_SHOWCACHE,
67    OPTION_SHADOW_FB,
68    OPTION_FBDEV,
69    OPTION_ROTATE
70} RivaOpts;
71
72
73static const OptionInfoRec RivaOptions[] = {
74    { OPTION_SW_CURSOR,         "SWcursor",     OPTV_BOOLEAN,   {0}, FALSE },
75    { OPTION_HW_CURSOR,         "HWcursor",     OPTV_BOOLEAN,   {0}, FALSE },
76    { OPTION_NOACCEL,           "NoAccel",      OPTV_BOOLEAN,   {0}, FALSE },
77    { OPTION_SHOWCACHE,         "ShowCache",    OPTV_BOOLEAN,   {0}, FALSE },
78    { OPTION_SHADOW_FB,         "ShadowFB",     OPTV_BOOLEAN,   {0}, FALSE },
79    { OPTION_FBDEV,             "UseFBDev",     OPTV_BOOLEAN,   {0}, FALSE },
80    { OPTION_ROTATE,		"Rotate",	OPTV_ANYSTR,	{0}, FALSE },
81    { -1,                       NULL,           OPTV_NONE,      {0}, FALSE }
82};
83
84/*
85 * This is intentionally screen-independent.  It indicates the binding
86 * choice made in the first PreInit.
87 */
88static int pix24bpp = 0;
89
90/*
91 * ramdac info structure initialization
92 */
93static RivaRamdacRec DacInit = {
94        FALSE, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL,
95        0, NULL, NULL, NULL, NULL
96};
97
98
99
100static Bool
101RivaGetRec(ScrnInfoPtr pScrn)
102{
103    /*
104     * Allocate an RivaRec, and hook it into pScrn->driverPrivate.
105     * pScrn->driverPrivate is initialised to NULL, so we can check if
106     * the allocation has already been done.
107     */
108    if (pScrn->driverPrivate != NULL)
109        return TRUE;
110
111    pScrn->driverPrivate = xnfcalloc(sizeof(RivaRec), 1);
112    /* Initialise it */
113
114    RivaPTR(pScrn)->Dac = DacInit;
115    return TRUE;
116}
117
118static void
119RivaFreeRec(ScrnInfoPtr pScrn)
120{
121    if (pScrn->driverPrivate == NULL)
122        return;
123    free(pScrn->driverPrivate);
124    pScrn->driverPrivate = NULL;
125}
126
127_X_EXPORT const OptionInfoRec *
128RivaAvailableOptions(int chipid, int busid)
129{
130    return RivaOptions;
131}
132
133
134_X_EXPORT Bool
135RivaGetScrnInfoRec(PciChipsets *chips, int chip)
136{
137    ScrnInfoPtr pScrn;
138
139    pScrn = xf86ConfigPciEntity(NULL, 0, chip,
140                                chips, NULL, NULL, NULL,
141                                NULL, NULL);
142
143    if(!pScrn) return FALSE;
144
145    pScrn->driverVersion    = RIVA_VERSION;
146    pScrn->driverName       = RIVA_DRIVER_NAME;
147    pScrn->name             = RIVA_NAME;
148
149    pScrn->Probe            = NULL;
150    pScrn->PreInit          = RivaPreInit;
151    pScrn->ScreenInit       = RivaScreenInit;
152    pScrn->SwitchMode       = RivaSwitchMode;
153    pScrn->AdjustFrame      = RivaAdjustFrame;
154    pScrn->EnterVT          = RivaEnterVT;
155    pScrn->LeaveVT          = RivaLeaveVT;
156    pScrn->FreeScreen       = RivaFreeScreen;
157    pScrn->ValidMode        = RivaValidMode;
158
159    return TRUE;
160}
161
162/* Usually mandatory */
163Bool
164RivaSwitchMode(SWITCH_MODE_ARGS_DECL)
165{
166    SCRN_INFO_PTR(arg);
167    return RivaModeInit(pScrn, mode);
168}
169
170/*
171 * This function is used to initialize the Start Address - the first
172 * displayed location in the video memory.
173 */
174/* Usually mandatory */
175void
176RivaAdjustFrame(ADJUST_FRAME_ARGS_DECL)
177{
178    SCRN_INFO_PTR(arg);
179    int startAddr;
180    RivaPtr pRiva = RivaPTR(pScrn);
181    RivaFBLayout *pLayout = &pRiva->CurrentLayout;
182
183    if(pRiva->ShowCache && y && pScrn->vtSema)
184	y += pScrn->virtualY - 1;
185
186    startAddr = (((y*pLayout->displayWidth)+x)*(pLayout->bitsPerPixel/8));
187    pRiva->riva.SetStartAddress(&pRiva->riva, startAddr);
188}
189
190
191/*
192 * This is called when VT switching back to the X server.  Its job is
193 * to reinitialise the video mode.
194 *
195 * We may wish to unmap video/MMIO memory too.
196 */
197
198/* Mandatory */
199static Bool
200RivaEnterVT(VT_FUNC_ARGS_DECL)
201{
202    SCRN_INFO_PTR(arg);
203
204    if (!RivaModeInit(pScrn, pScrn->currentMode))
205        return FALSE;
206    RivaAdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
207
208    return TRUE;
209}
210
211static Bool
212RivaEnterVTFBDev(VT_FUNC_ARGS_DECL)
213{
214    SCRN_INFO_PTR(arg);
215    fbdevHWEnterVT(VT_FUNC_ARGS);
216    return TRUE;
217}
218
219/*
220 * This is called when VT switching away from the X server.  Its job is
221 * to restore the previous (text) mode.
222 *
223 * We may wish to remap video/MMIO memory too.
224 */
225
226/* Mandatory */
227static void
228RivaLeaveVT(VT_FUNC_ARGS_DECL)
229{
230    SCRN_INFO_PTR(arg);
231    RivaPtr pRiva = RivaPTR(pScrn);
232
233    RivaRestore(pScrn);
234    pRiva->riva.LockUnlock(&pRiva->riva, 1);
235}
236
237
238
239/*
240 * This is called at the end of each server generation.  It restores the
241 * original (text) mode.  It should also unmap the video memory, and free
242 * any per-generation data allocated by the driver.  It should finish
243 * by unwrapping and calling the saved CloseScreen function.
244 */
245
246/* Mandatory */
247static Bool
248RivaCloseScreen(CLOSE_SCREEN_ARGS_DECL)
249{
250    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
251    RivaPtr pRiva = RivaPTR(pScrn);
252
253    if (pScrn->vtSema) {
254        RivaRestore(pScrn);
255        pRiva->riva.LockUnlock(&pRiva->riva, 1);
256    }
257
258    RivaUnmapMem(pScrn);
259    vgaHWUnmapMem(pScrn);
260#ifdef HAVE_XAA_H
261    if (pRiva->AccelInfoRec)
262        XAADestroyInfoRec(pRiva->AccelInfoRec);
263#endif
264    if (pRiva->CursorInfoRec)
265        xf86DestroyCursorInfoRec(pRiva->CursorInfoRec);
266    if (pRiva->ShadowPtr)
267        free(pRiva->ShadowPtr);
268    if (pRiva->DGAModes)
269        free(pRiva->DGAModes);
270    if ( pRiva->expandBuffer )
271        free(pRiva->expandBuffer);
272
273    pScrn->vtSema = FALSE;
274    pScreen->CloseScreen = pRiva->CloseScreen;
275    return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
276}
277
278/* Free up any persistent data structures */
279
280/* Optional */
281static void
282RivaFreeScreen(FREE_SCREEN_ARGS_DECL)
283{
284    SCRN_INFO_PTR(arg);
285    /*
286     * This only gets called when a screen is being deleted.  It does not
287     * get called routinely at the end of a server generation.
288     */
289    if (xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
290	vgaHWFreeHWRec(pScrn);
291    RivaFreeRec(pScrn);
292}
293
294
295/* Checks if a mode is suitable for the selected chipset. */
296
297/* Optional */
298static ModeStatus
299RivaValidMode(SCRN_ARG_TYPE arg, DisplayModePtr mode, Bool verbose, int flags)
300{
301    return (MODE_OK);
302}
303
304static void
305rivaProbeDDC(ScrnInfoPtr pScrn, int index)
306{
307    vbeInfoPtr pVbe;
308
309    if (xf86LoadSubModule(pScrn, "vbe")) {
310        pVbe = VBEInit(NULL,index);
311        ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
312	vbeFree(pVbe);
313    }
314}
315
316
317Bool RivaI2CInit(ScrnInfoPtr pScrn)
318{
319    const char *mod = "i2c";
320
321    if (xf86LoadSubModule(pScrn, mod)) {
322
323        mod = "ddc";
324        if(xf86LoadSubModule(pScrn, mod)) {
325            return RivaDACi2cInit(pScrn);
326        }
327    }
328
329    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
330              "Couldn't load %s module.  DDC probing can't be done\n", mod);
331
332    return FALSE;
333}
334
335/* Mandatory */
336Bool
337RivaPreInit(ScrnInfoPtr pScrn, int flags)
338{
339    RivaPtr pRiva;
340    MessageType from;
341    int i;
342    ClockRangePtr clockRanges;
343    const char *s;
344
345    if (flags & PROBE_DETECT) {
346        EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
347
348        if (!pEnt)
349            return FALSE;
350
351        i = pEnt->index;
352        free(pEnt);
353
354        rivaProbeDDC(pScrn, i);
355        return TRUE;
356    }
357
358    /*
359     * Note: This function is only called once at server startup, and
360     * not at the start of each server generation.  This means that
361     * only things that are persistent across server generations can
362     * be initialised here.  xf86Screens[] is (pScrn is a pointer to one
363     * of these).  Privates allocated using xf86AllocateScrnInfoPrivateIndex()
364     * are too, and should be used for data that must persist across
365     * server generations.
366     *
367     * Per-generation data should be allocated with
368     * AllocateScreenPrivateIndex() from the ScreenInit() function.
369     */
370
371    /* Check the number of entities, and fail if it isn't one. */
372    if (pScrn->numEntities != 1)
373	return FALSE;
374
375    /* Allocate the RivaRec driverPrivate */
376    if (!RivaGetRec(pScrn)) {
377	return FALSE;
378    }
379    pRiva = RivaPTR(pScrn);
380
381    /* Get the entity, and make sure it is PCI. */
382    pRiva->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
383    if (pRiva->pEnt->location.type != BUS_PCI)
384	return FALSE;
385
386    /* Find the PCI info for this screen */
387    pRiva->PciInfo = xf86GetPciInfoForEntity(pRiva->pEnt->index);
388#ifndef XSERVER_LIBPCIACCESS
389    pRiva->PciTag = pciTag(pRiva->PciInfo->bus, pRiva->PciInfo->device,
390			  pRiva->PciInfo->func);
391#endif
392
393    pRiva->Primary = xf86IsPrimaryPci(pRiva->PciInfo);
394
395    /* Initialize the card through int10 interface if needed */
396    if (xf86LoadSubModule(pScrn, "int10")) {
397#if !defined(__alpha__)
398        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initializing int10\n");
399        pRiva->pInt = xf86InitInt10(pRiva->pEnt->index);
400#endif
401    }
402
403#ifndef XSERVER_LIBPCIACCESS
404    xf86SetOperatingState(resVgaIo, pRiva->pEnt->index, ResUnusedOpr);
405    xf86SetOperatingState(resVgaMem, pRiva->pEnt->index, ResDisableOpr);
406#endif
407
408    /* Set pScrn->monitor */
409    pScrn->monitor = pScrn->confScreen->monitor;
410
411    pRiva->ChipRev = CHIP_REVISION(pRiva->PciInfo);
412    if(VENDOR_ID(pRiva->PciInfo) != PCI_VENDOR_NVIDIA_SGS ||
413       DEVICE_ID(pRiva->PciInfo) != PCI_CHIP_RIVA128)
414    {
415        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "This is not a RIVA 128\n");
416        xf86FreeInt10(pRiva->pInt);
417        return FALSE;
418    }
419
420    pScrn->chipset = "RIVA 128";
421
422    /*
423     * The first thing we should figure out is the depth, bpp, etc.
424     */
425
426    if (!xf86SetDepthBpp(pScrn, 15, 0, 0, Support32bppFb)) {
427	xf86FreeInt10(pRiva->pInt);
428	return FALSE;
429    } else {
430	/* Check that the returned depth is one we support */
431	switch (pScrn->depth) {
432            case 8:
433            case 15:
434            case 24:
435                break;
436            default:
437                xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
438                    "Given depth (%d) is not supported by this driver\n",
439                    pScrn->depth);
440		xf86FreeInt10(pRiva->pInt);
441                return FALSE;
442	}
443    }
444    xf86PrintDepthBpp(pScrn);
445
446    /* Get the depth24 pixmap format */
447    if (pScrn->depth == 24 && pix24bpp == 0)
448	pix24bpp = xf86GetBppFromDepth(pScrn, 24);
449
450    /*
451     * This must happen after pScrn->display has been set because
452     * xf86SetWeight references it.
453     */
454    if (pScrn->depth > 8) {
455	/* The defaults are OK for us */
456	rgb zeros = {0, 0, 0};
457
458	if (!xf86SetWeight(pScrn, zeros, zeros)) {
459	    xf86FreeInt10(pRiva->pInt);
460	    return FALSE;
461	}
462    }
463
464    if (!xf86SetDefaultVisual(pScrn, -1)) {
465	xf86FreeInt10(pRiva->pInt);
466	return FALSE;
467    } else {
468	/* We don't currently support DirectColor at > 8bpp */
469	if (pScrn->depth > 8 && (pScrn->defaultVisual != TrueColor)) {
470	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
471		       " (%s) is not supported at depth %d\n",
472		       xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
473	    xf86FreeInt10(pRiva->pInt);
474	    return FALSE;
475	}
476    }
477
478    /* The vgahw module should be loaded here when needed */
479    if (!xf86LoadSubModule(pScrn, "vgahw")) {
480	xf86FreeInt10(pRiva->pInt);
481	return FALSE;
482    }
483
484    /*
485     * Allocate a vgaHWRec
486     */
487    if (!vgaHWGetHWRec(pScrn)) {
488	xf86FreeInt10(pRiva->pInt);
489	return FALSE;
490    }
491    vgaHWSetStdFuncs(VGAHWPTR(pScrn));
492
493    /* We use a programmable clock */
494    pScrn->progClock = TRUE;
495
496    /* Collect all of the relevant option flags (fill in pScrn->options) */
497    xf86CollectOptions(pScrn, NULL);
498
499    /* Process the options */
500    if (!(pRiva->Options = malloc(sizeof(RivaOptions))))
501	return FALSE;
502    memcpy(pRiva->Options, RivaOptions, sizeof(RivaOptions));
503    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pRiva->Options);
504
505    /* Set the bits per RGB for 8bpp mode */
506    if (pScrn->depth == 8)
507	pScrn->rgbBits = 8;
508
509    from = X_DEFAULT;
510    pRiva->HWCursor = TRUE;
511    /*
512     * The preferred method is to use the "hw cursor" option as a tri-state
513     * option, with the default set above.
514     */
515    if (xf86GetOptValBool(pRiva->Options, OPTION_HW_CURSOR, &pRiva->HWCursor)) {
516	from = X_CONFIG;
517    }
518    /* For compatibility, accept this too (as an override) */
519    if (xf86ReturnOptValBool(pRiva->Options, OPTION_SW_CURSOR, FALSE)) {
520	from = X_CONFIG;
521	pRiva->HWCursor = FALSE;
522    }
523    xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
524		pRiva->HWCursor ? "HW" : "SW");
525    if (xf86ReturnOptValBool(pRiva->Options, OPTION_NOACCEL, FALSE)) {
526	pRiva->NoAccel = TRUE;
527	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
528    }
529    if (xf86ReturnOptValBool(pRiva->Options, OPTION_SHOWCACHE, FALSE)) {
530	pRiva->ShowCache = TRUE;
531	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ShowCache enabled\n");
532    }
533    if (xf86ReturnOptValBool(pRiva->Options, OPTION_SHADOW_FB, FALSE)) {
534	pRiva->ShadowFB = TRUE;
535	pRiva->NoAccel = TRUE;
536	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
537		"Using \"Shadow Framebuffer\" - acceleration disabled\n");
538    }
539    if (xf86ReturnOptValBool(pRiva->Options, OPTION_FBDEV, FALSE)) {
540	pRiva->FBDev = TRUE;
541	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
542		"Using framebuffer device\n");
543    }
544    if (pRiva->FBDev) {
545	/* check for linux framebuffer device */
546	if (!xf86LoadSubModule(pScrn, "fbdevhw")) {
547	    xf86FreeInt10(pRiva->pInt);
548	    return FALSE;
549	}
550
551	if (!fbdevHWInit(pScrn, pRiva->PciInfo, NULL)) {
552	    xf86FreeInt10(pRiva->pInt);
553	    return FALSE;
554	}
555	pScrn->SwitchMode    = fbdevHWSwitchModeWeak();
556	pScrn->AdjustFrame   = fbdevHWAdjustFrameWeak();
557	pScrn->EnterVT       = RivaEnterVTFBDev;
558	pScrn->LeaveVT       = fbdevHWLeaveVTWeak();
559	pScrn->ValidMode     = fbdevHWValidModeWeak();
560    }
561    pRiva->Rotate = 0;
562    if ((s = xf86GetOptValString(pRiva->Options, OPTION_ROTATE))) {
563      if(!xf86NameCmp(s, "CW")) {
564	pRiva->ShadowFB = TRUE;
565	pRiva->NoAccel = TRUE;
566	pRiva->HWCursor = FALSE;
567	pRiva->Rotate = 1;
568	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
569		"Rotating screen clockwise - acceleration disabled\n");
570      } else
571      if(!xf86NameCmp(s, "CCW")) {
572	pRiva->ShadowFB = TRUE;
573	pRiva->NoAccel = TRUE;
574	pRiva->HWCursor = FALSE;
575	pRiva->Rotate = -1;
576	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
577		"Rotating screen counter clockwise - acceleration disabled\n");
578      } else {
579	xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
580		"\"%s\" is not a valid value for Option \"Rotate\"\n", s);
581	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
582		"Valid options are \"CW\" or \"CCW\"\n");
583      }
584    }
585
586    if (pRiva->pEnt->device->MemBase != 0) {
587	/* Require that the config file value matches one of the PCI values. */
588	if (!xf86CheckPciMemBase(pRiva->PciInfo, pRiva->pEnt->device->MemBase)) {
589	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
590		"MemBase 0x%08lX doesn't match any PCI base register.\n",
591		pRiva->pEnt->device->MemBase);
592	    xf86FreeInt10(pRiva->pInt);
593	    RivaFreeRec(pScrn);
594	    return FALSE;
595	}
596	pRiva->FbAddress = pRiva->pEnt->device->MemBase;
597	from = X_CONFIG;
598    } else {
599	int i = 1;
600	pRiva->FbBaseReg = i;
601	if (MEMBASE(pRiva->PciInfo, i) != 0) {
602	    pRiva->FbAddress = MEMBASE(pRiva->PciInfo, i) & 0xff800000;
603	    from = X_PROBED;
604	} else {
605	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
606			   "No valid FB address in PCI config space\n");
607	    xf86FreeInt10(pRiva->pInt);
608	    RivaFreeRec(pScrn);
609	    return FALSE;
610	}
611    }
612    xf86DrvMsg(pScrn->scrnIndex, from, "Linear framebuffer at 0x%lX\n",
613	       (unsigned long)pRiva->FbAddress);
614
615    if (pRiva->pEnt->device->IOBase != 0) {
616	/* Require that the config file value matches one of the PCI values. */
617	if (!xf86CheckPciMemBase(pRiva->PciInfo, pRiva->pEnt->device->IOBase)) {
618	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
619		"IOBase 0x%08lX doesn't match any PCI base register.\n",
620		pRiva->pEnt->device->IOBase);
621	    xf86FreeInt10(pRiva->pInt);
622	    RivaFreeRec(pScrn);
623	    return FALSE;
624	}
625	pRiva->IOAddress = pRiva->pEnt->device->IOBase;
626	from = X_CONFIG;
627    } else {
628	int i = 0;
629	if (MEMBASE(pRiva->PciInfo, i) != 0) {
630	    pRiva->IOAddress = MEMBASE(pRiva->PciInfo, i) & 0xffffc000;
631	    from = X_PROBED;
632	} else {
633	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
634			"No valid MMIO address in PCI config space\n");
635	    xf86FreeInt10(pRiva->pInt);
636	    RivaFreeRec(pScrn);
637	    return FALSE;
638	}
639    }
640    xf86DrvMsg(pScrn->scrnIndex, from, "MMIO registers at 0x%lX\n",
641	       (unsigned long)pRiva->IOAddress);
642
643#ifndef XSERVER_LIBPCIACCESS
644    if (xf86RegisterResources(pRiva->pEnt->index, NULL, ResExclusive)) {
645	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
646		"xf86RegisterResources() found resource conflicts\n");
647	xf86FreeInt10(pRiva->pInt);
648	RivaFreeRec(pScrn);
649	return FALSE;
650    }
651#endif
652    Riva3Setup(pScrn);
653
654    /*
655     * If the user has specified the amount of memory in the XF86Config
656     * file, we respect that setting.
657     */
658    if (pRiva->pEnt->device->videoRam != 0) {
659	pScrn->videoRam = pRiva->pEnt->device->videoRam;
660	from = X_CONFIG;
661    } else {
662	if (pRiva->FBDev) {
663	    pScrn->videoRam = fbdevHWGetVidmem(pScrn)/1024;
664	} else {
665            pScrn->videoRam = pRiva->riva.RamAmountKBytes;
666	}
667	from = X_PROBED;
668    }
669    xf86DrvMsg(pScrn->scrnIndex, from, "VideoRAM: %d kBytes\n",
670               pScrn->videoRam);
671
672    pRiva->FbMapSize = pScrn->videoRam * 1024;
673
674    /*
675     * If the driver can do gamma correction, it should call xf86SetGamma()
676     * here.
677     */
678
679    {
680	Gamma zeros = {0.0, 0.0, 0.0};
681
682	if (!xf86SetGamma(pScrn, zeros)) {
683	    xf86FreeInt10(pRiva->pInt);
684	    return FALSE;
685	}
686    }
687
688    pRiva->FbUsableSize = pRiva->FbMapSize - (32 * 1024);
689
690    /*
691     * Setup the ClockRanges, which describe what clock ranges are available,
692     * and what sort of modes they can be used for.
693     */
694
695    pRiva->MinClock = 12000;
696    pRiva->MaxClock = pRiva->riva.MaxVClockFreqKHz;
697
698    clockRanges = xnfcalloc(sizeof(ClockRange), 1);
699    clockRanges->next = NULL;
700    clockRanges->minClock = pRiva->MinClock;
701    clockRanges->maxClock = pRiva->MaxClock;
702    clockRanges->clockIndex = -1;		/* programmable */
703    clockRanges->interlaceAllowed = TRUE;
704    clockRanges->doubleScanAllowed = TRUE;
705
706
707    /*
708     * xf86ValidateModes will check that the mode HTotal and VTotal values
709     * don't exceed the chipset's limit if pScrn->maxHValue and
710     * pScrn->maxVValue are set.  Since our RivaValidMode() already takes
711     * care of this, we don't worry about setting them here.
712     */
713    i = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
714                          pScrn->display->modes, clockRanges,
715                          NULL, 256, 2048,
716                          32 * pScrn->bitsPerPixel, 128, 2048,
717                          pScrn->display->virtualX,
718                          pScrn->display->virtualY,
719                          pRiva->FbUsableSize,
720                          LOOKUP_BEST_REFRESH);
721
722    if (i < 1 && pRiva->FBDev) {
723	fbdevHWUseBuildinMode(pScrn);
724	pScrn->displayWidth = pScrn->virtualX; /* FIXME: might be wrong */
725	i = 1;
726    }
727    if (i == -1) {
728	xf86FreeInt10(pRiva->pInt);
729	RivaFreeRec(pScrn);
730	return FALSE;
731    }
732
733    /* Prune the modes marked as invalid */
734    xf86PruneDriverModes(pScrn);
735
736    if (i == 0 || pScrn->modes == NULL) {
737	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
738	xf86FreeInt10(pRiva->pInt);
739	RivaFreeRec(pScrn);
740	return FALSE;
741    }
742
743    /*
744     * Set the CRTC parameters for all of the modes based on the type
745     * of mode, and the chipset's interlace requirements.
746     *
747     * Calling this is required if the mode->Crtc* values are used by the
748     * driver and if the driver doesn't provide code to set them.  They
749     * are not pre-initialised at all.
750     */
751    xf86SetCrtcForModes(pScrn, 0);
752
753    /* Set the current mode to the first in the list */
754    pScrn->currentMode = pScrn->modes;
755
756    /* Print the list of modes being used */
757    xf86PrintModes(pScrn);
758
759    /* Set display resolution */
760    xf86SetDpi(pScrn, 0, 0);
761
762
763    /*
764     * XXX This should be taken into account in some way in the mode valdation
765     * section.
766     */
767
768    if (xf86LoadSubModule(pScrn, "fb") == NULL) {
769	xf86FreeInt10(pRiva->pInt);
770	RivaFreeRec(pScrn);
771	return FALSE;
772    }
773
774    /* Load XAA if needed */
775    if (!pRiva->NoAccel) {
776	if (!xf86LoadSubModule(pScrn, "xaa")) {
777	    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Falling back to shadowfb\n");
778	    pRiva->NoAccel = 1;
779	    pRiva->ShadowFB = 1;
780	}
781    }
782
783    /* Load ramdac if needed */
784    if (pRiva->HWCursor) {
785	if (!xf86LoadSubModule(pScrn, "ramdac")) {
786	    xf86FreeInt10(pRiva->pInt);
787	    RivaFreeRec(pScrn);
788	    return FALSE;
789	}
790    }
791
792    /* Load shadowfb if needed */
793    if (pRiva->ShadowFB) {
794	if (!xf86LoadSubModule(pScrn, "shadowfb")) {
795	    xf86FreeInt10(pRiva->pInt);
796	    RivaFreeRec(pScrn);
797	    return FALSE;
798	}
799    }
800
801    pRiva->CurrentLayout.bitsPerPixel = pScrn->bitsPerPixel;
802    pRiva->CurrentLayout.depth = pScrn->depth;
803    pRiva->CurrentLayout.displayWidth = pScrn->displayWidth;
804    pRiva->CurrentLayout.weight.red = pScrn->weight.red;
805    pRiva->CurrentLayout.weight.green = pScrn->weight.green;
806    pRiva->CurrentLayout.weight.blue = pScrn->weight.blue;
807    pRiva->CurrentLayout.mode = pScrn->currentMode;
808
809    xf86FreeInt10(pRiva->pInt);
810
811    pRiva->pInt = NULL;
812    return TRUE;
813}
814
815
816/*
817 * Map the framebuffer and MMIO memory.
818 */
819
820static Bool
821RivaMapMem(ScrnInfoPtr pScrn)
822{
823    RivaPtr pRiva = RivaPTR(pScrn);
824
825    /*
826     * Map IO registers to virtual address space
827     */
828#ifdef XSERVER_LIBPCIACCESS
829    void *tmp;
830
831    pci_device_map_range(pRiva->PciInfo, pRiva->IOAddress, 0x1000000,
832                         PCI_DEV_MAP_FLAG_WRITABLE, &tmp);
833    pRiva->IOBase = tmp;
834    pci_device_map_range(pRiva->PciInfo, pRiva->FbAddress, pRiva->FbMapSize,
835                         PCI_DEV_MAP_FLAG_WRITABLE |
836                         PCI_DEV_MAP_FLAG_WRITE_COMBINE,
837                         &tmp);
838    pRiva->FbBase = tmp;
839#else
840    pRiva->IOBase = xf86MapPciMem(pScrn->scrnIndex,
841                                VIDMEM_MMIO | VIDMEM_READSIDEEFFECT,
842                                pRiva->PciTag, pRiva->IOAddress, 0x1000000);
843    pRiva->FbBase = xf86MapPciMem(pScrn->scrnIndex, VIDMEM_FRAMEBUFFER,
844				 pRiva->PciTag, pRiva->FbAddress,
845				 pRiva->FbMapSize);
846#endif
847
848    if (pRiva->IOBase == NULL)
849	return FALSE;
850
851    if (pRiva->FbBase == NULL)
852	return FALSE;
853
854    pRiva->FbStart = pRiva->FbBase;
855
856    return TRUE;
857}
858
859Bool
860RivaMapMemFBDev(ScrnInfoPtr pScrn)
861{
862    RivaPtr pRiva;
863
864    pRiva = RivaPTR(pScrn);
865
866    pRiva->FbBase = fbdevHWMapVidmem(pScrn);
867    if (pRiva->FbBase == NULL)
868        return FALSE;
869
870    pRiva->IOBase = fbdevHWMapMMIO(pScrn);
871    if (pRiva->IOBase == NULL)
872        return FALSE;
873
874    pRiva->FbStart = pRiva->FbBase;
875
876    return TRUE;
877}
878
879/*
880 * Unmap the framebuffer and MMIO memory.
881 */
882
883static Bool
884RivaUnmapMem(ScrnInfoPtr pScrn)
885{
886    RivaPtr pRiva;
887
888    pRiva = RivaPTR(pScrn);
889
890    /*
891     * Unmap IO registers to virtual address space
892     */
893#ifdef XSERVER_LIBPCIACCESS
894    pci_device_unmap_range(pRiva->PciInfo, pRiva->IOBase, 0x1000000);
895    pci_device_unmap_range(pRiva->PciInfo, pRiva->FbBase, pRiva->FbMapSize);
896#else
897    xf86UnMapVidMem(pScrn->scrnIndex, (pointer)pRiva->IOBase, 0x1000000);
898    xf86UnMapVidMem(pScrn->scrnIndex, (pointer)pRiva->FbBase, pRiva->FbMapSize);
899#endif
900
901    pRiva->IOBase = NULL;
902    pRiva->FbBase = NULL;
903    pRiva->FbStart = NULL;
904
905    return TRUE;
906}
907
908
909/*
910 * Initialise a new mode.
911 */
912
913static Bool
914RivaModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
915{
916    vgaHWPtr hwp = VGAHWPTR(pScrn);
917    vgaRegPtr vgaReg;
918    RivaPtr pRiva = RivaPTR(pScrn);
919    RivaRegPtr rivaReg;
920
921
922    /* Initialise the ModeReg values */
923    if (!vgaHWInit(pScrn, mode))
924	return FALSE;
925    pScrn->vtSema = TRUE;
926
927    vgaReg = &hwp->ModeReg;
928    rivaReg = &pRiva->ModeReg;
929
930    if(!(*pRiva->ModeInit)(pScrn, mode))
931        return FALSE;
932
933    pRiva->riva.LockUnlock(&pRiva->riva, 0);
934
935    /* Program the registers */
936    vgaHWProtect(pScrn, TRUE);
937
938    (*pRiva->Restore)(pScrn, vgaReg, rivaReg, FALSE);
939
940    RivaResetGraphics(pScrn);
941
942    vgaHWProtect(pScrn, FALSE);
943
944    pRiva->CurrentLayout.mode = mode;
945
946    return TRUE;
947}
948
949/*
950 * Restore the initial (text) mode.
951 */
952static void
953RivaRestore(ScrnInfoPtr pScrn)
954{
955    vgaHWPtr hwp = VGAHWPTR(pScrn);
956    vgaRegPtr vgaReg = &hwp->SavedReg;
957    RivaPtr pRiva = RivaPTR(pScrn);
958    RivaRegPtr rivaReg = &pRiva->SavedReg;
959
960
961    pRiva->riva.LockUnlock(&pRiva->riva, 0);
962
963    /* Only restore text mode fonts/text for the primary card */
964    vgaHWProtect(pScrn, TRUE);
965    (*pRiva->Restore)(pScrn, vgaReg, rivaReg, pRiva->Primary);
966    vgaHWProtect(pScrn, FALSE);
967}
968
969static void
970RivaDPMSSet(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
971{
972  unsigned char crtc1A;
973  vgaHWPtr hwp = VGAHWPTR(pScrn);
974
975  if (!pScrn->vtSema) return;
976
977  crtc1A = hwp->readCrtc(hwp, 0x1A) & ~0xC0;
978
979  switch (PowerManagementMode) {
980  case DPMSModeStandby:  /* HSync: Off, VSync: On */
981    crtc1A |= 0x80;
982    break;
983  case DPMSModeSuspend:  /* HSync: On, VSync: Off */
984    crtc1A |= 0x40;
985    break;
986  case DPMSModeOff:      /* HSync: Off, VSync: Off */
987    crtc1A |= 0xC0;
988    break;
989  case DPMSModeOn:       /* HSync: On, VSync: On */
990  default:
991    break;
992  }
993
994  /* vgaHWDPMSSet will merely cut the dac output */
995  vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
996
997  hwp->writeCrtc(hwp, 0x1A, crtc1A);
998}
999
1000
1001/* Mandatory */
1002
1003/* This gets called at the start of each server generation */
1004
1005static Bool
1006RivaScreenInit(SCREEN_INIT_ARGS_DECL)
1007{
1008    ScrnInfoPtr pScrn;
1009    vgaHWPtr hwp;
1010    RivaPtr pRiva;
1011    RivaRamdacPtr Rivadac;
1012    int ret;
1013    VisualPtr visual;
1014    unsigned char *FBStart;
1015    int width, height, displayWidth;
1016    BoxRec AvailFBArea;
1017
1018    /*
1019     * First get the ScrnInfoRec
1020     */
1021    pScrn = xf86ScreenToScrn(pScreen);
1022
1023
1024    hwp = VGAHWPTR(pScrn);
1025    pRiva = RivaPTR(pScrn);
1026    Rivadac = &pRiva->Dac;
1027
1028    /* Map the Riva memory and MMIO areas */
1029    if (pRiva->FBDev) {
1030	if (!RivaMapMemFBDev(pScrn))
1031	    return FALSE;
1032    } else {
1033	if (!RivaMapMem(pScrn))
1034	    return FALSE;
1035    }
1036
1037    /* Map the VGA memory when the primary video */
1038    if (pRiva->Primary && !pRiva->FBDev) {
1039	hwp->MapSize = 0x10000;
1040	if (!vgaHWMapMem(pScrn))
1041	    return FALSE;
1042    }
1043
1044    if (pRiva->FBDev) {
1045	fbdevHWSave(pScrn);
1046	if (!fbdevHWModeInit(pScrn, pScrn->currentMode))
1047	    return FALSE;
1048    } else {
1049	/* Save the current state */
1050	RivaSave(pScrn);
1051	/* Initialise the first mode */
1052	if (!RivaModeInit(pScrn, pScrn->currentMode))
1053	    return FALSE;
1054    }
1055
1056
1057    /* Darken the screen for aesthetic reasons and set the viewport */
1058    RivaSaveScreen(pScreen, SCREEN_SAVER_ON);
1059    pScrn->AdjustFrame(ADJUST_FRAME_ARGS(pScrn, pScrn->frameX0, pScrn->frameY0));
1060
1061
1062    /*
1063     * The next step is to setup the screen's visuals, and initialise the
1064     * framebuffer code.  In cases where the framebuffer's default
1065     * choices for things like visual layouts and bits per RGB are OK,
1066     * this may be as simple as calling the framebuffer's ScreenInit()
1067     * function.  If not, the visuals will need to be setup before calling
1068     * a fb ScreenInit() function and fixed up after.
1069     *
1070     * For most PC hardware at depths >= 8, the defaults that fb uses
1071     * are not appropriate.  In this driver, we fixup the visuals after.
1072     */
1073
1074    /*
1075     * Reset the visual list.
1076     */
1077    miClearVisualTypes();
1078
1079    /* Setup the visuals we support. */
1080
1081    if (pScrn->bitsPerPixel > 8) {
1082          if (!miSetVisualTypes(pScrn->depth, TrueColorMask, 8,
1083                                pScrn->defaultVisual))
1084              return FALSE;
1085    } else {
1086          if (!miSetVisualTypes(pScrn->depth,
1087                                miGetDefaultVisualMask(pScrn->depth), 8,
1088                                pScrn->defaultVisual))
1089	  return FALSE;
1090     }
1091    if (!miSetPixmapDepths ()) return FALSE;
1092
1093
1094    /*
1095     * Call the framebuffer layer's ScreenInit function, and fill in other
1096     * pScreen fields.
1097     */
1098
1099    width = pScrn->virtualX;
1100    height = pScrn->virtualY;
1101    displayWidth = pScrn->displayWidth;
1102
1103
1104    if(pRiva->Rotate) {
1105	height = pScrn->virtualX;
1106	width = pScrn->virtualY;
1107    }
1108
1109    if(pRiva->ShadowFB) {
1110 	pRiva->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * width);
1111        pRiva->ShadowPtr = malloc(pRiva->ShadowPitch * height);
1112	displayWidth = pRiva->ShadowPitch / (pScrn->bitsPerPixel >> 3);
1113        FBStart = pRiva->ShadowPtr;
1114    } else {
1115	pRiva->ShadowPtr = NULL;
1116	FBStart = pRiva->FbStart;
1117    }
1118
1119    switch (pScrn->bitsPerPixel) {
1120        case 8:
1121        case 16:
1122        case 32:
1123            ret = fbScreenInit(pScreen, FBStart, width, height,
1124                               pScrn->xDpi, pScrn->yDpi,
1125                               displayWidth, pScrn->bitsPerPixel);
1126            break;
1127        default:
1128            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1129                       "Internal error: invalid bpp (%d) in RivaScreenInit\n",
1130                       pScrn->bitsPerPixel);
1131            ret = FALSE;
1132            break;
1133    }
1134    if (!ret)
1135	return FALSE;
1136
1137
1138    if (pScrn->bitsPerPixel > 8) {
1139        /* Fixup RGB ordering */
1140        visual = pScreen->visuals + pScreen->numVisuals;
1141        while (--visual >= pScreen->visuals) {
1142	    if ((visual->class | DynamicClass) == DirectColor) {
1143		visual->offsetRed = pScrn->offset.red;
1144		visual->offsetGreen = pScrn->offset.green;
1145		visual->offsetBlue = pScrn->offset.blue;
1146		visual->redMask = pScrn->mask.red;
1147		visual->greenMask = pScrn->mask.green;
1148		visual->blueMask = pScrn->mask.blue;
1149	    }
1150	}
1151    }
1152
1153    fbPictureInit (pScreen, 0, 0);
1154
1155    xf86SetBlackWhitePixels(pScreen);
1156
1157
1158    if(!pRiva->ShadowFB) /* hardware cursor needs to wrap this layer */
1159	RivaDGAInit(pScreen);
1160
1161    AvailFBArea.x1 = 0;
1162    AvailFBArea.y1 = 0;
1163    AvailFBArea.x2 = pScrn->displayWidth;
1164    AvailFBArea.y2 = (min(pRiva->FbUsableSize, 32*1024*1024)) /
1165                     (pScrn->displayWidth * pScrn->bitsPerPixel / 8);
1166    xf86InitFBManager(pScreen, &AvailFBArea);
1167
1168    if (!pRiva->NoAccel)
1169	RivaAccelInit(pScreen);
1170
1171    xf86SetBackingStore(pScreen);
1172    xf86SetSilkenMouse(pScreen);
1173
1174
1175    /* Initialize software cursor.
1176	Must precede creation of the default colormap */
1177    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1178
1179
1180    /* Initialize HW cursor layer.
1181	Must follow software cursor initialization*/
1182    if (pRiva->HWCursor) {
1183	if(!RivaCursorInit(pScreen))
1184	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1185		"Hardware cursor initialization failed\n");
1186    }
1187
1188    /* Initialise default colourmap */
1189    if (!miCreateDefColormap(pScreen))
1190	return FALSE;
1191
1192
1193    /* Initialize colormap layer.
1194	Must follow initialization of the default colormap */
1195    if(!xf86HandleColormaps(pScreen, 256, 8,
1196	(pRiva->FBDev ? fbdevHWLoadPaletteWeak() : Rivadac->LoadPalette),
1197	NULL, CMAP_RELOAD_ON_MODE_SWITCH | CMAP_PALETTED_TRUECOLOR))
1198	return FALSE;
1199
1200
1201    if(pRiva->ShadowFB) {
1202	RefreshAreaFuncPtr refreshArea = RivaRefreshArea;
1203
1204	if(pRiva->Rotate) {
1205   	   pRiva->PointerMoved = pScrn->PointerMoved;
1206	   pScrn->PointerMoved = RivaPointerMoved;
1207
1208	   switch(pScrn->bitsPerPixel) {
1209               case 8:	refreshArea = RivaRefreshArea8;	break;
1210               case 16:	refreshArea = RivaRefreshArea16;	break;
1211               case 32:	refreshArea = RivaRefreshArea32;	break;
1212	   }
1213#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 24
1214           xf86DisableRandR();
1215           xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1216                      "Driver rotation enabled, RandR disabled\n");
1217#else
1218           xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1219                      "Driver rotation enabled\n");
1220#endif
1221	}
1222
1223	ShadowFBInit(pScreen, refreshArea);
1224    }
1225
1226    xf86DPMSInit(pScreen, RivaDPMSSet, 0);
1227
1228
1229    pScrn->memPhysBase = pRiva->FbAddress;
1230    pScrn->fbOffset = 0;
1231
1232    pScreen->SaveScreen = RivaSaveScreen;
1233
1234    /* Wrap the current CloseScreen function */
1235    pRiva->CloseScreen = pScreen->CloseScreen;
1236    pScreen->CloseScreen = RivaCloseScreen;
1237
1238    /* Report any unused options (only for the first generation) */
1239    if (serverGeneration == 1) {
1240	xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1241    }
1242    /* Done */
1243    return TRUE;
1244}
1245
1246/* Free up any persistent data structures */
1247
1248
1249/* Do screen blanking */
1250
1251/* Mandatory */
1252static Bool
1253RivaSaveScreen(ScreenPtr pScreen, int mode)
1254{
1255    return vgaHWSaveScreen(pScreen, mode);
1256}
1257
1258static void
1259RivaSave(ScrnInfoPtr pScrn)
1260{
1261    RivaPtr pRiva = RivaPTR(pScrn);
1262    RivaRegPtr rivaReg = &pRiva->SavedReg;
1263    vgaHWPtr pVga = VGAHWPTR(pScrn);
1264    vgaRegPtr vgaReg = &pVga->SavedReg;
1265
1266    (*pRiva->Save)(pScrn, vgaReg, rivaReg, pRiva->Primary);
1267}
1268