~ubuntu-branches/ubuntu/precise/xserver-xorg-video-openchrome-lts-trusty/precise-proposed

« back to all changes in this revision

Viewing changes to src/via_driver.c

  • Committer: Package Import Robot
  • Author(s): Maarten Lankhorst
  • Date: 2014-05-15 12:47:33 UTC
  • Revision ID: package-import@ubuntu.com-20140515124733-qw5cb5dqlvejqsy3
Tags: upstream-0.3.3
ImportĀ upstreamĀ versionĀ 0.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2005-2007 The Openchrome Project  [openchrome.org]
 
3
 * Copyright 2004-2006 Luc Verhaegen.
 
4
 * Copyright 2004-2005 The Unichrome Project  [unichrome.sf.net]
 
5
 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
 
6
 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
 
7
 *
 
8
 * Permission is hereby granted, free of charge, to any person obtaining a
 
9
 * copy of this software and associated documentation files (the "Software"),
 
10
 * to deal in the Software without restriction, including without limitation
 
11
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 
12
 * and/or sell copies of the Software, and to permit persons to whom the
 
13
 * Software is furnished to do so, subject to the following conditions:
 
14
 *
 
15
 * The above copyright notice and this permission notice (including the
 
16
 * next paragraph) shall be included in all copies or substantial portions
 
17
 * of the Software.
 
18
 *
 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 
22
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
25
 * DEALINGS IN THE SOFTWARE.
 
26
 */
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#include "config.h"
 
30
#endif
 
31
 
 
32
#include "shadow.h"
 
33
 
 
34
#include "globals.h"
 
35
#ifdef HAVE_XEXTPROTO_71
 
36
#include <X11/extensions/dpmsconst.h>
 
37
#else
 
38
#define DPMS_SERVER
 
39
#include <X11/extensions/dpms.h>
 
40
#endif
 
41
 
 
42
#include "version.h"
 
43
#include "via_driver.h"
 
44
 
 
45
#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
 
46
#include "xf86RAC.h"
 
47
#endif
 
48
#include "xf86Crtc.h"
 
49
 
 
50
#ifdef HAVE_DRI
 
51
#include "dri.h"
 
52
#endif
 
53
 
 
54
/* RandR support */
 
55
#include "xf86RandR12.h"
 
56
 
 
57
typedef struct
 
58
{
 
59
        int major;
 
60
        int minor;
 
61
        int patchlevel;
 
62
} ViaDRMVersion;
 
63
 
 
64
static const ViaDRMVersion drmExpected = { 1, 3, 0 };
 
65
static const ViaDRMVersion drmCompat = { 3, 1, 0 };
 
66
 
 
67
/* Prototypes. */
 
68
static void VIAIdentify(int flags);
 
69
 
 
70
#ifdef HAVE_PCIACCESS
 
71
static Bool via_pci_probe(DriverPtr drv, int entity_num,
 
72
                          struct pci_device *dev, intptr_t match_data);
 
73
#else /* !HAVE_PCIACCESS */
 
74
static Bool VIAProbe(DriverPtr drv, int flags);
 
75
#endif
 
76
 
 
77
static Bool VIASetupDefaultOptions(ScrnInfoPtr pScrn);
 
78
static Bool VIAPreInit(ScrnInfoPtr pScrn, int flags);
 
79
static Bool VIAScreenInit(SCREEN_INIT_ARGS_DECL);
 
80
static const OptionInfoRec *VIAAvailableOptions(int chipid, int busid);
 
81
 
 
82
#ifdef HAVE_PCIACCESS
 
83
 
 
84
#define VIA_DEVICE_MATCH(d,i) \
 
85
    { 0x1106, (d), PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, (i) }
 
86
 
 
87
static const struct pci_id_match via_device_match[] = {
 
88
   VIA_DEVICE_MATCH (PCI_CHIP_VT3204, 0 ),
 
89
   VIA_DEVICE_MATCH (PCI_CHIP_VT3259, 0 ),
 
90
   VIA_DEVICE_MATCH (PCI_CHIP_CLE3122, 0 ),
 
91
   VIA_DEVICE_MATCH (PCI_CHIP_VT3205, 0 ),
 
92
   VIA_DEVICE_MATCH (PCI_CHIP_VT3314, 0 ),
 
93
   VIA_DEVICE_MATCH (PCI_CHIP_VT3336, 0 ),
 
94
   VIA_DEVICE_MATCH (PCI_CHIP_VT3364, 0 ),
 
95
   VIA_DEVICE_MATCH (PCI_CHIP_VT3324, 0 ),
 
96
   VIA_DEVICE_MATCH (PCI_CHIP_VT3327, 0 ),
 
97
   VIA_DEVICE_MATCH (PCI_CHIP_VT3353, 0 ),
 
98
   VIA_DEVICE_MATCH (PCI_CHIP_VT3409, 0 ),
 
99
   VIA_DEVICE_MATCH (PCI_CHIP_VT3410, 0 ),
 
100
    { 0, 0, 0 },
 
101
};
 
102
 
 
103
#endif /* HAVE_PCIACCESS */
 
104
 
 
105
_X_EXPORT DriverRec VIA = {
 
106
    VIA_VERSION,
 
107
    DRIVER_NAME,
 
108
    VIAIdentify,
 
109
#ifdef HAVE_PCIACCESS
 
110
    NULL,
 
111
#else
 
112
    VIAProbe,
 
113
#endif
 
114
    VIAAvailableOptions,
 
115
    NULL,
 
116
    0,
 
117
    NULL,
 
118
#ifdef HAVE_PCIACCESS
 
119
    via_device_match,
 
120
    via_pci_probe
 
121
#endif
 
122
};
 
123
 
 
124
/* Supported chipsets */
 
125
static SymTabRec VIAChipsets[] = {
 
126
    {VIA_CLE266,   "CLE266"},
 
127
    {VIA_KM400,    "KM400/KN400"},
 
128
    {VIA_K8M800,   "K8M800/K8N800"},
 
129
    {VIA_PM800,    "PM800/PM880/CN400"},
 
130
    {VIA_VM800,    "VM800/P4M800Pro/VN800/CN700"},
 
131
    {VIA_CX700,    "CX700/VX700"},
 
132
    {VIA_K8M890,   "K8M890/K8N890"},
 
133
    {VIA_P4M890,   "P4M890"},
 
134
    {VIA_P4M900,   "P4M900/VN896/CN896"},
 
135
    {VIA_VX800,    "VX800/VX820"},
 
136
    {VIA_VX855,    "VX855/VX875"},
 
137
    {VIA_VX900,    "VX900"},
 
138
    {-1,            NULL }
 
139
};
 
140
 
 
141
/* Mapping a PCI device ID to a chipset family identifier. */
 
142
static PciChipsets VIAPciChipsets[] = {
 
143
    {VIA_CLE266,   PCI_CHIP_CLE3122,   VIA_RES_SHARED},
 
144
    {VIA_KM400,    PCI_CHIP_VT3205,    VIA_RES_SHARED},
 
145
    {VIA_K8M800,   PCI_CHIP_VT3204,    VIA_RES_SHARED},
 
146
    {VIA_PM800,    PCI_CHIP_VT3259,    VIA_RES_SHARED},
 
147
    {VIA_VM800,    PCI_CHIP_VT3314,    VIA_RES_SHARED},
 
148
    {VIA_CX700,    PCI_CHIP_VT3324,    VIA_RES_SHARED},
 
149
    {VIA_K8M890,   PCI_CHIP_VT3336,    VIA_RES_SHARED},
 
150
    {VIA_P4M890,   PCI_CHIP_VT3327,    VIA_RES_SHARED},
 
151
    {VIA_P4M900,   PCI_CHIP_VT3364,    VIA_RES_SHARED},
 
152
    {VIA_VX800,    PCI_CHIP_VT3353,    VIA_RES_SHARED},
 
153
    {VIA_VX855,    PCI_CHIP_VT3409,    VIA_RES_SHARED},
 
154
    {VIA_VX900,    PCI_CHIP_VT3410,    VIA_RES_SHARED},
 
155
    {-1,           -1,                 VIA_RES_UNDEF}
 
156
};
 
157
 
 
158
typedef enum
 
159
{
 
160
#ifdef HAVE_DEBUG
 
161
    OPTION_PRINTVGAREGS,
 
162
    OPTION_PRINTTVREGS,
 
163
    OPTION_I2CSCAN,
 
164
#endif
 
165
    OPTION_VBEMODES,
 
166
    OPTION_NOACCEL,
 
167
    OPTION_ACCELMETHOD,
 
168
    OPTION_EXA_NOCOMPOSITE,
 
169
    OPTION_EXA_SCRATCH_SIZE,
 
170
    OPTION_SWCURSOR,
 
171
    OPTION_SHADOW_FB,
 
172
    OPTION_ROTATION_TYPE,
 
173
    OPTION_ROTATE,
 
174
    OPTION_VIDEORAM,
 
175
    OPTION_ACTIVEDEVICE,
 
176
    OPTION_I2CDEVICES,
 
177
    OPTION_BUSWIDTH,
 
178
    OPTION_CENTER,
 
179
    OPTION_PANELSIZE,
 
180
    OPTION_FORCEPANEL,
 
181
    OPTION_TVDOTCRAWL,
 
182
    OPTION_TVTYPE,
 
183
    OPTION_TVOUTPUT,
 
184
    OPTION_TVDIPORT,
 
185
    OPTION_DISABLEVQ,
 
186
    OPTION_DISABLEIRQ,
 
187
    OPTION_TVDEFLICKER,
 
188
    OPTION_AGP_DMA,
 
189
    OPTION_2D_DMA,
 
190
    OPTION_XV_DMA,
 
191
    OPTION_VBE_SAVERESTORE,
 
192
    OPTION_MAX_DRIMEM,
 
193
    OPTION_AGPMEM,
 
194
    OPTION_DISABLE_XV_BW_CHECK,
 
195
    OPTION_MODE_SWITCH_METHOD
 
196
} VIAOpts;
 
197
 
 
198
static OptionInfoRec VIAOptions[] = {
 
199
#ifdef HAVE_DEBUG /* Don't document these three. */
 
200
    {OPTION_PRINTVGAREGS,        "PrintVGARegs",     OPTV_BOOLEAN, {0}, FALSE},
 
201
    {OPTION_PRINTTVREGS,         "PrintTVRegs",      OPTV_BOOLEAN, {0}, FALSE},
 
202
    {OPTION_I2CSCAN,             "I2CScan",          OPTV_BOOLEAN, {0}, FALSE},
 
203
#endif
 
204
    {OPTION_VBEMODES,            "VBEModes",         OPTV_BOOLEAN, {0}, FALSE},
 
205
    {OPTION_NOACCEL,             "NoAccel",          OPTV_BOOLEAN, {0}, FALSE},
 
206
    {OPTION_ACCELMETHOD,         "AccelMethod",      OPTV_STRING,  {0}, FALSE},
 
207
    {OPTION_EXA_NOCOMPOSITE,     "ExaNoComposite",   OPTV_BOOLEAN, {0}, FALSE},
 
208
    {OPTION_EXA_SCRATCH_SIZE,    "ExaScratchSize",   OPTV_INTEGER, {0}, FALSE},
 
209
    {OPTION_SWCURSOR,            "SWCursor",         OPTV_BOOLEAN, {0}, FALSE},
 
210
    {OPTION_SHADOW_FB,           "ShadowFB",         OPTV_BOOLEAN, {0}, FALSE},
 
211
    {OPTION_ROTATION_TYPE,       "RotationType",     OPTV_ANYSTR,  {0}, FALSE},
 
212
    {OPTION_ROTATE,              "Rotate",           OPTV_ANYSTR,  {0}, FALSE},
 
213
    {OPTION_VIDEORAM,            "VideoRAM",         OPTV_INTEGER, {0}, FALSE},
 
214
    {OPTION_ACTIVEDEVICE,        "ActiveDevice",     OPTV_ANYSTR,  {0}, FALSE},
 
215
    {OPTION_TVDOTCRAWL,          "TVDotCrawl",       OPTV_BOOLEAN, {0}, FALSE},
 
216
    {OPTION_TVDEFLICKER,         "TVDeflicker",      OPTV_INTEGER, {0}, FALSE},
 
217
    {OPTION_TVTYPE,              "TVType",           OPTV_ANYSTR,  {0}, FALSE},
 
218
    {OPTION_TVOUTPUT,            "TVOutput",         OPTV_ANYSTR,  {0}, FALSE},
 
219
    {OPTION_TVDIPORT,            "TVPort",           OPTV_ANYSTR,  {0}, FALSE},
 
220
    {OPTION_DISABLEVQ,           "DisableVQ",        OPTV_BOOLEAN, {0}, FALSE},
 
221
    {OPTION_DISABLEIRQ,          "DisableIRQ",       OPTV_BOOLEAN, {0}, FALSE},
 
222
    {OPTION_AGP_DMA,             "EnableAGPDMA",     OPTV_BOOLEAN, {0}, FALSE},
 
223
    {OPTION_2D_DMA,              "NoAGPFor2D",       OPTV_BOOLEAN, {0}, FALSE},
 
224
    {OPTION_XV_DMA,              "NoXVDMA",          OPTV_BOOLEAN, {0}, FALSE},
 
225
    {OPTION_VBE_SAVERESTORE,     "VbeSaveRestore",   OPTV_BOOLEAN, {0}, FALSE},
 
226
    {OPTION_DISABLE_XV_BW_CHECK, "DisableXvBWCheck", OPTV_BOOLEAN, {0}, FALSE},
 
227
    {OPTION_MODE_SWITCH_METHOD,  "ModeSwitchMethod", OPTV_ANYSTR,  {0}, FALSE},
 
228
    {OPTION_MAX_DRIMEM,          "MaxDRIMem",        OPTV_INTEGER, {0}, FALSE},
 
229
    {OPTION_AGPMEM,              "AGPMem",           OPTV_INTEGER, {0}, FALSE},
 
230
    {OPTION_I2CDEVICES,          "I2CDevices",       OPTV_ANYSTR,  {0}, FALSE},
 
231
    {-1,                         NULL,               OPTV_NONE,    {0}, FALSE}
 
232
};
 
233
 
 
234
#ifdef XFree86LOADER
 
235
static MODULESETUPPROTO(VIASetup);
 
236
 
 
237
static XF86ModuleVersionInfo VIAVersRec = {
 
238
    "openchrome",
 
239
    "http://openchrome.org/",
 
240
    MODINFOSTRING1,
 
241
    MODINFOSTRING2,
 
242
#ifdef XORG_VERSION_CURRENT
 
243
    XORG_VERSION_CURRENT,
 
244
#else
 
245
    XF86_VERSION_CURRENT,
 
246
#endif
 
247
    VIA_MAJOR_VERSION, VIA_MINOR_VERSION, VIA_PATCHLEVEL,
 
248
    ABI_CLASS_VIDEODRV,
 
249
    ABI_VIDEODRV_VERSION,
 
250
    MOD_CLASS_VIDEODRV,
 
251
    {0, 0, 0, 0}
 
252
};
 
253
 
 
254
_X_EXPORT XF86ModuleData openchromeModuleData = { &VIAVersRec, VIASetup, NULL };
 
255
 
 
256
static pointer
 
257
VIASetup(pointer module, pointer opts, int *errmaj, int *errmin)
 
258
{
 
259
    static Bool setupDone = FALSE;
 
260
 
 
261
    /* Only be loaded once */
 
262
    if (!setupDone) {
 
263
        setupDone = TRUE;
 
264
        xf86AddDriver(&VIA, module,
 
265
#ifdef HAVE_PCIACCESS
 
266
                     HaveDriverFuncs
 
267
#else
 
268
                     0
 
269
#endif
 
270
                     );
 
271
 
 
272
        return (pointer) 1;
 
273
    } else {
 
274
        if (errmaj)
 
275
            *errmaj = LDR_ONCEONLY;
 
276
 
 
277
        return NULL;
 
278
    }
 
279
} /* VIASetup */
 
280
 
 
281
#endif /* XFree86LOADER */
 
282
 
 
283
static const OptionInfoRec *
 
284
VIAAvailableOptions(int chipid, int busid)
 
285
{
 
286
    return VIAOptions;
 
287
}
 
288
 
 
289
static Bool
 
290
VIASwitchMode(SWITCH_MODE_ARGS_DECL)
 
291
{
 
292
    SCRN_INFO_PTR(arg);
 
293
 
 
294
    return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
 
295
}
 
296
 
 
297
static void
 
298
VIAAdjustFrame(ADJUST_FRAME_ARGS_DECL)
 
299
{
 
300
    SCRN_INFO_PTR(arg);
 
301
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
302
    int i;
 
303
 
 
304
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAAdjustFrame %dx%d\n", x, y));
 
305
 
 
306
    for (i = 0; i < xf86_config->num_crtc; i++) {
 
307
        xf86CrtcPtr crtc = xf86_config->crtc[i];
 
308
 
 
309
        xf86CrtcSetOrigin(crtc, x, y);
 
310
    }
 
311
}
 
312
 
 
313
static Bool
 
314
VIAEnterVT_internal(ScrnInfoPtr pScrn, int flags)
 
315
{
 
316
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
317
    VIAPtr pVia = VIAPTR(pScrn);
 
318
    int i;
 
319
 
 
320
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAEnterVT\n"));
 
321
 
 
322
    for (i = 0; i < xf86_config->num_crtc; i++) {
 
323
        xf86CrtcPtr crtc = xf86_config->crtc[i];
 
324
 
 
325
        if (crtc->funcs->save)
 
326
            crtc->funcs->save(crtc);
 
327
    }
 
328
 
 
329
    for (i = 0; i < xf86_config->num_output; i++) {
 
330
        xf86OutputPtr output = xf86_config->output[i];
 
331
 
 
332
        if (output->funcs->save)
 
333
            output->funcs->save(output);
 
334
    }
 
335
 
 
336
    if (!xf86SetDesiredModes(pScrn))
 
337
        return FALSE;
 
338
 
 
339
    if (!flags) {
 
340
        /* Restore video status. */
 
341
        if (!pVia->IsSecondary)
 
342
            viaRestoreVideo(pScrn);
 
343
 
 
344
#ifdef HAVE_DRI
 
345
        if (pVia->directRenderingType == DRI_1) {
 
346
            kickVblank(pScrn);
 
347
            VIADRIRingBufferInit(pScrn);
 
348
            viaDRIOffscreenRestore(pScrn);
 
349
            DRIUnlock(xf86ScrnToScreen(pScrn));
 
350
        }
 
351
#endif
 
352
    }
 
353
    return TRUE;
 
354
}
 
355
 
 
356
static Bool
 
357
VIAEnterVT(VT_FUNC_ARGS_DECL)
 
358
{
 
359
    SCRN_INFO_PTR(arg);
 
360
    return VIAEnterVT_internal(pScrn, 0);
 
361
}
 
362
 
 
363
static void
 
364
VIALeaveVT(VT_FUNC_ARGS_DECL)
 
365
{
 
366
    SCRN_INFO_PTR(arg);
 
367
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
368
    VIAPtr pVia = VIAPTR(pScrn);
 
369
    int i;
 
370
 
 
371
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIALeaveVT\n"));
 
372
 
 
373
#ifdef HAVE_DRI
 
374
    if (pVia->directRenderingType == DRI_1) {
 
375
        volatile drm_via_sarea_t *saPriv = (drm_via_sarea_t *) DRIGetSAREAPrivate(pScrn->pScreen);
 
376
 
 
377
        DRILock(xf86ScrnToScreen(pScrn), 0);
 
378
        saPriv->ctxOwner = ~0;
 
379
 
 
380
        viaAccelSync(pScrn);
 
381
 
 
382
        VIADRIRingBufferCleanup(pScrn);
 
383
        viaDRIOffscreenSave(pScrn);
 
384
 
 
385
        if (pVia->VQEnable)
 
386
            viaDisableVQ(pScrn);
 
387
    }
 
388
#endif
 
389
 
 
390
    /* Save video status and turn off all video activities. */
 
391
    if (!pVia->IsSecondary)
 
392
        viaSaveVideo(pScrn);
 
393
 
 
394
    for (i = 0; i < xf86_config->num_output; i++) {
 
395
        xf86OutputPtr output = xf86_config->output[i];
 
396
 
 
397
        if (output->funcs->restore)
 
398
            output->funcs->restore(output);
 
399
    }
 
400
 
 
401
    for (i = 0; i < xf86_config->num_crtc; i++) {
 
402
        xf86CrtcPtr crtc = xf86_config->crtc[i];
 
403
 
 
404
        if (crtc->funcs->restore)
 
405
            crtc->funcs->restore(crtc);
 
406
    }
 
407
    pScrn->vtSema = FALSE;
 
408
}
 
409
 
 
410
static void
 
411
VIAFreeRec(ScrnInfoPtr pScrn)
 
412
{
 
413
    VIAPtr pVia = VIAPTR(pScrn);
 
414
 
 
415
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAFreeRec\n"));
 
416
    if (!pScrn->driverPrivate)
 
417
        return;
 
418
 
 
419
    VIABIOSInfoPtr pBIOSInfo = pVia->pBIOSInfo;
 
420
 
 
421
    if (pBIOSInfo) {
 
422
        if (pBIOSInfo->TVI2CDev)
 
423
            xf86DestroyI2CDevRec(pBIOSInfo->TVI2CDev, TRUE);
 
424
 
 
425
        pVia->pBIOSInfo = NULL;
 
426
        free(pBIOSInfo);
 
427
    }
 
428
 
 
429
    if (VIAPTR(pScrn)->pVbe)
 
430
        vbeFree(VIAPTR(pScrn)->pVbe);
 
431
 
 
432
    if (pVia->VideoRegs)
 
433
        free(pVia->VideoRegs);
 
434
 
 
435
    VIAUnmapMem(pScrn);
 
436
 
 
437
    free(pScrn->driverPrivate);
 
438
    pScrn->driverPrivate = NULL;
 
439
} /* VIAFreeRec */
 
440
 
 
441
/*
 
442
 * This only gets called when a screen is being deleted.  It does not
 
443
 * get called routinely at the end of a server generation.
 
444
 */
 
445
static void
 
446
VIAFreeScreen(FREE_SCREEN_ARGS_DECL)
 
447
{
 
448
    SCRN_INFO_PTR(arg);
 
449
    VIAPtr pVia = VIAPTR(pScrn);
 
450
 
 
451
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAFreeScreen\n"));
 
452
 
 
453
    if (pVia->directRenderingType != DRI_2)
 
454
        VIAUnmapMem(pScrn);
 
455
 
 
456
    VIAFreeRec(pScrn);
 
457
 
 
458
    if (!pVia->KMS && xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
 
459
        vgaHWFreeHWRec(pScrn);
 
460
}
 
461
 
 
462
static void
 
463
VIAIdentify(int flags)
 
464
{
 
465
    xf86PrintChipsets("OPENCHROME", "Driver for VIA Chrome chipsets",
 
466
                      VIAChipsets);
 
467
}
 
468
 
 
469
#ifdef HAVE_PCIACCESS
 
470
static Bool
 
471
via_pci_probe(DriverPtr driver, int entity_num,
 
472
              struct pci_device *device, intptr_t match_data)
 
473
{
 
474
    ScrnInfoPtr scrn = NULL;
 
475
    EntityInfoPtr entity;
 
476
 
 
477
    scrn = xf86ConfigPciEntity(scrn, 0, entity_num, VIAPciChipsets,
 
478
                               NULL, NULL, NULL, NULL, NULL);
 
479
 
 
480
    if (scrn != NULL) {
 
481
        scrn->driverVersion = VIA_VERSION;
 
482
        scrn->driverName = DRIVER_NAME;
 
483
        scrn->name = "CHROME";
 
484
        scrn->Probe = NULL;
 
485
 
 
486
        entity = xf86GetEntityInfo(entity_num);
 
487
 
 
488
        scrn->PreInit = VIAPreInit;
 
489
        scrn->ScreenInit = VIAScreenInit;
 
490
        scrn->SwitchMode = VIASwitchMode;
 
491
        scrn->AdjustFrame = VIAAdjustFrame;
 
492
                scrn->EnterVT = VIAEnterVT;
 
493
                scrn->LeaveVT = VIALeaveVT;
 
494
                scrn->FreeScreen = VIAFreeScreen;
 
495
 
 
496
        xf86Msg(X_NOTICE,
 
497
                "VIA Technologies does not support this driver in any way.\n");
 
498
        xf86Msg(X_NOTICE,
 
499
                "For support, please refer to http://www.openchrome.org/.\n");
 
500
#ifdef BUILDCOMMENT
 
501
        xf86Msg(X_NOTICE, BUILDCOMMENT"\n");
 
502
#endif
 
503
    }
 
504
    return scrn != NULL;
 
505
}
 
506
#else /* !HAVE_PCIACCESS */
 
507
static Bool
 
508
VIAProbe(DriverPtr drv, int flags)
 
509
{
 
510
    GDevPtr *devSections;
 
511
    int *usedChips;
 
512
    int numDevSections;
 
513
    int numUsed;
 
514
    Bool foundScreen = FALSE;
 
515
    int i;
 
516
 
 
517
    /* sanity checks */
 
518
    if ((numDevSections = xf86MatchDevice(DRIVER_NAME, &devSections)) <= 0)
 
519
        return FALSE;
 
520
 
 
521
    if (xf86GetPciVideoInfo() == NULL)
 
522
        return FALSE;
 
523
 
 
524
    numUsed = xf86MatchPciInstances(DRIVER_NAME,
 
525
                                    PCI_VIA_VENDOR_ID,
 
526
                                    VIAChipsets,
 
527
                                    VIAPciChipsets,
 
528
                                    devSections,
 
529
                                    numDevSections,
 
530
                                    drv,
 
531
                                    &usedChips);
 
532
    free(devSections);
 
533
 
 
534
    if (numUsed <= 0)
 
535
        return FALSE;
 
536
 
 
537
    xf86Msg(X_NOTICE,
 
538
            "VIA Technologies does not support this driver in any way.\n");
 
539
    xf86Msg(X_NOTICE, "For support, please refer to http://openchrome.org/.\n");
 
540
 
 
541
#ifdef BUILDCOMMENT
 
542
    xf86Msg(X_NOTICE, BUILDCOMMENT"\n");
 
543
#endif
 
544
 
 
545
    if (flags & PROBE_DETECT) {
 
546
        foundScreen = TRUE;
 
547
    } else {
 
548
        for (i = 0; i < numUsed; i++) {
 
549
            ScrnInfoPtr pScrn = xf86AllocateScreen(drv, 0);
 
550
            EntityInfoPtr pEnt;
 
551
 
 
552
            if ((pScrn = xf86ConfigPciEntity(pScrn, 0, usedChips[i],
 
553
                                             VIAPciChipsets, 0, 0, 0, 0, 0))) {
 
554
                                pScrn->driverVersion = VIA_VERSION;
 
555
                                pScrn->driverName = DRIVER_NAME;
 
556
                                pScrn->name = "CHROME";
 
557
                                pScrn->Probe = VIAProbe;
 
558
                                pScrn->PreInit = VIAPreInit;
 
559
                                pScrn->ScreenInit = VIAScreenInit;
 
560
                                pScrn->SwitchMode = VIASwitchMode;
 
561
                                pScrn->AdjustFrame = VIAAdjustFrame;
 
562
                                pScrn->EnterVT = VIAEnterVT;
 
563
                                pScrn->LeaveVT = VIALeaveVT;
 
564
                                pScrn->FreeScreen = VIAFreeScreen;
 
565
                                foundScreen = TRUE;
 
566
            }
 
567
#if 0
 
568
            xf86ConfigActivePciEntity(pScrn,
 
569
                                      usedChips[i],
 
570
                                      VIAPciChipsets,
 
571
                                      NULL,
 
572
                                      NULL,
 
573
                                      NULL,
 
574
                                      NULL,
 
575
                                      NULL);
 
576
#endif
 
577
            pEnt = xf86GetEntityInfo(usedChips[i]);
 
578
 
 
579
            /* CLE266 supports dual-head; mark the entity as sharable. */
 
580
            if (pEnt->chipset == VIA_CLE266 || pEnt->chipset == VIA_KM400) {
 
581
                static int instance = 0;
 
582
                DevUnion *pPriv;
 
583
 
 
584
                xf86SetEntitySharable(usedChips[i]);
 
585
                xf86SetEntityInstanceForScreen(pScrn,
 
586
                                               pScrn->entityList[0], instance);
 
587
 
 
588
                if (gVIAEntityIndex < 0) {
 
589
                    gVIAEntityIndex = xf86AllocateEntityPrivateIndex();
 
590
                    pPriv = xf86GetEntityPrivate(pScrn->entityList[0],
 
591
                                                 gVIAEntityIndex);
 
592
 
 
593
                    if (!pPriv->ptr) {
 
594
                        VIAEntPtr pVIAEnt;
 
595
 
 
596
                        pPriv->ptr = xnfcalloc(sizeof(VIAEntRec), 1);
 
597
                        pVIAEnt = pPriv->ptr;
 
598
                        pVIAEnt->IsDRIEnabled = FALSE;
 
599
                        pVIAEnt->BypassSecondary = FALSE;
 
600
                        pVIAEnt->HasSecondary = FALSE;
 
601
                        pVIAEnt->IsSecondaryRestored = FALSE;
 
602
                    }
 
603
                }
 
604
                instance++;
 
605
            }
 
606
            free(pEnt);
 
607
        }
 
608
    }
 
609
 
 
610
    free(usedChips);
 
611
 
 
612
    return foundScreen;
 
613
 
 
614
} /* VIAProbe */
 
615
#endif /* !HAVE_PCIACCESS */
 
616
 
 
617
static int
 
618
LookupChipSet(PciChipsets *pset, int chipSet)
 
619
{
 
620
    while (pset->numChipset >= 0) {
 
621
        if (pset->numChipset == chipSet)
 
622
            return pset->PCIid;
 
623
        pset++;
 
624
    }
 
625
    return -1;
 
626
}
 
627
 
 
628
 
 
629
static int
 
630
LookupChipID(PciChipsets *pset, int ChipID)
 
631
{
 
632
    /* Is there a function to do this for me? */
 
633
    while (pset->numChipset >= 0) {
 
634
        if (pset->PCIid == ChipID)
 
635
            return pset->numChipset;
 
636
 
 
637
        pset++;
 
638
    }
 
639
 
 
640
    return -1;
 
641
}
 
642
 
 
643
static Bool
 
644
VIASetupDefaultOptions(ScrnInfoPtr pScrn)
 
645
{
 
646
    VIAPtr pVia = VIAPTR(pScrn);
 
647
    VIABIOSInfoPtr pBIOSInfo = pVia->pBIOSInfo;
 
648
 
 
649
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIASetupDefaultOptions - Setting up default chipset options.\n"));
 
650
 
 
651
    pVia->shadowFB = FALSE;
 
652
    pVia->NoAccel = FALSE;
 
653
    pVia->noComposite = FALSE;
 
654
    pVia->useEXA = TRUE;
 
655
    pVia->exaScratchSize = VIA_SCRATCH_SIZE / 1024;
 
656
    pVia->drmmode.hwcursor = TRUE;
 
657
    pVia->VQEnable = TRUE;
 
658
    pVia->DRIIrqEnable = TRUE;
 
659
    pVia->agpEnable = TRUE;
 
660
    pVia->dma2d = TRUE;
 
661
    pVia->dmaXV = TRUE;
 
662
    pVia->useVBEModes = FALSE;
 
663
    pVia->vbeSR = FALSE;
 
664
#ifdef HAVE_DEBUG
 
665
    pVia->disableXvBWCheck = FALSE;
 
666
#endif
 
667
    pVia->maxDriSize = 0;
 
668
    pVia->agpMem = AGP_SIZE / 1024;
 
669
    pVia->ActiveDevice = 0x00;
 
670
    pVia->I2CDevices = VIA_I2C_BUS1 | VIA_I2C_BUS2 | VIA_I2C_BUS3;
 
671
    pVia->VideoEngine = VIDEO_ENGINE_CLE;
 
672
#ifdef HAVE_DEBUG
 
673
    pVia->PrintVGARegs = FALSE;
 
674
#endif
 
675
 
 
676
    /* Disable vertical interpolation because the size of */
 
677
    /* line buffer (limited to 800) is too small to do interpolation. */
 
678
    pVia->swov.maxWInterp = 800;
 
679
    pVia->swov.maxHInterp = 600;
 
680
    pVia->useLegacyVBE = TRUE;
 
681
 
 
682
    pVia->UseLegacyModeSwitch = FALSE;
 
683
    pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP1;
 
684
 
 
685
    switch (pVia->Chipset) {
 
686
        case VIA_CLE266:
 
687
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP0;
 
688
            pVia->UseLegacyModeSwitch = TRUE;
 
689
            break;
 
690
        case VIA_KM400:
 
691
            /* IRQ is not broken on KM400A, but testing (pVia->ChipRev < 0x80)
 
692
             * is not enough to make sure we have an older, broken KM400. */
 
693
            pVia->DRIIrqEnable = FALSE;
 
694
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP0;
 
695
            break;
 
696
        case VIA_K8M800:
 
697
            pVia->DRIIrqEnable = FALSE;
 
698
            break;
 
699
        case VIA_PM800:
 
700
            /* Use new mode switch to resolve many resolution and display bugs (switch to console) */
 
701
            /* FIXME The video playing (XV) is not working correctly after turn on new mode switch */
 
702
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
703
            break;
 
704
        case VIA_VM800:
 
705
            /* New mode switch resolve bug with gamma set #282 */
 
706
            /* and with Xv after hibernate #240                */
 
707
            break;
 
708
        case VIA_CX700:
 
709
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
710
            pVia->swov.maxWInterp = 1920;
 
711
            pVia->swov.maxHInterp = 1080;
 
712
            break;
 
713
        case VIA_K8M890:
 
714
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
715
            pVia->agpEnable = FALSE;
 
716
            pVia->dmaXV = FALSE;
 
717
            break;
 
718
        case VIA_P4M890:
 
719
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
720
            pVia->dmaXV = FALSE;
 
721
            break;
 
722
        case VIA_P4M900:
 
723
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
724
            pVia->agpEnable = FALSE;
 
725
            pVia->useLegacyVBE = FALSE;
 
726
            /* FIXME: this needs to be tested */
 
727
            pVia->dmaXV = FALSE;
 
728
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP0;
 
729
            break;
 
730
        case VIA_VX800:
 
731
        case VIA_VX855:
 
732
        case VIA_VX900:
 
733
            pVia->VideoEngine = VIDEO_ENGINE_CME;
 
734
            pVia->agpEnable = FALSE;
 
735
            pVia->dmaXV = FALSE;
 
736
            break;
 
737
    }
 
738
 
 
739
    return TRUE;
 
740
}
 
741
 
 
742
Bool
 
743
VIAGetRec(ScrnInfoPtr pScrn)
 
744
{
 
745
    Bool ret = FALSE;
 
746
    VIAPtr pVia;
 
747
 
 
748
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAGetRec\n"));
 
749
 
 
750
    if (pScrn->driverPrivate)
 
751
        return TRUE;
 
752
 
 
753
    /* allocate VIARec */
 
754
    pVia = (VIARec *) xnfcalloc(sizeof(VIARec), 1);
 
755
    if (pVia) {
 
756
        pVia->pBIOSInfo = xnfcalloc(sizeof(VIABIOSInfoRec), 1);
 
757
        VIABIOSInfoPtr pBIOSInfo = pVia->pBIOSInfo;
 
758
 
 
759
        if (pBIOSInfo) {
 
760
            pBIOSInfo->TVI2CDev = NULL;
 
761
 
 
762
            pVia->VideoRegs = (video_via_regs *) xnfcalloc(sizeof(video_via_regs), 1);
 
763
            if (!pVia->VideoRegs) {
 
764
                free(pBIOSInfo);
 
765
                free(pVia);
 
766
            } else {
 
767
                pScrn->driverPrivate = pVia;
 
768
                ret = TRUE;
 
769
            }
 
770
        }
 
771
    }
 
772
    return ret;
 
773
} /* VIAGetRec */
 
774
 
 
775
static int
 
776
map_legacy_formats(int bpp, int depth)
 
777
{
 
778
        int fmt = DRM_FORMAT_XRGB8888;
 
779
 
 
780
        switch (bpp) {
 
781
        case 8:
 
782
                fmt = DRM_FORMAT_C8;
 
783
                break;
 
784
        case 16:
 
785
                if (depth == 15)
 
786
                        fmt = DRM_FORMAT_XRGB1555;
 
787
                else
 
788
                        fmt = DRM_FORMAT_RGB565;
 
789
                break;
 
790
        case 24:
 
791
                fmt = DRM_FORMAT_RGB888;
 
792
                break;
 
793
        case 32:
 
794
                if (depth == 24)
 
795
                        fmt = DRM_FORMAT_XRGB8888;
 
796
                else if (depth == 30)
 
797
                        fmt = DRM_FORMAT_XRGB2101010;
 
798
        default:
 
799
                break;
 
800
        }
 
801
        return fmt;
 
802
}
 
803
 
 
804
static Bool
 
805
via_xf86crtc_resize(ScrnInfoPtr scrn, int width, int height)
 
806
{
 
807
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
 
808
    struct buffer_object *old_front = NULL, *new_front = NULL;
 
809
    int old_width, old_height, old_dwidth, format;
 
810
    int cpp = (scrn->bitsPerPixel + 7) >> 3, i;
 
811
    ScreenPtr screen = scrn->pScreen;
 
812
    VIAPtr pVia = VIAPTR(scrn);
 
813
    void *new_pixels = NULL;
 
814
    uint32_t old_fb_id;
 
815
    Bool ret = FALSE;
 
816
    PixmapPtr ppix;
 
817
 
 
818
    if (scrn->virtualX == width && scrn->virtualY == height)
 
819
        return TRUE;
 
820
 
 
821
    format = map_legacy_formats(scrn->bitsPerPixel, scrn->depth);
 
822
    new_front = drm_bo_alloc_surface(scrn, width, height, format,
 
823
                                            16, TTM_PL_FLAG_VRAM);
 
824
    if (!new_front)
 
825
        goto fail;
 
826
 
 
827
    xf86DrvMsg(scrn->scrnIndex, X_INFO,
 
828
                "Allocate new frame buffer %dx%d stride %d\n",
 
829
                width, height, new_front->pitch);
 
830
 
 
831
    new_pixels = drm_bo_map(scrn, new_front);
 
832
    if (!new_pixels)
 
833
        goto fail;
 
834
 
 
835
    if (pVia->shadowFB) {
 
836
        new_pixels = malloc(height * new_front->pitch);
 
837
        if (!new_pixels)
 
838
            goto fail;
 
839
        free(pVia->ShadowPtr);
 
840
        pVia->ShadowPtr = new_pixels;
 
841
    }
 
842
 
 
843
    ppix = screen->GetScreenPixmap(screen);
 
844
    if (!screen->ModifyPixmapHeader(ppix, width, height, -1, -1,
 
845
                                    new_front->pitch,
 
846
                                    new_pixels))
 
847
        goto fail;
 
848
 
 
849
#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,99,1,0)
 
850
    scrn->pixmapPrivate.ptr = ppix->devPrivate.ptr;
 
851
#endif
 
852
 
 
853
    scrn->virtualX = width;
 
854
    scrn->virtualY = height;
 
855
    scrn->displayWidth = new_front->pitch / cpp;
 
856
 
 
857
    for (i = 0; i < xf86_config->num_crtc; i++) {
 
858
        xf86CrtcPtr crtc = xf86_config->crtc[i];
 
859
        drmmode_crtc_private_ptr drmmode_crtc;
 
860
        drmmode_ptr drmmode;
 
861
 
 
862
        if (!crtc->enabled || !crtc->driver_private)
 
863
            continue;
 
864
 
 
865
        drmmode_crtc = crtc->driver_private;
 
866
        drmmode = drmmode_crtc->drmmode;
 
867
 
 
868
        old_front = drmmode->front_bo;
 
869
        old_fb_id = drmmode->fb_id;
 
870
 
 
871
        drmmode->front_bo = new_front;
 
872
        drmmode->fb_id = 0;
 
873
 
 
874
        ret = xf86CrtcSetMode(crtc, &crtc->mode, crtc->rotation,
 
875
                                  crtc->x, crtc->y);
 
876
        if (!ret) {
 
877
            xf86DrvMsg(scrn->scrnIndex, X_INFO,
 
878
                "SetMode !ret so we reset front_bo\n");
 
879
            drmmode->front_bo = old_front;
 
880
            drmmode->fb_id = old_fb_id;
 
881
            break;
 
882
#ifdef HAVE_DRI
 
883
        } else {
 
884
            xf86DrvMsg(scrn->scrnIndex, X_INFO,
 
885
                "SetMode ret so we cleanup old front_bo\n");
 
886
            if (pVia->KMS && old_fb_id)
 
887
                drmModeRmFB(drmmode->fd, old_fb_id);
 
888
#endif
 
889
        }
 
890
    }
 
891
 
 
892
 
 
893
    if (ret) {
 
894
        xf86DrvMsg(scrn->scrnIndex, X_INFO,
 
895
                   "More cleanup old front_bo\n");
 
896
        drm_bo_unmap(scrn, old_front);
 
897
        drm_bo_free(scrn, old_front);
 
898
        return ret;
 
899
    }
 
900
 
 
901
fail:
 
902
    if (new_front) {
 
903
        drm_bo_unmap(scrn, new_front);
 
904
        drm_bo_free(scrn, new_front);
 
905
    }
 
906
    scrn->virtualY = old_height;
 
907
    scrn->virtualX = old_width;
 
908
    scrn->displayWidth = old_dwidth;
 
909
    return FALSE;
 
910
}
 
911
 
 
912
static const
 
913
xf86CrtcConfigFuncsRec via_xf86crtc_config_funcs = {
 
914
    via_xf86crtc_resize
 
915
};
 
916
 
 
917
static Bool
 
918
VIAPreInit(ScrnInfoPtr pScrn, int flags)
 
919
{
 
920
    XF86OptionPtr option = xf86NewOption("MigrationHeuristic", "greedy");
 
921
    EntityInfoPtr pEnt;
 
922
    VIAPtr pVia;
 
923
    VIABIOSInfoPtr pBIOSInfo;
 
924
    MessageType from = X_DEFAULT;
 
925
    char *s = NULL;
 
926
#ifdef HAVE_DRI
 
927
    char *busId = NULL;
 
928
    drmVersionPtr drmVer;
 
929
#endif
 
930
 
 
931
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAPreInit\n"));
 
932
 
 
933
    if (pScrn->numEntities > 1)
 
934
        return FALSE;
 
935
 
 
936
    if (flags & PROBE_DETECT)
 
937
        return FALSE;
 
938
 
 
939
    if (!VIAGetRec(pScrn)) {
 
940
        return FALSE;
 
941
    }
 
942
 
 
943
    pVia = VIAPTR(pScrn);
 
944
    pVia->IsSecondary = FALSE;
 
945
    pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
 
946
#ifndef HAVE_PCIACCESS
 
947
    if (pEnt->resources) {
 
948
        free(pEnt);
 
949
        VIAFreeRec(pScrn);
 
950
        return FALSE;
 
951
    }
 
952
#endif
 
953
 
 
954
    pVia->EntityIndex = pEnt->index;
 
955
 
 
956
    if (xf86IsEntityShared(pScrn->entityList[0])) {
 
957
        if (xf86IsPrimInitDone(pScrn->entityList[0])) {
 
958
            DevUnion *pPriv;
 
959
            VIAEntPtr pVIAEnt;
 
960
            VIAPtr pVia1;
 
961
 
 
962
            pVia->IsSecondary = TRUE;
 
963
            pPriv = xf86GetEntityPrivate(pScrn->entityList[0], gVIAEntityIndex);
 
964
            pVIAEnt = pPriv->ptr;
 
965
            if (pVIAEnt->BypassSecondary) {
 
966
                free(pEnt);
 
967
                VIAFreeRec(pScrn);
 
968
                return FALSE;
 
969
            }
 
970
            pVIAEnt->pSecondaryScrn = pScrn;
 
971
            pVIAEnt->HasSecondary = TRUE;
 
972
            pVia1 = VIAPTR(pVIAEnt->pPrimaryScrn);
 
973
            pVia1->HasSecondary = TRUE;
 
974
            pVia->sharedData = pVia1->sharedData;
 
975
        } else {
 
976
            DevUnion *pPriv;
 
977
            VIAEntPtr pVIAEnt;
 
978
 
 
979
            xf86SetPrimInitDone(pScrn->entityList[0]);
 
980
            pPriv = xf86GetEntityPrivate(pScrn->entityList[0], gVIAEntityIndex);
 
981
            pVia->sharedData = xnfcalloc(sizeof(ViaSharedRec), 1);
 
982
            pVIAEnt = pPriv->ptr;
 
983
            pVIAEnt->pPrimaryScrn = pScrn;
 
984
            pVIAEnt->IsDRIEnabled = FALSE;
 
985
            pVIAEnt->BypassSecondary = FALSE;
 
986
            pVIAEnt->HasSecondary = FALSE;
 
987
            pVIAEnt->RestorePrimary = FALSE;
 
988
            pVIAEnt->IsSecondaryRestored = FALSE;
 
989
        }
 
990
    } else {
 
991
        pVia->sharedData = xnfcalloc(sizeof(ViaSharedRec), 1);
 
992
    }
 
993
 
 
994
    pVia->PciInfo = xf86GetPciInfoForEntity(pEnt->index);
 
995
#ifndef HAVE_PCIACCESS
 
996
    xf86RegisterResources(pEnt->index, NULL, ResNone);
 
997
#endif
 
998
    if (pEnt->device->chipset && *pEnt->device->chipset) {
 
999
        from = X_CONFIG;
 
1000
        pScrn->chipset = pEnt->device->chipset;
 
1001
        pVia->Chipset = xf86StringToToken(VIAChipsets, pScrn->chipset);
 
1002
        pVia->ChipId = LookupChipSet(VIAPciChipsets, pVia->Chipset);
 
1003
    } else if (pEnt->device->chipID >= 0) {
 
1004
        from = X_CONFIG;
 
1005
        pVia->ChipId = pEnt->device->chipID;
 
1006
        pVia->Chipset = LookupChipID(VIAPciChipsets, pVia->ChipId);
 
1007
        pScrn->chipset = (char *)xf86TokenToString(VIAChipsets, pVia->Chipset);
 
1008
        xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipID override: 0x%04X\n",
 
1009
                   pEnt->device->chipID);
 
1010
    } else {
 
1011
        from = X_PROBED;
 
1012
        pVia->ChipId = DEVICE_ID(pVia->PciInfo);
 
1013
        pVia->Chipset = LookupChipID(VIAPciChipsets, pVia->ChipId);
 
1014
        pScrn->chipset = (char *)xf86TokenToString(VIAChipsets, pVia->Chipset);
 
1015
    }
 
1016
 
 
1017
    xf86DrvMsg(pScrn->scrnIndex, from, "Chipset: %s\n", pScrn->chipset);
 
1018
 
 
1019
    if (pEnt->device->chipRev >= 0) {
 
1020
        pVia->ChipRev = pEnt->device->chipRev;
 
1021
        xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipRev override: %d\n",
 
1022
                   pVia->ChipRev);
 
1023
    } else {
 
1024
        /* Read PCI bus 0, dev 0, function 0, index 0xF6 to get chip revision */
 
1025
#ifdef HAVE_PCIACCESS
 
1026
        struct pci_device *bridge = pci_device_get_parent_bridge(pVia->PciInfo);
 
1027
        uint8_t rev = 0;
 
1028
 
 
1029
        pci_device_cfg_read_u8(bridge, &rev, 0xF6);
 
1030
        pVia->ChipRev = rev;
 
1031
#else
 
1032
        pVia->ChipRev = pciReadByte(pciTag(0, 0, 0), 0xF6);
 
1033
#endif
 
1034
    }
 
1035
    free(pEnt);
 
1036
    xf86DrvMsg(pScrn->scrnIndex, from, "Chipset revision: %d\n", pVia->ChipRev);
 
1037
 
 
1038
    pVia->directRenderingType = DRI_NONE;
 
1039
    pVia->KMS = FALSE;
 
1040
#ifdef HAVE_DRI
 
1041
    busId = DRICreatePCIBusID(pVia->PciInfo);
 
1042
    pVia->drmmode.fd = drmOpen("via", busId);
 
1043
    if (pVia->drmmode.fd != -1) {
 
1044
        if (!drmCheckModesettingSupported(busId)) {
 
1045
            xf86DrvMsg(-1, X_INFO, "[drm] KMS supported\n");
 
1046
            pVia->KMS = TRUE;
 
1047
        } else
 
1048
            xf86DrvMsg(-1, X_INFO, "[drm] KMS not enabled\n");
 
1049
 
 
1050
        drmVer = drmGetVersion(pVia->drmmode.fd);
 
1051
        if (drmVer) {
 
1052
            pVia->drmVerMajor = drmVer->version_major;
 
1053
            pVia->drmVerMinor = drmVer->version_minor;
 
1054
            pVia->drmVerPL = drmVer->version_patchlevel;
 
1055
            drmFreeVersion(drmVer);
 
1056
 
 
1057
            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
1058
                        "[drm] via interface version: %d.%d.%d\n",
 
1059
                        pVia->drmVerMajor, pVia->drmVerMinor, pVia->drmVerPL);
 
1060
 
 
1061
            /* DRI2 or DRI1 support */
 
1062
            if ((pVia->drmVerMajor < drmExpected.major) ||
 
1063
                (pVia->drmVerMajor > drmCompat.major) ||
 
1064
               ((pVia->drmVerMajor == drmExpected.major) &&
 
1065
                (pVia->drmVerMinor < drmExpected.minor))) {
 
1066
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 
1067
                            "[drm] Kernel drm is not compatible with this driver.\n"
 
1068
                            "[drm] Kernel drm version is %d.%d.%d, "
 
1069
                            "and I can work with versions %d.%d.x - %d.x.x.\n"
 
1070
                            "[drm] Update either this 2D driver or your kernel DRM. "
 
1071
                            "Disabling DRI.\n", pVia->drmVerMajor, pVia->drmVerMinor,
 
1072
                            pVia->drmVerPL, drmExpected.major, drmExpected.minor,
 
1073
                            drmCompat.major);
 
1074
            } else {
 
1075
                /* DRI2 or DRI1 support */
 
1076
                if (pVia->drmVerMajor < drmCompat.major) {
 
1077
                    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "DRI 1 api supported\n");
 
1078
                    pVia->directRenderingType = DRI_1;
 
1079
                } else {
 
1080
                    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "DRI 2 api not supported yet\n");
 
1081
                    pVia->directRenderingType = DRI_2;
 
1082
                    pVia->NoAccel = TRUE;
 
1083
                }
 
1084
            }
 
1085
        } else {
 
1086
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Could not get DRM driver version\n");
 
1087
        }
 
1088
    } else {
 
1089
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
 
1090
                    "[drm] Failed to open DRM device for %s: %s\n",
 
1091
                    busId, strerror(errno));
 
1092
    }
 
1093
    free(busId);
 
1094
#endif
 
1095
 
 
1096
    if (!UMSPreInit(pScrn)) {
 
1097
        VIAFreeRec(pScrn);
 
1098
        return FALSE;
 
1099
    }
 
1100
 
 
1101
        /* Now handle the Display */
 
1102
    if (flags & PROBE_DETECT)
 
1103
        return TRUE;
 
1104
 
 
1105
    pScrn->monitor = pScrn->confScreen->monitor;
 
1106
 
 
1107
    /*
 
1108
     * We support depths of 8, 16 and 24.
 
1109
     * We support bpp of 8, 16, and 32.
 
1110
     */
 
1111
 
 
1112
    if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support32bppFb)) {
 
1113
        free(pEnt);
 
1114
        VIAFreeRec(pScrn);
 
1115
        return FALSE;
 
1116
    } else {
 
1117
        switch (pScrn->depth) {
 
1118
            case 8:
 
1119
            case 16:
 
1120
            case 24:
 
1121
            case 32:
 
1122
                /* OK */
 
1123
                break;
 
1124
            default:
 
1125
                xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
 
1126
                           "Given depth (%d) is not supported by this driver\n",
 
1127
                           pScrn->depth);
 
1128
                free(pEnt);
 
1129
                VIAFreeRec(pScrn);
 
1130
                return FALSE;
 
1131
        }
 
1132
    }
 
1133
 
 
1134
    xf86PrintDepthBpp(pScrn);
 
1135
 
 
1136
    if (pScrn->depth == 32) {
 
1137
        pScrn->depth = 24;
 
1138
    }
 
1139
 
 
1140
    if (pScrn->depth > 8) {
 
1141
        rgb zeros = { 0, 0, 0 };
 
1142
 
 
1143
        if (!xf86SetWeight(pScrn, zeros, zeros)) {
 
1144
            free(pEnt);
 
1145
            VIAFreeRec(pScrn);
 
1146
            return FALSE;
 
1147
        } else {
 
1148
            /* TODO check weight returned is supported */
 
1149
            ;
 
1150
        }
 
1151
    }
 
1152
 
 
1153
    if (!xf86SetDefaultVisual(pScrn, -1)) {
 
1154
        return FALSE;
 
1155
    } else {
 
1156
        /* We don't currently support DirectColor at > 8bpp */
 
1157
        if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
 
1158
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual"
 
1159
                       " (%s) is not supported at depth %d.\n",
 
1160
                       xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
 
1161
            free(pEnt);
 
1162
            VIAFreeRec(pScrn);
 
1163
            return FALSE;
 
1164
        }
 
1165
    }
 
1166
 
 
1167
    /* We use a programmable clock */
 
1168
    pScrn->progClock = TRUE;
 
1169
 
 
1170
    xf86CollectOptions(pScrn, option);
 
1171
 
 
1172
    /* Set the bits per RGB for 8bpp mode */
 
1173
    if (pScrn->depth == 8)
 
1174
        pScrn->rgbBits = 6;
 
1175
 
 
1176
    if (!VIASetupDefaultOptions(pScrn)) {
 
1177
        VIAFreeRec(pScrn);
 
1178
        return FALSE;
 
1179
    }
 
1180
 
 
1181
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, VIAOptions);
 
1182
 
 
1183
    if (xf86GetOptValInteger(VIAOptions, OPTION_VIDEORAM, &pScrn->videoRam))
 
1184
        xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1185
                   "Setting amount of VideoRAM to %d kB\n", pScrn->videoRam);
 
1186
 
 
1187
    if ((s = xf86GetOptValString(VIAOptions, OPTION_MODE_SWITCH_METHOD))) {
 
1188
        if (!xf86NameCmp(s, "legacy")) {
 
1189
            if (pVia->UseLegacyModeSwitch) {
 
1190
                xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1191
                           "Already using \"legacy\" as ModeSwitchMethod, "
 
1192
                           "did not force anything.\n");
 
1193
            } else {
 
1194
                pVia->UseLegacyModeSwitch = TRUE;
 
1195
                xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1196
                           "Forced ModeSwitchMethod to \"legacy\".\n");
 
1197
            }
 
1198
        }
 
1199
        else if (!xf86NameCmp(s, "new")) {
 
1200
            if (pVia->UseLegacyModeSwitch) {
 
1201
                pVia->UseLegacyModeSwitch = FALSE;
 
1202
                xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1203
                           "Forced ModeSwitchMethod to \"new\".\n");
 
1204
            } else {
 
1205
                xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1206
                           "Already using \"new\" as ModeSwitchMethod, "
 
1207
                           "did not force anything.\n");
 
1208
            }
 
1209
        } else {
 
1210
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "\"%s\" is not a valid"
 
1211
                       "value for Option \"ModeSwitchMethod\".\n", s);
 
1212
            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
1213
                       "Valid options are \"legacy\" or \"new\".\n");
 
1214
        }
 
1215
    }
 
1216
 
 
1217
    /* When rotating, switch shadow framebuffer on and acceleration off. */
 
1218
    if ((s = xf86GetOptValString(VIAOptions, OPTION_ROTATION_TYPE))) {
 
1219
        if (!xf86NameCmp(s, "SWRandR")) {
 
1220
            pVia->shadowFB = TRUE;
 
1221
            pVia->NoAccel = TRUE;
 
1222
            pVia->RandRRotation = TRUE;
 
1223
            pVia->rotate = RR_Rotate_0;
 
1224
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen "
 
1225
                       "RandR enabled, acceleration disabled\n");
 
1226
        } else if (!xf86NameCmp(s, "HWRandR")) {
 
1227
            pVia->shadowFB = TRUE;
 
1228
            pVia->NoAccel = TRUE;
 
1229
            pVia->RandRRotation = TRUE;
 
1230
            pVia->rotate = RR_Rotate_0;
 
1231
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Hardware accelerated "
 
1232
                       "rotating screen is not implemented. Using SW RandR.\n");
 
1233
        } else {
 
1234
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "\"%s\" is not a valid"
 
1235
                       "value for Option \"RotationType\".\n", s);
 
1236
            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
1237
                       "Valid options are \"SWRandR\" and \"HWRandR\".\n");
 
1238
        }
 
1239
    }
 
1240
 
 
1241
    /* When rotating, switch shadow framebuffer on and acceleration off. */
 
1242
    if ((s = xf86GetOptValString(VIAOptions, OPTION_ROTATE))) {
 
1243
        if (!xf86NameCmp(s, "CW")) {
 
1244
            pVia->shadowFB = TRUE;
 
1245
            pVia->NoAccel = TRUE;
 
1246
            pVia->RandRRotation = TRUE;
 
1247
            pVia->rotate = RR_Rotate_270;
 
1248
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen "
 
1249
                       "clockwise -- acceleration is disabled.\n");
 
1250
        } else if (!xf86NameCmp(s, "CCW")) {
 
1251
            pVia->shadowFB = TRUE;
 
1252
            pVia->NoAccel = TRUE;
 
1253
            pVia->RandRRotation = TRUE;
 
1254
            pVia->rotate = RR_Rotate_90;
 
1255
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen "
 
1256
                       "counterclockwise -- acceleration is disabled.\n");
 
1257
        } else if (!xf86NameCmp(s, "UD")) {
 
1258
            pVia->shadowFB = TRUE;
 
1259
            pVia->NoAccel = TRUE;
 
1260
            pVia->RandRRotation = TRUE;
 
1261
            pVia->rotate = RR_Rotate_180;
 
1262
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen "
 
1263
                       "upside-down -- acceleration is disabled.\n");
 
1264
        } else {
 
1265
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "\"%s\" is not a valid"
 
1266
                       "value for Option \"Rotate\".\n", s);
 
1267
            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
1268
                       "Valid options are \"CW\", \"CCW\" or  \"UD\".\n");
 
1269
        }
 
1270
    }
 
1271
 
 
1272
    from = (xf86GetOptValBool(VIAOptions, OPTION_SHADOW_FB, &pVia->shadowFB)
 
1273
            ? X_CONFIG : X_DEFAULT);
 
1274
    xf86DrvMsg(pScrn->scrnIndex, from, "Shadow framebuffer is %s.\n",
 
1275
               pVia->shadowFB ? "enabled" : "disabled");
 
1276
 
 
1277
    /* Use hardware acceleration, unless on shadow framebuffer. */
 
1278
    from = (xf86GetOptValBool(VIAOptions, OPTION_NOACCEL, &pVia->NoAccel)
 
1279
            ? X_CONFIG : X_DEFAULT);
 
1280
    if (!pVia->NoAccel && pVia->shadowFB) {
 
1281
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Acceleration is "
 
1282
                   "not supported when using shadow framebuffer.\n");
 
1283
        pVia->NoAccel = TRUE;
 
1284
        from = X_DEFAULT;
 
1285
    }
 
1286
 
 
1287
    /* Disable EXA for KMS case */
 
1288
    if (pVia->KMS)
 
1289
        pVia->NoAccel = TRUE;
 
1290
 
 
1291
    xf86DrvMsg(pScrn->scrnIndex, from, "Hardware acceleration is %s.\n",
 
1292
               !pVia->NoAccel ? "enabled" : "disabled");
 
1293
 
 
1294
    if (!pVia->NoAccel) {
 
1295
        from = X_DEFAULT;
 
1296
        if ((s = (char *)xf86GetOptValString(VIAOptions, OPTION_ACCELMETHOD))) {
 
1297
            if (!xf86NameCmp(s, "EXA")) {
 
1298
                from = X_CONFIG;
 
1299
                pVia->useEXA = TRUE;
 
1300
            } else if (!xf86NameCmp(s, "XAA")) {
 
1301
                from = X_CONFIG;
 
1302
                pVia->useEXA = TRUE;
 
1303
            }
 
1304
        }
 
1305
        xf86DrvMsg(pScrn->scrnIndex, from,
 
1306
                   "Using %s acceleration architecture.\n",
 
1307
                   pVia->useEXA ? "EXA" : "XAA");
 
1308
 
 
1309
        //pVia->noComposite = FALSE;
 
1310
        if (pVia->useEXA) {
 
1311
            from = xf86GetOptValBool(VIAOptions, OPTION_EXA_NOCOMPOSITE,
 
1312
                                     &pVia->noComposite) ? X_CONFIG : X_DEFAULT;
 
1313
            xf86DrvMsg(pScrn->scrnIndex, from,
 
1314
                       "EXA composite acceleration %s.\n",
 
1315
                       !pVia->noComposite ? "enabled" : "disabled");
 
1316
 
 
1317
            //pVia->exaScratchSize = VIA_SCRATCH_SIZE / 1024;
 
1318
            from = xf86GetOptValInteger(VIAOptions, OPTION_EXA_SCRATCH_SIZE,
 
1319
                                        &pVia->exaScratchSize)
 
1320
                    ? X_CONFIG : X_DEFAULT;
 
1321
            xf86DrvMsg(pScrn->scrnIndex, from,
 
1322
                       "EXA scratch area size is %d kB.\n",
 
1323
                       pVia->exaScratchSize);
 
1324
        }
 
1325
    }
 
1326
 
 
1327
    /* Use a hardware cursor, unless on secondary or on shadow framebuffer. */
 
1328
    from = X_DEFAULT;
 
1329
    if (pVia->IsSecondary || pVia->shadowFB)
 
1330
        pVia->drmmode.hwcursor = FALSE;
 
1331
    else if (xf86GetOptValBool(VIAOptions, OPTION_SWCURSOR,
 
1332
                               &pVia->drmmode.hwcursor)) {
 
1333
        pVia->drmmode.hwcursor = !pVia->drmmode.hwcursor;
 
1334
        from = X_CONFIG;
 
1335
    }
 
1336
    if (pVia->drmmode.hwcursor)
 
1337
        xf86DrvMsg(pScrn->scrnIndex, from, "Using hardware two-color "
 
1338
                   "cursors and software full-color cursors.\n");
 
1339
    else
 
1340
        xf86DrvMsg(pScrn->scrnIndex, from, "Using software cursors.\n");
 
1341
 
 
1342
    //pVia->VQEnable = TRUE;
 
1343
    from = xf86GetOptValBool(VIAOptions, OPTION_DISABLEVQ, &pVia->VQEnable)
 
1344
            ? X_CONFIG : X_DEFAULT;
 
1345
    if (from == X_CONFIG)
 
1346
        pVia->VQEnable = !pVia->VQEnable;
 
1347
    xf86DrvMsg(pScrn->scrnIndex, from,
 
1348
               "GPU virtual command queue will be %s.\n",
 
1349
               (pVia->VQEnable) ? "enabled" : "disabled");
 
1350
 
 
1351
    //pVia->DRIIrqEnable = TRUE;
 
1352
    from = xf86GetOptValBool(VIAOptions, OPTION_DISABLEIRQ, &pVia->DRIIrqEnable)
 
1353
            ? X_CONFIG : X_DEFAULT;
 
1354
    if (from == X_CONFIG)
 
1355
        pVia->DRIIrqEnable = !pVia->DRIIrqEnable;
 
1356
    xf86DrvMsg(pScrn->scrnIndex, from,
 
1357
               "DRI IRQ will be %s if DRI is enabled.\n",
 
1358
               (pVia->DRIIrqEnable) ? "enabled" : "disabled");
 
1359
 
 
1360
    //pVia->agpEnable = FALSE;
 
1361
    from = xf86GetOptValBool(VIAOptions, OPTION_AGP_DMA, &pVia->agpEnable)
 
1362
            ? X_CONFIG : X_DEFAULT;
 
1363
    xf86DrvMsg(pScrn->scrnIndex, from,
 
1364
               "AGP DMA will be %s if DRI is enabled.\n",
 
1365
               (pVia->agpEnable) ? "enabled" : "disabled");
 
1366
 
 
1367
    //pVia->dma2d = TRUE;
 
1368
    if (pVia->agpEnable) {
 
1369
        from = xf86GetOptValBool(VIAOptions, OPTION_2D_DMA, &pVia->dma2d)
 
1370
                ? X_CONFIG : X_DEFAULT;
 
1371
        if (from == X_CONFIG)
 
1372
            pVia->dma2d = !pVia->dma2d;
 
1373
        xf86DrvMsg(pScrn->scrnIndex, from, "AGP DMA will %sbe used for "
 
1374
                   "2D acceleration.\n", (pVia->dma2d) ? "" : "not ");
 
1375
    }
 
1376
    //pVia->dmaXV = TRUE;
 
1377
    from = xf86GetOptValBool(VIAOptions, OPTION_XV_DMA, &pVia->dmaXV)
 
1378
            ? X_CONFIG : X_DEFAULT;
 
1379
    if (from == X_CONFIG)
 
1380
        pVia->dmaXV = !pVia->dmaXV;
 
1381
    xf86DrvMsg(pScrn->scrnIndex, from, "PCI DMA will %sbe used for XV "
 
1382
               "image transfer if DRI is enabled.\n",
 
1383
               (pVia->dmaXV) ? "" : "not ");
 
1384
 
 
1385
    //pVia->useVBEModes = FALSE;
 
1386
    from = xf86GetOptValBool(VIAOptions, OPTION_VBEMODES, &pVia->useVBEModes)
 
1387
            ? X_CONFIG : X_DEFAULT;
 
1388
    xf86DrvMsg(pScrn->scrnIndex, from, "Will %senable VBE modes.\n",
 
1389
               (pVia->useVBEModes) ? "" : "not ");
 
1390
 
 
1391
    //pVia->vbeSR = FALSE;
 
1392
    from = xf86GetOptValBool(VIAOptions, OPTION_VBE_SAVERESTORE, &pVia->vbeSR)
 
1393
            ? X_CONFIG : X_DEFAULT;
 
1394
    xf86DrvMsg(pScrn->scrnIndex, from, "VBE VGA register save & restore "
 
1395
               "will %sbe used\n\tif VBE modes are enabled.\n",
 
1396
               (pVia->vbeSR) ? "" : "not ");
 
1397
 
 
1398
#ifdef HAVE_DEBUG
 
1399
    //pVia->disableXvBWCheck = FALSE;
 
1400
    from = xf86GetOptValBool(VIAOptions, OPTION_DISABLE_XV_BW_CHECK,
 
1401
                             &pVia->disableXvBWCheck)
 
1402
            ? X_CONFIG : X_DEFAULT;
 
1403
    xf86DrvMsg(pScrn->scrnIndex, from, "Xv Bandwidth check is %s.\n",
 
1404
               pVia->disableXvBWCheck ? "disabled" : "enabled");
 
1405
    if (pVia->disableXvBWCheck) {
 
1406
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 
1407
                   "You may get a \"snowy\" screen"
 
1408
                   " when using the Xv overlay.\n");
 
1409
    }
 
1410
#endif
 
1411
 
 
1412
    //pVia->maxDriSize = 0;
 
1413
    from = xf86GetOptValInteger(VIAOptions, OPTION_MAX_DRIMEM,
 
1414
                                &pVia->maxDriSize)
 
1415
            ? X_CONFIG : X_DEFAULT;
 
1416
    if (pVia->maxDriSize > 0)
 
1417
        xf86DrvMsg(pScrn->scrnIndex, from,
 
1418
                   "Will impose a %d kB limit on video RAM reserved for DRI.\n",
 
1419
                   pVia->maxDriSize);
 
1420
    else
 
1421
        xf86DrvMsg(pScrn->scrnIndex, from,
 
1422
                   "Will not impose a limit on video RAM reserved for DRI.\n");
 
1423
 
 
1424
    //pVia->agpMem = AGP_SIZE / 1024;
 
1425
    from = xf86GetOptValInteger(VIAOptions, OPTION_AGPMEM, &pVia->agpMem)
 
1426
            ? X_CONFIG : X_DEFAULT;
 
1427
    xf86DrvMsg(pScrn->scrnIndex, from,
 
1428
               "Will try to allocate %d kB of AGP memory.\n", pVia->agpMem);
 
1429
 
 
1430
    /* ActiveDevice Option for device selection */
 
1431
    //pVia->ActiveDevice = 0x00;
 
1432
    if ((s = xf86GetOptValString(VIAOptions, OPTION_ACTIVEDEVICE))) {
 
1433
        if (strstr(s, "CRT"))
 
1434
            pVia->ActiveDevice |= VIA_DEVICE_CRT;
 
1435
        if (strstr(s, "LCD"))
 
1436
            pVia->ActiveDevice |= VIA_DEVICE_LCD;
 
1437
        if (strstr(s, "DFP"))
 
1438
            pVia->ActiveDevice |= VIA_DEVICE_DFP;
 
1439
        if (strstr(s, "TV"))
 
1440
            pVia->ActiveDevice |= VIA_DEVICE_TV;
 
1441
    }
 
1442
 
 
1443
    pBIOSInfo = pVia->pBIOSInfo;
 
1444
    pBIOSInfo->TVDotCrawl = FALSE;
 
1445
    from = xf86GetOptValBool(VIAOptions, OPTION_TVDOTCRAWL,
 
1446
                             &pBIOSInfo->TVDotCrawl)
 
1447
            ? X_CONFIG : X_DEFAULT;
 
1448
    xf86DrvMsg(pScrn->scrnIndex, from, "TV dotCrawl is %s.\n",
 
1449
               pBIOSInfo->TVDotCrawl ? "enabled" : "disabled");
 
1450
 
 
1451
    /* TV Deflicker */
 
1452
    pBIOSInfo->TVDeflicker = 0;
 
1453
    from = xf86GetOptValInteger(VIAOptions, OPTION_TVDEFLICKER,
 
1454
                                &pBIOSInfo->TVDeflicker)
 
1455
            ? X_CONFIG : X_DEFAULT;
 
1456
    xf86DrvMsg(pScrn->scrnIndex, from, "TV deflicker is set to %d.\n",
 
1457
               pBIOSInfo->TVDeflicker);
 
1458
 
 
1459
    pBIOSInfo->TVType = TVTYPE_NONE;
 
1460
    if ((s = xf86GetOptValString(VIAOptions, OPTION_TVTYPE))) {
 
1461
        if (!xf86NameCmp(s, "NTSC")) {
 
1462
            pBIOSInfo->TVType = TVTYPE_NTSC;
 
1463
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is NTSC.\n");
 
1464
        } else if (!xf86NameCmp(s, "PAL")) {
 
1465
            pBIOSInfo->TVType = TVTYPE_PAL;
 
1466
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is PAL.\n");
 
1467
        } else if (!xf86NameCmp(s, "480P")) {
 
1468
            pBIOSInfo->TVType = TVTYPE_480P;
 
1469
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is SDTV 480P.\n");
 
1470
        } else if (!xf86NameCmp(s, "576P")) {
 
1471
            pBIOSInfo->TVType = TVTYPE_576P;
 
1472
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is SDTV 576P.\n");
 
1473
        } else if (!xf86NameCmp(s, "720P")) {
 
1474
            pBIOSInfo->TVType = TVTYPE_720P;
 
1475
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is HDTV 720P.\n");
 
1476
        } else if (!xf86NameCmp(s, "1080I")) {
 
1477
            pBIOSInfo->TVType = TVTYPE_1080I;
 
1478
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Type is HDTV 1080i.\n");
 
1479
        }
 
1480
    } else {
 
1481
        xf86DrvMsg(pScrn->scrnIndex, X_DEFAULT, "No default TV type is set.\n");
 
1482
    }
 
1483
 
 
1484
    /* TV output signal Option */
 
1485
    pBIOSInfo->TVOutput = TVOUTPUT_NONE;
 
1486
    if ((s = xf86GetOptValString(VIAOptions, OPTION_TVOUTPUT))) {
 
1487
        if (!xf86NameCmp(s, "S-Video")) {
 
1488
            pBIOSInfo->TVOutput = TVOUTPUT_SVIDEO;
 
1489
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1490
                       "TV Output Signal is S-Video.\n");
 
1491
        } else if (!xf86NameCmp(s, "Composite")) {
 
1492
            pBIOSInfo->TVOutput = TVOUTPUT_COMPOSITE;
 
1493
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1494
                       "TV Output Signal is Composite.\n");
 
1495
        } else if (!xf86NameCmp(s, "SC")) {
 
1496
            pBIOSInfo->TVOutput = TVOUTPUT_SC;
 
1497
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "TV Output Signal is SC.\n");
 
1498
        } else if (!xf86NameCmp(s, "RGB")) {
 
1499
            pBIOSInfo->TVOutput = TVOUTPUT_RGB;
 
1500
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1501
                       "TV Output Signal is RGB.\n");
 
1502
        } else if (!xf86NameCmp(s, "YCbCr")) {
 
1503
            pBIOSInfo->TVOutput = TVOUTPUT_YCBCR;
 
1504
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1505
                       "TV Output Signal is YCbCr.\n");
 
1506
        }
 
1507
    } else {
 
1508
        xf86DrvMsg(pScrn->scrnIndex, X_DEFAULT,
 
1509
                   "No default TV output signal type is set.\n");
 
1510
    }
 
1511
 
 
1512
    /* TV DI Port */
 
1513
    if ((s = xf86GetOptValString(VIAOptions, OPTION_TVDIPORT))) {
 
1514
        if (!xf86NameCmp(s, "DVP0")) {
 
1515
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP0;
 
1516
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1517
                       "TV Output Port is DVP0.\n");
 
1518
        } else if (!xf86NameCmp(s, "DVP1")) {
 
1519
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DVP1;
 
1520
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1521
                       "TV Output Port is DVP1.\n");
 
1522
        } else if (!xf86NameCmp(s, "DFPHigh")) {
 
1523
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DFPHIGH;
 
1524
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1525
                       "TV Output Port is DFPHigh.\n");
 
1526
        } else if (!xf86NameCmp(s, "DFPLow")) {
 
1527
            pBIOSInfo->TVDIPort = VIA_DI_PORT_DFPLOW;
 
1528
            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
 
1529
                       "TV Output Port is DFPLow.\n");
 
1530
        }
 
1531
    } else {
 
1532
        xf86DrvMsg(pScrn->scrnIndex, X_DEFAULT,
 
1533
                   "No default TV output port is set.\n");
 
1534
    }
 
1535
 
 
1536
    VIAVidHWDiffInit(pScrn);
 
1537
 
 
1538
#ifdef HAVE_DEBUG
 
1539
    //pVia->PrintVGARegs = FALSE;
 
1540
    from = xf86GetOptValBool(VIAOptions, OPTION_PRINTVGAREGS,
 
1541
                             &pVia->PrintVGARegs)
 
1542
            ? X_CONFIG : X_DEFAULT;
 
1543
    xf86DrvMsg(pScrn->scrnIndex, from, "Will %sprint VGA registers.\n",
 
1544
               pVia->PrintVGARegs ? "" : "not ");
 
1545
    if (pVia->PrintVGARegs)
 
1546
        ViaVgahwPrint(VGAHWPTR(pScrn)); /* Do this as early as possible */
 
1547
 
 
1548
    pVia->I2CScan = FALSE;
 
1549
    from = xf86GetOptValBool(VIAOptions, OPTION_I2CSCAN, &pVia->I2CScan)
 
1550
            ? X_CONFIG : X_DEFAULT;
 
1551
    xf86DrvMsg(pScrn->scrnIndex, from, "Will %sscan I2C buses.\n",
 
1552
               pVia->I2CScan ? "" : "not ");
 
1553
#endif /* HAVE_DEBUG */
 
1554
 
 
1555
    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
1556
               "...Finished parsing config file options.\n");
 
1557
 
 
1558
    ViaCheckCardId(pScrn);
 
1559
 
 
1560
    /* I2CDevices Option for I2C Initialization */
 
1561
    if ((s = xf86GetOptValString(VIAOptions, OPTION_I2CDEVICES))) {
 
1562
        pVia->I2CDevices = 0;
 
1563
        if (strstr(s, "Bus1"))
 
1564
            pVia->I2CDevices |= VIA_I2C_BUS1;
 
1565
        if (strstr(s, "Bus2"))
 
1566
            pVia->I2CDevices |= VIA_I2C_BUS2;
 
1567
        if (strstr(s, "Bus3"))
 
1568
            pVia->I2CDevices |= VIA_I2C_BUS3;
 
1569
    }
 
1570
 
 
1571
    if (!xf86NameCmp(pVia->Id->String, "OLPC XO 1.5"))
 
1572
        pVia->I2CDevices &= ~VIA_I2C_BUS2;
 
1573
 
 
1574
    /* CRTC handling */
 
1575
    xf86CrtcConfigInit(pScrn, &via_xf86crtc_config_funcs);
 
1576
 
 
1577
    if (pVia->KMS) {
 
1578
        if (!KMSCrtcInit(pScrn, &pVia->drmmode)) {
 
1579
            VIAFreeRec(pScrn);
 
1580
            return FALSE;
 
1581
        }
 
1582
    } else {
 
1583
        if (!UMSCrtcInit(pScrn)) {
 
1584
            VIAFreeRec(pScrn);
 
1585
            return FALSE;
 
1586
        }
 
1587
    }
 
1588
 
 
1589
    if (!xf86InitialConfiguration(pScrn, TRUE)) {
 
1590
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial configuration failed\n");
 
1591
        return FALSE;
 
1592
    }
 
1593
 
 
1594
    if (!pScrn->modes) {
 
1595
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
 
1596
        return FALSE;
 
1597
    }
 
1598
 
 
1599
    /* Initialize the colormap */
 
1600
    Gamma zeros = { 0.0, 0.0, 0.0 };
 
1601
    if (!xf86SetGamma(pScrn, zeros)) {
 
1602
        VIAFreeRec(pScrn);
 
1603
        return FALSE;
 
1604
    }
 
1605
 
 
1606
    /* Set up screen parameters. */
 
1607
    pVia->Bpp = pScrn->bitsPerPixel >> 3;
 
1608
    pVia->Bpl = pScrn->virtualX * pVia->Bpp;
 
1609
 
 
1610
    /* Set the current mode to the first in the list */
 
1611
    pScrn->currentMode = pScrn->modes;
 
1612
 
 
1613
    /* Set display resolution */
 
1614
    xf86SetDpi(pScrn, 0, 0);
 
1615
 
 
1616
    if (xf86LoadSubModule(pScrn, "fb") == NULL) {
 
1617
        VIAFreeRec(pScrn);
 
1618
        return FALSE;
 
1619
    }
 
1620
 
 
1621
    if (!pVia->NoAccel) {
 
1622
        XF86ModReqInfo req;
 
1623
        int errmaj, errmin;
 
1624
 
 
1625
        memset(&req, 0, sizeof(req));
 
1626
        req.majorversion = 2;
 
1627
        req.minorversion = 0;
 
1628
        if (!LoadSubModule(pScrn->module, "exa", NULL, NULL, NULL, &req,
 
1629
                            &errmaj, &errmin)) {
 
1630
            LoaderErrorMsg(NULL, "exa", errmaj, errmin);
 
1631
            VIAFreeRec(pScrn);
 
1632
            return FALSE;
 
1633
        }
 
1634
    }
 
1635
 
 
1636
    if (pVia->shadowFB) {
 
1637
        if (!xf86LoadSubModule(pScrn, "shadow")) {
 
1638
            VIAFreeRec(pScrn);
 
1639
            return FALSE;
 
1640
        }
 
1641
    }
 
1642
    return TRUE;
 
1643
}
 
1644
 
 
1645
static void
 
1646
LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
 
1647
                LOCO * colors, VisualPtr pVisual)
 
1648
{
 
1649
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
1650
    CARD16 lut_r[256], lut_g[256], lut_b[256];
 
1651
    int i, j, k, index;
 
1652
 
 
1653
    for (k = 0; k < xf86_config->num_crtc; k++) {
 
1654
        xf86CrtcPtr crtc = xf86_config->crtc[k];
 
1655
 
 
1656
        switch (pScrn->depth) {
 
1657
        case 15:
 
1658
            for (i = 0; i < numColors; i++) {
 
1659
                index = indices[i];
 
1660
                for (j = 0; j < 8; j++) {
 
1661
                    lut_r[index * 8 + j] = colors[index].red << 8;
 
1662
                    lut_g[index * 8 + j] = colors[index].green << 8;
 
1663
                    lut_b[index * 8 + j] = colors[index].blue << 8;
 
1664
                }
 
1665
            }
 
1666
            break;
 
1667
        case 16:
 
1668
            for (i = 0; i < numColors; i++) {
 
1669
                index = indices[i];
 
1670
 
 
1671
                if (index <= 31) {
 
1672
                    for (j = 0; j < 8; j++) {
 
1673
                        lut_r[index * 8 + j] = colors[index].red << 8;
 
1674
                        lut_b[index * 8 + j] = colors[index].blue << 8;
 
1675
                    }
 
1676
                }
 
1677
 
 
1678
                for (j = 0; j < 4; j++)
 
1679
                    lut_g[index * 4 + j] = colors[index].green << 8;
 
1680
            }
 
1681
            break;
 
1682
        default:
 
1683
            for (i = 0; i < numColors; i++) {
 
1684
                index = indices[i];
 
1685
                lut_r[index] = colors[index].red << 8;
 
1686
                lut_g[index] = colors[index].green << 8;
 
1687
                lut_b[index] = colors[index].blue << 8;
 
1688
            }
 
1689
            break;
 
1690
        }
 
1691
 
 
1692
        /* Make the change through RandR */
 
1693
#ifdef RANDR_12_INTERFACE
 
1694
        RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
 
1695
#else /*RANDR_12_INTERFACE*/
 
1696
        crtc->funcs->gamma_set(crtc, lut_r, lut_g, lut_b, 256);
 
1697
#endif
 
1698
    }
 
1699
}
 
1700
 
 
1701
static void *
 
1702
viaShadowWindow(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
 
1703
                                CARD32 *size, void *closure)
 
1704
{
 
1705
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
 
1706
    VIAPtr pVia = VIAPTR(pScrn);
 
1707
    int stride;
 
1708
 
 
1709
    stride = (pScrn->displayWidth * pScrn->bitsPerPixel) / 8;
 
1710
    *size = stride;
 
1711
    return ((uint8_t *) drm_bo_map(pScrn, pVia->drmmode.front_bo) + row * stride + offset);
 
1712
}
 
1713
 
 
1714
static Bool
 
1715
VIACreateScreenResources(ScreenPtr pScreen)
 
1716
{
 
1717
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
 
1718
    VIAPtr pVia = VIAPTR(pScrn);
 
1719
    PixmapPtr rootPixmap;
 
1720
    void *surface;
 
1721
 
 
1722
    pScreen->CreateScreenResources = pVia->CreateScreenResources;
 
1723
    if (!(*pScreen->CreateScreenResources)(pScreen))
 
1724
        return FALSE;
 
1725
    pScreen->CreateScreenResources = VIACreateScreenResources;
 
1726
 
 
1727
    rootPixmap = pScreen->GetScreenPixmap(pScreen);
 
1728
 
 
1729
#ifdef HAVE_DRI
 
1730
    drmmode_uevent_init(pScrn, &pVia->drmmode);
 
1731
#endif
 
1732
    surface = drm_bo_map(pScrn, pVia->drmmode.front_bo);
 
1733
    if (!surface)
 
1734
        return FALSE;
 
1735
 
 
1736
    if (pVia->shadowFB)
 
1737
        surface = pVia->ShadowPtr;
 
1738
 
 
1739
    if (!pScreen->ModifyPixmapHeader(rootPixmap, pScrn->virtualX,
 
1740
                                        pScrn->virtualY, -1, -1,
 
1741
                                        pVia->drmmode.front_bo->pitch,
 
1742
                                        surface))
 
1743
        return FALSE;
 
1744
 
 
1745
    if (pVia->shadowFB) {
 
1746
        if (!shadowAdd(pScreen, rootPixmap, shadowUpdatePackedWeak(),
 
1747
                        viaShadowWindow, 0, NULL))
 
1748
            return FALSE;
 
1749
    }
 
1750
    return TRUE;
 
1751
}
 
1752
 
 
1753
static Bool
 
1754
VIACloseScreen(CLOSE_SCREEN_ARGS_DECL)
 
1755
{
 
1756
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
 
1757
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
1758
    VIAPtr pVia = VIAPTR(pScrn);
 
1759
    int i;
 
1760
 
 
1761
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIACloseScreen\n"));
 
1762
 
 
1763
    if (pVia->directRenderingType != DRI_2)
 
1764
        viaExitVideo(pScrn);
 
1765
 
 
1766
    viaExitAccel(pScreen);
 
1767
 
 
1768
    if (pVia->ShadowPtr) {
 
1769
        shadowRemove(pScreen, pScreen->GetScreenPixmap(pScreen));
 
1770
        free(pVia->ShadowPtr);
 
1771
        pVia->ShadowPtr = NULL;
 
1772
    }
 
1773
 
 
1774
    /* Is the display currently visible? */
 
1775
    if (pScrn->vtSema)
 
1776
        VIALeaveVT(VT_FUNC_ARGS(0));
 
1777
 
 
1778
#ifdef HAVE_DRI
 
1779
    drmmode_uevent_fini(pScrn, &pVia->drmmode);
 
1780
#endif
 
1781
    xf86_cursors_fini(pScreen);
 
1782
 
 
1783
    for (i = 0; i < xf86_config->num_crtc; i++) {
 
1784
        xf86CrtcPtr crtc = xf86_config->crtc[i];
 
1785
        drmmode_crtc_private_ptr iga = crtc->driver_private;
 
1786
 
 
1787
        if (iga->cursor_bo)
 
1788
            drm_bo_free(pScrn, iga->cursor_bo);
 
1789
    }
 
1790
 
 
1791
    if (pVia->drmmode.front_bo) {
 
1792
#ifdef HAVE_DRI
 
1793
        if (pVia->KMS && pVia->drmmode.fb_id)
 
1794
            drmModeRmFB(pVia->drmmode.fd, pVia->drmmode.fb_id);
 
1795
#endif
 
1796
        pVia->drmmode.fb_id = 0;
 
1797
 
 
1798
        drm_bo_free(pScrn, pVia->drmmode.front_bo);
 
1799
    }
 
1800
 
 
1801
#ifdef HAVE_DRI
 
1802
    if (pVia->directRenderingType == DRI_1)
 
1803
        VIADRICloseScreen(pScreen);
 
1804
 
 
1805
    if (pVia->KMS) {
 
1806
        drmmode_uevent_fini(pScrn, &pVia->drmmode);
 
1807
 
 
1808
        if (drmDropMaster(pVia->drmmode.fd))
 
1809
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 
1810
                        "drmDropMaster failed: %s\n",
 
1811
                        strerror(errno));
 
1812
    }
 
1813
#endif
 
1814
 
 
1815
    pScrn->vtSema = FALSE;
 
1816
    pScreen->CloseScreen = pVia->CloseScreen;
 
1817
    return (*pScreen->CloseScreen) (CLOSE_SCREEN_ARGS);
 
1818
}
 
1819
 
 
1820
static Bool
 
1821
VIAScreenInit(SCREEN_INIT_ARGS_DECL)
 
1822
{
 
1823
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
 
1824
    VIAPtr pVia = VIAPTR(pScrn);
 
1825
    int format;
 
1826
 
 
1827
    pScrn->pScreen = pScreen;
 
1828
    pScrn->displayWidth = pScrn->virtualX;
 
1829
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAScreenInit\n"));
 
1830
 
 
1831
#ifdef HAVE_DRI
 
1832
    if (pVia->KMS) {
 
1833
        if (drmSetMaster(pVia->drmmode.fd)) {
 
1834
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 
1835
                        "drmSetMaster failed: %s\n",
 
1836
                        strerror(errno));
 
1837
        }
 
1838
    }
 
1839
 
 
1840
    if (pVia->drmmode.fd != -1) {
 
1841
        if (pVia->directRenderingType == DRI_1) {
 
1842
            /* DRI2 or DRI1 support */
 
1843
            if (VIADRI1ScreenInit(pScreen))
 
1844
                DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "DRI1 ScreenInit commplete\n"));
 
1845
            else
 
1846
                pVia->directRenderingType = DRI_NONE;
 
1847
        }
 
1848
    }
 
1849
#endif
 
1850
 
 
1851
    if (!drm_bo_manager_init(pScrn))
 
1852
        return FALSE;
 
1853
 
 
1854
    format = map_legacy_formats(pScrn->bitsPerPixel, pScrn->depth);
 
1855
    pVia->drmmode.front_bo = drm_bo_alloc_surface(pScrn, pScrn->virtualX, pScrn->virtualY,
 
1856
                                                    format, 16, TTM_PL_FLAG_VRAM);
 
1857
    if (!pVia->drmmode.front_bo)
 
1858
        return FALSE;
 
1859
 
 
1860
    if (!drm_bo_map(pScrn, pVia->drmmode.front_bo))
 
1861
        return FALSE;
 
1862
 
 
1863
    if (!pVia->NoAccel && !UMSAccelInit(pScrn->pScreen))
 
1864
        return FALSE;
 
1865
 
 
1866
    miClearVisualTypes();
 
1867
 
 
1868
    if (pScrn->bitsPerPixel > 8 && !pVia->IsSecondary) {
 
1869
        if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
 
1870
                              pScrn->rgbBits, pScrn->defaultVisual))
 
1871
            return FALSE;
 
1872
        if (!miSetPixmapDepths())
 
1873
            return FALSE;
 
1874
    } else {
 
1875
        if (!miSetVisualTypes(pScrn->depth,
 
1876
                              miGetDefaultVisualMask(pScrn->depth),
 
1877
                              pScrn->rgbBits, pScrn->defaultVisual))
 
1878
            return FALSE;
 
1879
        if (!miSetPixmapDepths())
 
1880
            return FALSE;
 
1881
    }
 
1882
 
 
1883
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Visuals set up\n"));
 
1884
 
 
1885
    if (pVia->shadowFB) {
 
1886
        int pitch = BitmapBytePad(pScrn->bitsPerPixel * pScrn->virtualX);
 
1887
 
 
1888
        pVia->shadowFB = FALSE;
 
1889
        pVia->ShadowPtr = malloc(pitch * pScrn->virtualY);
 
1890
        if (pVia->ShadowPtr) {
 
1891
            if (shadowSetup(pScreen))
 
1892
                pVia->shadowFB = TRUE;
 
1893
        }
 
1894
    }
 
1895
 
 
1896
    if (!fbScreenInit(pScreen, NULL, pScrn->virtualX, pScrn->virtualY,
 
1897
                        pScrn->xDpi, pScrn->yDpi, pScrn->displayWidth,
 
1898
                        pScrn->bitsPerPixel))
 
1899
        return FALSE;
 
1900
 
 
1901
    xf86SetBlackWhitePixels(pScreen);
 
1902
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- B & W\n"));
 
1903
 
 
1904
    if (pScrn->bitsPerPixel > 8) {
 
1905
        VisualPtr visual;
 
1906
 
 
1907
        visual = pScreen->visuals + pScreen->numVisuals;
 
1908
        while (--visual >= pScreen->visuals) {
 
1909
            if ((visual->class | DynamicClass) == DirectColor) {
 
1910
                visual->offsetRed = pScrn->offset.red;
 
1911
                visual->offsetGreen = pScrn->offset.green;
 
1912
                visual->offsetBlue = pScrn->offset.blue;
 
1913
                visual->redMask = pScrn->mask.red;
 
1914
                visual->greenMask = pScrn->mask.green;
 
1915
                visual->blueMask = pScrn->mask.blue;
 
1916
            }
 
1917
        }
 
1918
    }
 
1919
 
 
1920
    /* Must be after RGB ordering is fixed. */
 
1921
    fbPictureInit(pScreen, NULL, 0);
 
1922
 
 
1923
    if (!pVia->NoAccel && !viaInitExa(pScreen))
 
1924
        return FALSE;
 
1925
 
 
1926
    xf86SetBackingStore(pScreen);
 
1927
#if 0
 
1928
    xf86SetSilkenMouse(pScreen);
 
1929
#endif
 
1930
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Backing store set up\n"));
 
1931
 
 
1932
    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
 
1933
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- SW cursor set up\n"));
 
1934
 
 
1935
    if (pVia->drmmode.hwcursor) {
 
1936
        xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
 
1937
        int flags = (HARDWARE_CURSOR_AND_SOURCE_WITH_MASK |
 
1938
                     HARDWARE_CURSOR_TRUECOLOR_AT_8BPP);
 
1939
        int cursorSize, size, i = 0;
 
1940
 
 
1941
        switch (pVia->Chipset) {
 
1942
        case VIA_CLE266:
 
1943
        case VIA_KM400:
 
1944
            flags |= HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1;
 
1945
            size = 32;
 
1946
            cursorSize = ((size * size) >> 3) * 2;
 
1947
            break;
 
1948
        default:
 
1949
            DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "HWCursor ARGB enabled\n"));
 
1950
            flags |= (HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 | HARDWARE_CURSOR_ARGB);
 
1951
            size = 64;
 
1952
            cursorSize = (size * size) << 2;
 
1953
            break;
 
1954
        }
 
1955
 
 
1956
        for (i = 0; i < xf86_config->num_crtc; i++) {
 
1957
            xf86CrtcPtr crtc = xf86_config->crtc[i];
 
1958
            drmmode_crtc_private_ptr iga = crtc->driver_private;
 
1959
 
 
1960
            /* Set cursor location in frame buffer. */
 
1961
            iga->cursor_bo = drm_bo_alloc(pScrn, cursorSize, 16, TTM_PL_FLAG_VRAM);
 
1962
        }
 
1963
 
 
1964
        if (!xf86_cursors_init(pScreen, size, size, flags)) {
 
1965
            pVia->drmmode.hwcursor = FALSE;
 
1966
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
 
1967
                        "Hardware cursor initialization failed\n");
 
1968
        }
 
1969
    }
 
1970
 
 
1971
    pScrn->vtSema = TRUE;
 
1972
    pScreen->SaveScreen = xf86SaveScreen;
 
1973
    pVia->CloseScreen = pScreen->CloseScreen;
 
1974
    pScreen->CloseScreen = VIACloseScreen;
 
1975
    pVia->CreateScreenResources = pScreen->CreateScreenResources;
 
1976
    pScreen->CreateScreenResources = VIACreateScreenResources;
 
1977
 
 
1978
    if (!xf86CrtcScreenInit(pScreen))
 
1979
        return FALSE;
 
1980
 
 
1981
    if (!miCreateDefColormap(pScreen))
 
1982
        return FALSE;
 
1983
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Def Color map set up\n"));
 
1984
 
 
1985
    if (!xf86HandleColormaps(pScreen, 256, 8, LoadPalette, NULL,
 
1986
                             CMAP_RELOAD_ON_MODE_SWITCH
 
1987
                             | CMAP_PALETTED_TRUECOLOR))
 
1988
        return FALSE;
 
1989
 
 
1990
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Palette loaded\n"));
 
1991
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Color maps etc. set up\n"));
 
1992
 
 
1993
    xf86DPMSInit(pScreen, xf86DPMSSet, 0);
 
1994
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- DPMS set up\n"));
 
1995
 
 
1996
    if (!VIAEnterVT_internal(pScrn, 1))
 
1997
        return FALSE;
 
1998
 
 
1999
    if (pVia->directRenderingType != DRI_2) {
 
2000
#ifdef HAVE_DRI
 
2001
        if (pVia->directRenderingType == DRI_1) {
 
2002
            if (!VIADRIFinishScreenInit(pScreen)) {
 
2003
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "direct rendering disabled\n");
 
2004
                pVia->directRenderingType = DRI_NONE;
 
2005
            } else
 
2006
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "direct rendering enabled\n");
 
2007
        }
 
2008
#endif
 
2009
        if (!pVia->NoAccel)
 
2010
            viaFinishInitAccel(pScreen);
 
2011
 
 
2012
        viaInitVideo(pScrn->pScreen);
 
2013
    }
 
2014
 
 
2015
    if (serverGeneration == 1)
 
2016
        xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
 
2017
 
 
2018
#ifdef HAVE_DEBUG
 
2019
    if (pVia->PrintVGARegs) {
 
2020
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
2021
                   "VIAScreenInit: Printing VGA registers.\n");
 
2022
        ViaVgahwPrint(VGAHWPTR(pScrn));
 
2023
    }
 
2024
 
 
2025
    if (pVia->PrintTVRegs) {
 
2026
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
 
2027
                   "VIAScreenInit: Printing TV registers.\n");
 
2028
        ViaTVPrintRegs(pScrn);
 
2029
    }
 
2030
#endif
 
2031
 
 
2032
    DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "- Done\n"));
 
2033
    return TRUE;
 
2034
}