swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 1 | #include <dri/pci/pci.h> |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 2 | #include <dri/pci/vendors.h> |
| 3 | #include <io.h> |
| 4 | #include <log.h> |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 5 | |
| 6 | ushort pci_config_readw(uchar bus, uchar slot, uchar func, uchar offset) |
| 7 | { |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 8 | 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 | |
| 15 | struct 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)) |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 20 | { |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 21 | // 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 | |
| 30 | struct 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 | } |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 52 | } |