1
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_pci.c,v 3.8 2002/04/09 15:59:37 tsi Exp $ */
3
#ifdef HAVE_XORG_CONFIG_H
4
#include <xorg-config.h>
13
#include "xf86_OSproc.h"
17
#define PCIADDR_TYPE long long
18
#define PCIADDR_IGNORE_FMT "%*x"
19
#define PCIADDR_FMT "%llx"
21
#define PCIADDR_TYPE long
22
#define PCIADDR_IGNORE_FMT "%*x"
23
#define PCIADDR_FMT "%lx"
26
FILE *xf86OSLinuxPCIFile = NULL;
29
xf86GetPciSizeFromOS(PCITAG tag, int index, int* bits)
33
unsigned int bus, devfn, dev, fn;
34
unsigned PCIADDR_TYPE size[7];
36
signed PCIADDR_TYPE Size;
41
if (!xf86OSLinuxPCIFile && \
42
!(xf86OSLinuxPCIFile = fopen("/proc/bus/pci/devices","r")))
45
res = fgets(c,0x1ff,xf86OSLinuxPCIFile);
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 */
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);
74
if (tag == pciTag(bus,dev,fn)) {
76
if (size[index] != 0) {
77
Size = size[index] - ((PCIADDR_TYPE) 1);
78
while (Size & ((PCIADDR_TYPE) 0x01)) {
79
Size = Size >> ((PCIADDR_TYPE) 1);
83
fseek(xf86OSLinuxPCIFile, 0L, SEEK_SET);
89
fseek(xf86OSLinuxPCIFile, 0L, SEEK_SET);
95
/* Query the kvirt address (64bit) of a BAR range from TAG */
97
xf86GetPciOffsetFromOS(PCITAG tag, int index, unsigned long* bases)
102
unsigned int bus, devfn, dev, fn;
103
unsigned PCIADDR_TYPE offset[7];
109
if (!(file = fopen("/proc/bus/pci/devices","r")))
112
res = fgets(c,0x1ff,file);
115
/*bus+dev vendorid deviceid irq */
116
"%02x%02x\t%*04x%*04x\t%*x"
117
/* 7 PCI resource base addresses */
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 */
142
if (tag == pciTag(bus,dev,fn)) {
143
/* return the offset for the index requested */
144
*bases = offset[index];
155
/* Query the kvirt address (64bit) of a BAR range from size for a given TAG */
157
xf86GetOSOffsetFromPCI(PCITAG tag, int space, unsigned long base)
162
unsigned int bus, devfn, dev, fn;
163
unsigned PCIADDR_TYPE offset[7];
164
unsigned PCIADDR_TYPE size[7];
168
if (!(file = fopen("/proc/bus/pci/devices","r")))
171
res = fgets(c,0x1ff,file);
174
/*bus+dev vendorid deviceid irq */
175
"%02x%02x\t%*04x%*04x\t%*x"
176
/* 7 PCI resource base addresses */
184
/* 7 PCI resource sizes, and then optionally a driver name */
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 */
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;
207
* remember to lop of the last 4bits of the BAR values as they are
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));
217
/* find the index of the incoming base */
218
if (base >= savePtr && base <= (savePtr + size[ndx])) {
220
return (offset[ndx] & ~(0xFUL)) + (base - savePtr);