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 | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 15 | struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, ushort *v, ushort *d) |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 16 | { |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 17 | uint vendor_device = pci_config_readd(bus, slot, func, 0); |
| 18 | ushort vendor = vendor_device & 0xffff; |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 19 | |
swissChili | 77eb147 | 2021-05-28 21:40:00 -0700 | [diff] [blame] | 20 | if (v) |
| 21 | *v = vendor; |
| 22 | |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 23 | if (vendor != 0xffff) |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 24 | { |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 25 | if (d) |
| 26 | *d = vendor_device >> 16; |
| 27 | |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 28 | return pci_vendor_by_id(vendor); |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 29 | } |
swissChili | 9321498 | 2021-05-28 21:32:26 -0700 | [diff] [blame] | 30 | return NULL; |
swissChili | ca26848 | 2021-05-28 18:31:46 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | struct pci_vendor *pci_vendor_by_id(ushort id) |
| 34 | { |
| 35 | // Find vendor using binary search |
| 36 | |
| 37 | uint start = 0, |
| 38 | max = pci_num_vendors; |
| 39 | |
| 40 | while (true) |
| 41 | { |
| 42 | if (max == start) |
| 43 | // Can't find one |
| 44 | return NULL; |
| 45 | |
| 46 | uint guess = (max - start) / 2 + start; |
| 47 | |
| 48 | if (pci_vendors[guess].id == id) |
| 49 | return &pci_vendors[guess]; |
| 50 | else if (pci_vendors[guess].id > id) |
| 51 | max = guess; |
| 52 | else |
| 53 | start = guess; |
| 54 | } |
swissChili | 0d24883 | 2021-04-08 18:16:02 -0700 | [diff] [blame] | 55 | } |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 56 | |
| 57 | void pci_print_devices() |
| 58 | { |
| 59 | kprintf("Enumerating PCI devices:\n"); |
| 60 | for (int bus = 0; bus < 0xff; bus++) |
| 61 | { |
| 62 | for (int slot = 0; slot < 32; slot++) |
| 63 | { |
| 64 | for (int func = 0; func < 8; func++) |
| 65 | { |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 66 | ushort vendor, device; |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 67 | |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 68 | struct pci_vendor *v = pci_check_vendor(bus, slot, func, &vendor, &device); |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 69 | |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 70 | if (v) |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 71 | { |
swissChili | b35a5cf | 2021-05-30 12:22:18 -0700 | [diff] [blame] | 72 | kprintf("%d %d %d --- v:0x%x d:0x%x --- %s\n", bus, slot, func, vendor, device, v->name); |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |