blob: 39622a512afa0333b9f91222c1b9311fab2df89f [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
swissChili93214982021-05-28 21:32:26 -07006uint pci_config_readd(uchar bus, uchar slot, uchar func, uchar offset)
swissChili0d248832021-04-08 18:16:02 -07007{
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
swissChili93214982021-05-28 21:32:26 -070015struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, uint *v)
swissChilica268482021-05-28 18:31:46 -070016{
swissChili93214982021-05-28 21:32:26 -070017 uint vendor;
swissChilica268482021-05-28 18:31:46 -070018
swissChili93214982021-05-28 21:32:26 -070019 if ((vendor = pci_config_readd(bus, slot, func, 0) != ~0))
swissChili0d248832021-04-08 18:16:02 -070020 {
swissChili93214982021-05-28 21:32:26 -070021 if (v)
22 *v = vendor;
23
24 return pci_vendor_by_id(vendor);
swissChilica268482021-05-28 18:31:46 -070025 }
swissChili93214982021-05-28 21:32:26 -070026 return NULL;
swissChilica268482021-05-28 18:31:46 -070027}
28
29struct pci_vendor *pci_vendor_by_id(ushort id)
30{
31 // Find vendor using binary search
32
33 uint start = 0,
34 max = pci_num_vendors;
35
36 while (true)
37 {
38 if (max == start)
39 // Can't find one
40 return NULL;
41
42 uint guess = (max - start) / 2 + start;
43
44 if (pci_vendors[guess].id == id)
45 return &pci_vendors[guess];
46 else if (pci_vendors[guess].id > id)
47 max = guess;
48 else
49 start = guess;
50 }
swissChili0d248832021-04-08 18:16:02 -070051}