~kris/9p

9hist

ref: 5d3cbbbf16d2126d13baf2fa3e0d5edf4386f7e6 9hist/pc/pci.c -rw-r--r-- 1.4 KiB
5d3cbbbf — David du Colombier Plan 9 from Bell Labs 1995-07-22 31 years ago
                                                                                
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
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"

static Lock pcicfglock;

/*
 * Read a chunk of PCI configuration space.
 * Assumes arguments are within limits and regno and
 * nbytes are DWORD aligned.
 */
void
pcicfgr(int busno, int devno, int funcno, int regno, void* data, int nbytes)
{
	ulong* p;
	int base, len;

	lock(&pcicfglock);
	outb(PCIcse, 0x80|((funcno & 0x07)<<1));
	outb(PCIforward, busno);

	base = (0xC000|(devno<<8)) + regno;
	p = data;
	for(len = nbytes/sizeof(ulong); len > 0; len--){
		*p = inl(base);
		p++;
		base += sizeof(*p);
	}

	outb(PCIcse, 0x00);
	unlock(&pcicfglock);
}

void
pcicfgw(int busno, int devno, int funcno, int regno, void* data, int nbytes)
{
	ulong* p;
	int base, len;

	lock(&pcicfglock);
	outb(PCIcse, 0x80|((funcno & 0x07)<<1));
	outb(PCIforward, busno);

	base = (0xC000|(devno<<8)) + regno;
	p = data;
	for(len = nbytes/sizeof(ulong); len > 0; len--){
		outl(base, *p);
		p++;
		base += sizeof(*p);
	}

	outb(PCIcse, 0x00);
	unlock(&pcicfglock);
}

int
pcimatch(int busno, int devno, PCIcfg* pcicfg)
{
	ulong l;

	while(devno < MaxPCI){
		l = 0;
		pcicfgr(busno, devno, 0, 0, &l, sizeof(ulong));
		devno++;
		if((l & 0xFFFF) != pcicfg->vid)
			continue;
		if(pcicfg->did && ((l>>16) & 0xFFFF) != pcicfg->did)
			continue;
		pcicfgr(busno, devno-1, 0, 0, pcicfg, sizeof(PCIcfg));
		return devno;
	}
	return -1;
}