blob: f9ebadb00d8bbe99f0562a0a1ec187a1cee03c1e [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{
swissChili77eb1472021-05-28 21:40:00 -070017 uint vendor = pci_config_readd(bus, slot, func, 0);
swissChilica268482021-05-28 18:31:46 -070018
swissChili77eb1472021-05-28 21:40:00 -070019 if (v)
20 *v = vendor;
21
22 if (vendor != ~0)
swissChili0d248832021-04-08 18:16:02 -070023 {
swissChili93214982021-05-28 21:32:26 -070024 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}
swissChili402a3832021-05-29 21:41:31 -070052
53void 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}