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 | } |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame^] | 52 | |
| 53 | void pci_print_devices() |
| 54 | { |
| 55 | kprintf("Enumerating PCI devices:\n"); |
| 56 | for (int bus = 0; bus < 0xff; bus++) |
| 57 | { |
| 58 | for (int slot = 0; slot < 32; slot++) |
| 59 | { |
| 60 | for (int func = 0; func < 8; func++) |
| 61 | { |
| 62 | uint vendor; |
| 63 | |
| 64 | struct pci_vendor *v = pci_check_vendor(bus, slot, func, &vendor); |
| 65 | |
| 66 | if (vendor != ~0) |
| 67 | { |
| 68 | kprintf("%d %d %d --- 0x%x --- %s\n", bus, slot, func, vendor, v->name); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |