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 | |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 6 | uint pci_config_readd(uchar bus, uchar slot, uchar func, uchar offset) |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 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 | |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 15 | struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, uint *v) |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 16 | { |
swissChili | 77eb147 | 2021-05-28 21:40:00 -0700 | [diff] [blame] | 17 | uint vendor = pci_config_readd(bus, slot, func, 0); |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 18 | |
swissChili | 77eb147 | 2021-05-28 21:40:00 -0700 | [diff] [blame] | 19 | if (v) |
| 20 | *v = vendor; |
| 21 | |
| 22 | if (vendor != ~0) |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 23 | { |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 24 | return pci_vendor_by_id(vendor); |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 25 | } |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 26 | return NULL; |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | struct 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 | } |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 51 | } |