aboutsummaryrefslogtreecommitdiff
path: root/board/MAI/bios_emulator/scitech/src/pm/tests/uswc.c
blob: 0892e25f3a5c441d656afddb9a23d596245ac647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
*
*                   SciTech OS Portability Manager Library
*
*  ========================================================================
*
*    The contents of this file are subject to the SciTech MGL Public
*    License Version 1.0 (the "License"); you may not use this file
*    except in compliance with the License. You may obtain a copy of
*    the License at http://www.scitechsoft.com/mgl-license.txt
*
*    Software distributed under the License is distributed on an
*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
*    implied. See the License for the specific language governing
*    rights and limitations under the License.
*
*    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
*    The Initial Developer of the Original Code is SciTech Software, Inc.
*    All Rights Reserved.
*
*  ========================================================================
*
* Language:     ANSI C
* Environment:  any
*
* Description:  Simple test program to test the write combine functions.
*
*               Note that this program should never be used in a production
*               environment, because write combining needs to be handled
*               with more intimate knowledge of the display hardware than
*               you can obtain by simply examining the PCI configuration
*               space.
*
****************************************************************************/

#include "pmapi.h"
#include "pcilib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

/*------------------------- Global Variables ------------------------------*/

static int              NumPCI = -1;
static PCIDeviceInfo    *PCI;
static int              *BridgeIndex;
static int              *DeviceIndex;
static int              NumBridges;
static PCIDeviceInfo    *AGPBridge = NULL;
static int              NumDevices;

/*-------------------------- Implementation -------------------------------*/

/****************************************************************************
RETURNS:
Number of display devices found.

REMARKS:
This function enumerates the number of available display devices on the
PCI bus, and returns the number found.
****************************************************************************/
static int PCI_enumerateDevices(void)
{
    int             i,j;
    PCIDeviceInfo   *info;

    // If this is the first time we have been called, enumerate all
    // devices on the PCI bus.
    if (NumPCI == -1) {
        if ((NumPCI = PCI_getNumDevices()) == 0)
            return -1;
        PCI = malloc(NumPCI * sizeof(PCI[0]));
        BridgeIndex = malloc(NumPCI * sizeof(BridgeIndex[0]));
        DeviceIndex = malloc(NumPCI * sizeof(DeviceIndex[0]));
        if (!PCI || !BridgeIndex || !DeviceIndex)
            return -1;
        for (i = 0; i < NumPCI; i++)
            PCI[i].dwSize = sizeof(PCI[i]);
        if (PCI_enumerate(PCI) == 0)
            return -1;

        // Build a list of all PCI bridge devices
        for (i = 0,NumBridges = 0,BridgeIndex[0] = -1; i < NumPCI; i++) {
            if (PCI[i].BaseClass == PCI_BRIDGE_CLASS)
                BridgeIndex[NumBridges++] = i;
            }

        // Now build a list of all display class devices
        for (i = 0,NumDevices = 1,DeviceIndex[0] = -1; i < NumPCI; i++) {
            if (PCI_IS_DISPLAY_CLASS(&PCI[i])) {
                if ((PCI[i].Command & 0x3) == 0x3)
                    DeviceIndex[0] = i;
                else
                    DeviceIndex[NumDevices++] = i;
                if (PCI[i].slot.p.Bus != 0) {
                    // This device is on a different bus than the primary
                    // PCI bus, so it is probably an AGP device. Find the
                    // AGP bus device that controls that bus so we can
                    // control it.
                    for (j = 0; j < NumBridges; j++) {
                        info = (PCIDeviceInfo*)&PCI[BridgeIndex[j]];
                        if (info->u.type1.SecondayBusNumber == PCI[i].slot.p.Bus) {
                            AGPBridge = info;
                            break;
                            }
                        }
                    }
                }
            }
        }
    return NumDevices;
}

/****************************************************************************
REMARKS:
Enumerates useful information about attached display devices.
****************************************************************************/
static void ShowDisplayDevices(void)
{
    int i,index;

    printf("Displaying enumeration of %d PCI display devices\n", NumDevices);
    printf("\n");
    printf("DeviceID  SubSystem  Base10h  (length  )  Base14h  (length  )\n");
    for (index = 0; index < NumDevices; index++) {
        i = DeviceIndex[index];
        printf("%04X:%04X %04X:%04X  %08lX (%6ld KB) %08lX (%6ld KB)\n",
            PCI[i].VendorID,
            PCI[i].DeviceID,
            PCI[i].u.type0.SubSystemVendorID,
            PCI[i].u.type0.SubSystemID,
            PCI[i].u.type0.BaseAddress10,
            PCI[i].u.type0.BaseAddress10Len / 1024,
            PCI[i].u.type0.BaseAddress14,
            PCI[i].u.type0.BaseAddress14Len / 1024);
        }
    printf("\n");
}

/****************************************************************************
REMARKS:
Dumps the value for a write combine region to the display.
****************************************************************************/
static char *DecodeWCType(
    uint type)
{
    static char *names[] = {
        "UNCACHABLE",
        "WRCOMB",
        "UNKNOWN",
        "UNKNOWN",
        "WRTHROUGH",
        "WRPROT",
        "WRBACK",
        };
    if (type <= PM_MTRR_MAX)
        return names[type];
    return "UNKNOWN";
}

/****************************************************************************
REMARKS:
Dumps the value for a write combine region to the display.
****************************************************************************/
static void PMAPI EnumWriteCombine(
    ulong base,
    ulong length,
    uint type)
{
    printf("%08lX %-10ld %s\n", base, length / 1024, DecodeWCType(type));
}

/****************************************************************************
PARAMETERS:
err - Error to log

REMARKS:
Function to log an error message if the MTRR write combining attempt failed.
****************************************************************************/
static void LogMTRRError(
    int err)
{
    if (err == PM_MTRR_ERR_OK)
        return;
    switch (err) {
        case PM_MTRR_NOT_SUPPORTED:
            printf("Failed: MTRR is not supported by host CPU\n");
            break;
        case PM_MTRR_ERR_PARAMS:
            printf("Failed: Invalid parameters passed to PM_enableWriteCombined!\n");
            break;
        case PM_MTRR_ERR_NOT_4KB_ALIGNED:
            printf("Failed: Address is not 4Kb aligned!\n");
            break;
        case PM_MTRR_ERR_BELOW_1MB:
            printf("Failed: Addresses below 1Mb cannot be write combined!\n");
            break;
        case PM_MTRR_ERR_NOT_ALIGNED:
            printf("Failed: Address is not correctly aligned for processor!\n");
            break;
        case PM_MTRR_ERR_OVERLAP:
            printf("Failed: Address overlaps an existing region!\n");
            break;
        case PM_MTRR_ERR_TYPE_MISMATCH:
            printf("Failed: Adress is contained with existing region, but type is different!\n");
            break;
        case PM_MTRR_ERR_NONE_FREE:
            printf("Failed: Out of MTRR registers!\n");
            break;
        case PM_MTRR_ERR_NOWRCOMB:
            printf("Failed: This processor does not support write combining!\n");
            break;
        case PM_MTRR_ERR_NO_OS_SUPPORT:
            printf("Failed: MTRR is not supported by host OS\n");
            break;
        default:
            printf("Failed: UNKNOWN ERROR!\n");
            break;
        }
    exit(-1);
}

/****************************************************************************
REMARKS:
Shows all write combine regions.
****************************************************************************/
static void ShowWriteCombine(void)
{
    printf("Base     Length(KB) Type\n");
    LogMTRRError(PM_enumWriteCombine(EnumWriteCombine));
    printf("\n");
}

/****************************************************************************
REMARKS:
Dumps the value for a write combine region to the display.
****************************************************************************/
static void EnableWriteCombine(void)
{
    int i,index;

    for (index = 0; index < NumDevices; index++) {
        i = DeviceIndex[index];
        if (PCI[i].u.type0.BaseAddress10 & 0x8) {
            LogMTRRError(PM_enableWriteCombine(
                PCI[i].u.type0.BaseAddress10 & 0xFFFFFFF0,
                PCI[i].u.type0.BaseAddress10Len,
                PM_MTRR_WRCOMB));
            }
        if (PCI[i].u.type0.BaseAddress14 & 0x8) {
            LogMTRRError(PM_enableWriteCombine(
                PCI[i].u.type0.BaseAddress14 & 0xFFFFFFF0,
                PCI[i].u.type0.BaseAddress14Len,
                PM_MTRR_WRCOMB));
            }
        }
    printf("\n");
    ShowDisplayDevices();
    ShowWriteCombine();
}

/****************************************************************************
REMARKS:
Dumps the value for a write combine region to the display.
****************************************************************************/
static void DisableWriteCombine(void)
{
    int i,index;

    for (index = 0; index < NumDevices; index++) {
        i = DeviceIndex[index];
        if (PCI[i].u.type0.BaseAddress10 & 0x8) {
            LogMTRRError(PM_enableWriteCombine(
                PCI[i].u.type0.BaseAddress10 & 0xFFFFFFF0,
                PCI[i].u.type0.BaseAddress10Len,
                PM_MTRR_UNCACHABLE));
            }
        if (PCI[i].u.type0.BaseAddress14 & 0x8) {
            LogMTRRError(PM_enableWriteCombine(
                PCI[i].u.type0.BaseAddress14 & 0xFFFFFFF0,
                PCI[i].u.type0.BaseAddress14Len,
                PM_MTRR_UNCACHABLE));
            }
        }
    printf("\n");
    ShowDisplayDevices();
    ShowWriteCombine();
}

int main(int argc,char *argv[])
{
    PM_init();
    if (PCI_enumerateDevices() < 1) {
        printf("No PCI display devices found!\n");
        return -1;
        }
    if (argc < 2) {
        printf("usage: uswc [-show -on -off]\n\n");
        ShowDisplayDevices();
        return -1;
        }
    if (stricmp(argv[1],"-show") == 0)
        ShowWriteCombine();
    else if (stricmp(argv[1],"-on") == 0)
        EnableWriteCombine();
    else if (stricmp(argv[1],"-off") == 0)
        DisableWriteCombine();
    return 0;
}