blob: 710ce1119456d90fc176ab5f090df458ff07e4af [file] [log] [blame]
swissChilie8491742021-04-08 20:38:06 -07001#include <dri/pci/pci.h>
swissChilica268482021-05-28 18:31:46 -07002#include <dri/pci/vendors.h>
3#include <io.h>
4#include <log.h>
swissChili0d248832021-04-08 18:16:02 -07005
6ushort pci_config_readw(uchar bus, uchar slot, uchar func, uchar offset)
7{
swissChilica268482021-05-28 18:31:46 -07008 uint address = (bus << 16) | (slot << 11) | (func << 8) | (offset << 2) | 0x80000000;
9
10 outl(PCI_CONFIG_ADDRESS, address);
11
12 return inl(PCI_CONFIG_DATA);
13}
14
15struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, ushort *v)
16{
17 ushort vendor;
18
19 if ((vendor = pci_config_readw(bus, slot, func, 0) != 0xffff))
swissChili0d248832021-04-08 18:16:02 -070020 {
swissChilica268482021-05-28 18:31:46 -070021 // TODO: check device and return here as well
22 }
23
24 if (v)
25 *v = vendor;
26
27 return pci_vendor_by_id(vendor);
28}
29
30struct pci_vendor *pci_vendor_by_id(ushort id)
31{
32 // Find vendor using binary search
33
34 uint start = 0,
35 max = pci_num_vendors;
36
37 while (true)
38 {
39 if (max == start)
40 // Can't find one
41 return NULL;
42
43 uint guess = (max - start) / 2 + start;
44
45 if (pci_vendors[guess].id == id)
46 return &pci_vendors[guess];
47 else if (pci_vendors[guess].id > id)
48 max = guess;
49 else
50 start = guess;
51 }
swissChili0d248832021-04-08 18:16:02 -070052}