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
|
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_pci.c,v 3.8 2002/04/09 15:59:37 tsi Exp $ */
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <stdio.h>
#include <X11/X.h>
#include <X11/os.h>
#include "xf86.h"
#include "xf86Priv.h"
#define XF86_OS_PRIVS
#include "xf86_OSproc.h"
#include "xf86Pci.h"
#ifdef __sparc__
#define PCIADDR_TYPE long long
#define PCIADDR_IGNORE_FMT "%*x"
#define PCIADDR_FMT "%llx"
#else
#define PCIADDR_TYPE long
#define PCIADDR_IGNORE_FMT "%*x"
#define PCIADDR_FMT "%lx"
#endif
Bool
xf86GetPciSizeFromOS(PCITAG tag, int index, int* bits)
{
FILE *file;
char c[0x200];
char *res;
unsigned int bus, devfn, dev, fn;
unsigned PCIADDR_TYPE size[7];
unsigned int num;
signed PCIADDR_TYPE Size;
if (index > 7)
return FALSE;
if (!(file = fopen("/proc/bus/pci/devices","r")))
return FALSE;
do {
res = fgets(c,0x1ff,file);
if (res) {
num = sscanf(res,
/*bus+dev vendorid deviceid irq */
"%02x%02x\t%*04x%*04x\t%*x"
/* 7 PCI resource base addresses */
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
/* 7 PCI resource sizes, and then optionally a driver name */
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT,
&bus,&devfn,&size[0],&size[1],&size[2],&size[3],
&size[4],&size[5],&size[6]);
if (num != 9) { /* apparantly not 2.3 style */
fclose(file);
return FALSE;
}
dev = devfn >> 3;
fn = devfn & 0x7;
if (tag == pciTag(bus,dev,fn)) {
*bits = 0;
if (size[index] != 0) {
Size = size[index] - ((PCIADDR_TYPE) 1);
while (Size & ((PCIADDR_TYPE) 0x01)) {
Size = Size >> ((PCIADDR_TYPE) 1);
(*bits)++;
}
}
fclose(file);
return TRUE;
}
}
} while (res);
fclose(file);
return FALSE;
}
/* Query the kvirt address (64bit) of a BAR range from TAG */
Bool
xf86GetPciOffsetFromOS(PCITAG tag, int index, unsigned long* bases)
{
FILE *file;
char c[0x200];
char *res;
unsigned int bus, devfn, dev, fn;
unsigned PCIADDR_TYPE offset[7];
unsigned int num;
if (index > 7)
return FALSE;
if (!(file = fopen("/proc/bus/pci/devices","r")))
return FALSE;
do {
res = fgets(c,0x1ff,file);
if (res) {
num = sscanf(res,
/*bus+dev vendorid deviceid irq */
"%02x%02x\t%*04x%*04x\t%*x"
/* 7 PCI resource base addresses */
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
/* 7 PCI resource sizes, and then optionally a driver name */
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT
"\t" PCIADDR_IGNORE_FMT,
&bus,&devfn,&offset[0],&offset[1],&offset[2],&offset[3],
&offset[4],&offset[5],&offset[6]);
if (num != 9) { /* apparantly not 2.3 style */
fclose(file);
return FALSE;
}
dev = devfn >> 3;
fn = devfn & 0x7;
if (tag == pciTag(bus,dev,fn)) {
/* return the offset for the index requested */
*bases = offset[index];
fclose(file);
return TRUE;
}
}
} while (res);
fclose(file);
return FALSE;
}
/* Query the kvirt address (64bit) of a BAR range from size for a given TAG */
unsigned long
xf86GetOSOffsetFromPCI(PCITAG tag, int space, unsigned long base)
{
FILE *file;
char c[0x200];
char *res;
unsigned int bus, devfn, dev, fn;
unsigned PCIADDR_TYPE offset[7];
unsigned PCIADDR_TYPE size[7];
unsigned int num;
unsigned int ndx;
if (!(file = fopen("/proc/bus/pci/devices","r")))
return NULL;
do {
res = fgets(c,0x1ff,file);
if (res) {
num = sscanf(res,
/*bus+dev vendorid deviceid irq */
"%02x%02x\t%*04x%*04x\t%*x"
/* 7 PCI resource base addresses */
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
/* 7 PCI resource sizes, and then optionally a driver name */
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT
"\t" PCIADDR_FMT,
&bus,&devfn,&offset[0],&offset[1],&offset[2],&offset[3],
&offset[4],&offset[5],&offset[6], &size[0], &size[1], &size[2],
&size[3], &size[4], &size[5], &size[6]);
if (num != 16) { /* apparantly not 2.3 style */
fclose(file);
return NULL;
}
dev = devfn >> 3;
fn = devfn & 0x7;
if (tag == pciTag(bus,dev,fn)) {
/* ok now look through all the BAR values of this device */
for (ndx=0; ndx<7; ndx++) {
unsigned long savePtr;
/*
* remember to lop of the last 4bits of the BAR values as they are
* memory attributes
*/
if (ndx == 6)
savePtr = (0xFFFFFFF0) &
pciReadLong(tag, PCI_CMD_BIOS_REG);
else /* this the ROM bar */
savePtr = (0xFFFFFFF0) &
pciReadLong(tag, PCI_CMD_BASE_REG + (0x4 * ndx));
/* find the index of the incoming base */
if (base >= savePtr && base <= (savePtr + size[ndx])) {
fclose(file);
return ( ~(0xFUL) & (offset[ndx] + (base - savePtr)));
}
}
}
}
} while (res);
fclose(file);
return NULL;
}
|