~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/xfree86/os-support/linux/lnx_pci.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_pci.c,v 3.8 2002/04/09 15:59:37 tsi Exp $ */
 
2
 
 
3
#ifdef HAVE_XORG_CONFIG_H
 
4
#include <xorg-config.h>
 
5
#endif
 
6
 
 
7
#include <stdio.h>
 
8
#include <X11/X.h>
 
9
#include "os.h"
 
10
#include "xf86.h"
 
11
#include "xf86Priv.h"
 
12
#define XF86_OS_PRIVS
 
13
#include "xf86_OSproc.h"
 
14
#include "xf86Pci.h"
 
15
 
 
16
#ifdef __sparc__
 
17
#define PCIADDR_TYPE            long long
 
18
#define PCIADDR_IGNORE_FMT      "%*x"
 
19
#define PCIADDR_FMT             "%llx"
 
20
#else
 
21
#define PCIADDR_TYPE            long
 
22
#define PCIADDR_IGNORE_FMT      "%*x"
 
23
#define PCIADDR_FMT             "%lx"
 
24
#endif
 
25
 
 
26
FILE *xf86OSLinuxPCIFile = NULL;
 
27
 
 
28
Bool
 
29
xf86GetPciSizeFromOS(PCITAG tag, int index, int* bits)
 
30
{
 
31
    char c[0x200];
 
32
    char *res;
 
33
    unsigned int bus, devfn, dev, fn;
 
34
    unsigned PCIADDR_TYPE size[7];
 
35
    unsigned int num;
 
36
    signed PCIADDR_TYPE Size;
 
37
 
 
38
    if (index > 7)
 
39
        return FALSE;
 
40
    
 
41
    if (!xf86OSLinuxPCIFile && \
 
42
        !(xf86OSLinuxPCIFile = fopen("/proc/bus/pci/devices","r")))
 
43
        return FALSE;
 
44
    do {
 
45
        res = fgets(c,0x1ff,xf86OSLinuxPCIFile);
 
46
        if (res) {
 
47
            num = sscanf(res,
 
48
                         /*bus+dev vendorid deviceid irq */
 
49
                         "%02x%02x\t%*04x%*04x\t%*x"
 
50
                         /* 7 PCI resource base addresses */
 
51
                         "\t" PCIADDR_IGNORE_FMT
 
52
                         "\t" PCIADDR_IGNORE_FMT
 
53
                         "\t" PCIADDR_IGNORE_FMT
 
54
                         "\t" PCIADDR_IGNORE_FMT
 
55
                         "\t" PCIADDR_IGNORE_FMT
 
56
                         "\t" PCIADDR_IGNORE_FMT
 
57
                         "\t" PCIADDR_IGNORE_FMT
 
58
                         /* 7 PCI resource sizes, and then optionally a driver name */
 
59
                         "\t" PCIADDR_FMT
 
60
                         "\t" PCIADDR_FMT
 
61
                         "\t" PCIADDR_FMT
 
62
                         "\t" PCIADDR_FMT
 
63
                         "\t" PCIADDR_FMT
 
64
                         "\t" PCIADDR_FMT
 
65
                         "\t" PCIADDR_FMT,
 
66
                         &bus,&devfn,&size[0],&size[1],&size[2],&size[3],
 
67
                         &size[4],&size[5],&size[6]);
 
68
            if (num != 9) {  /* apparantly not 2.3 style */ 
 
69
                fseek(xf86OSLinuxPCIFile, 0L, SEEK_SET);
 
70
                return FALSE;
 
71
            }
 
72
            dev = devfn >> 3;
 
73
            fn = devfn & 0x7;
 
74
            if (tag == pciTag(bus,dev,fn)) {
 
75
                *bits = 0;
 
76
                if (size[index] != 0) {
 
77
                    Size = size[index] - ((PCIADDR_TYPE) 1);
 
78
                    while (Size & ((PCIADDR_TYPE) 0x01)) {
 
79
                        Size = Size >> ((PCIADDR_TYPE) 1);
 
80
                        (*bits)++;
 
81
                    }
 
82
                }
 
83
                fseek(xf86OSLinuxPCIFile, 0L, SEEK_SET);
 
84
                return TRUE;
 
85
            }
 
86
        }
 
87
    } while (res);
 
88
 
 
89
    fseek(xf86OSLinuxPCIFile, 0L, SEEK_SET);
 
90
    return FALSE;
 
91
}
 
92
 
 
93
 
 
94
 
 
95
/* Query the kvirt address (64bit) of a BAR range from TAG */
 
96
Bool
 
97
xf86GetPciOffsetFromOS(PCITAG tag, int index, unsigned long* bases)
 
98
{
 
99
    FILE *file;
 
100
    char c[0x200];
 
101
    char *res;
 
102
    unsigned int bus, devfn, dev, fn;
 
103
    unsigned PCIADDR_TYPE offset[7];
 
104
    unsigned int num;
 
105
 
 
106
    if (index > 7)
 
107
        return FALSE;
 
108
 
 
109
    if (!(file = fopen("/proc/bus/pci/devices","r")))
 
110
        return FALSE;
 
111
    do {
 
112
        res = fgets(c,0x1ff,file);
 
113
        if (res) {
 
114
            num = sscanf(res,
 
115
                         /*bus+dev vendorid deviceid irq */
 
116
                         "%02x%02x\t%*04x%*04x\t%*x"
 
117
                         /* 7 PCI resource base addresses */
 
118
                         "\t" PCIADDR_FMT
 
119
                         "\t" PCIADDR_FMT
 
120
                         "\t" PCIADDR_FMT
 
121
                         "\t" PCIADDR_FMT
 
122
                         "\t" PCIADDR_FMT
 
123
                         "\t" PCIADDR_FMT
 
124
                         "\t" PCIADDR_FMT
 
125
                         /* 7 PCI resource sizes, and then optionally a driver name */
 
126
                         "\t" PCIADDR_IGNORE_FMT
 
127
                         "\t" PCIADDR_IGNORE_FMT
 
128
                         "\t" PCIADDR_IGNORE_FMT
 
129
                         "\t" PCIADDR_IGNORE_FMT
 
130
                         "\t" PCIADDR_IGNORE_FMT
 
131
                         "\t" PCIADDR_IGNORE_FMT
 
132
                         "\t" PCIADDR_IGNORE_FMT,
 
133
                         &bus,&devfn,&offset[0],&offset[1],&offset[2],&offset[3],
 
134
                         &offset[4],&offset[5],&offset[6]);
 
135
            if (num != 9) {  /* apparantly not 2.3 style */
 
136
                fclose(file);
 
137
                return FALSE;
 
138
            }
 
139
 
 
140
            dev = devfn >> 3;
 
141
            fn = devfn & 0x7;
 
142
            if (tag == pciTag(bus,dev,fn)) {
 
143
                /* return the offset for the index requested */
 
144
                *bases = offset[index];
 
145
                fclose(file);
 
146
                return TRUE;
 
147
            }
 
148
        }
 
149
    } while (res);
 
150
 
 
151
    fclose(file);
 
152
    return FALSE;
 
153
}
 
154
 
 
155
/* Query the kvirt address (64bit) of a BAR range from size for a given TAG */
 
156
unsigned long
 
157
xf86GetOSOffsetFromPCI(PCITAG tag, int space, unsigned long base)
 
158
{
 
159
    FILE *file;
 
160
    char c[0x200];
 
161
    char *res;
 
162
    unsigned int bus, devfn, dev, fn;
 
163
    unsigned PCIADDR_TYPE offset[7];
 
164
    unsigned PCIADDR_TYPE size[7];
 
165
    unsigned int num;
 
166
    unsigned int ndx;
 
167
 
 
168
    if (!(file = fopen("/proc/bus/pci/devices","r")))
 
169
        return 0;
 
170
    do {
 
171
        res = fgets(c,0x1ff,file);
 
172
        if (res) {
 
173
            num = sscanf(res,
 
174
                         /*bus+dev vendorid deviceid irq */
 
175
                         "%02x%02x\t%*04x%*04x\t%*x"
 
176
                         /* 7 PCI resource base addresses */
 
177
                         "\t" PCIADDR_FMT
 
178
                         "\t" PCIADDR_FMT
 
179
                         "\t" PCIADDR_FMT
 
180
                         "\t" PCIADDR_FMT
 
181
                         "\t" PCIADDR_FMT
 
182
                         "\t" PCIADDR_FMT
 
183
                         "\t" PCIADDR_FMT
 
184
                         /* 7 PCI resource sizes, and then optionally a driver name */
 
185
                         "\t" PCIADDR_FMT
 
186
                         "\t" PCIADDR_FMT
 
187
                         "\t" PCIADDR_FMT
 
188
                         "\t" PCIADDR_FMT
 
189
                         "\t" PCIADDR_FMT
 
190
                         "\t" PCIADDR_FMT
 
191
                         "\t" PCIADDR_FMT,
 
192
                         &bus,&devfn,&offset[0],&offset[1],&offset[2],&offset[3],
 
193
                         &offset[4],&offset[5],&offset[6], &size[0], &size[1], &size[2],
 
194
                         &size[3], &size[4], &size[5], &size[6]);
 
195
            if (num != 16) {  /* apparantly not 2.3 style */
 
196
                fclose(file);
 
197
                return 0;
 
198
            }
 
199
 
 
200
            dev = devfn >> 3;
 
201
            fn = devfn & 0x7;
 
202
            if (tag == pciTag(bus,dev,fn)) {
 
203
                /* ok now look through all the BAR values of this device */
 
204
                for (ndx=0; ndx<7; ndx++) {
 
205
                    unsigned long savePtr;
 
206
                    /*
 
207
                     * remember to lop of the last 4bits of the BAR values as they are
 
208
                     * memory attributes
 
209
                     */
 
210
                    if (ndx == 6) 
 
211
                        savePtr = (0xFFFFFFF0) & 
 
212
                            pciReadLong(tag, PCI_CMD_BIOS_REG);
 
213
                    else /* this the ROM bar */
 
214
                        savePtr = (0xFFFFFFF0) & 
 
215
                            pciReadLong(tag, PCI_CMD_BASE_REG + (0x4 * ndx));
 
216
 
 
217
                    /* find the index of the incoming base */
 
218
                    if (base >= savePtr && base <= (savePtr + size[ndx])) {
 
219
                        fclose(file);
 
220
                        return (offset[ndx] & ~(0xFUL)) + (base - savePtr);
 
221
                    }
 
222
                }
 
223
            }
 
224
        }
 
225
    } while (res);
 
226
 
 
227
    fclose(file);
 
228
    return 0;
 
229
 
 
230
}