blob: a5416b665d6ffc0d4a56914edffe8e7192526028 [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
swissChilib35a5cf2021-05-30 12:22:18 -070015struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, ushort *v, ushort *d)
swissChilica268482021-05-28 18:31:46 -070016{
swissChilib35a5cf2021-05-30 12:22:18 -070017 uint vendor_device = pci_config_readd(bus, slot, func, 0);
18 ushort vendor = vendor_device & 0xffff;
swissChilica268482021-05-28 18:31:46 -070019
swissChili77eb1472021-05-28 21:40:00 -070020 if (v)
21 *v = vendor;
22
swissChilib35a5cf2021-05-30 12:22:18 -070023 if (vendor != 0xffff)
swissChili0d248832021-04-08 18:16:02 -070024 {
swissChilib35a5cf2021-05-30 12:22:18 -070025 if (d)
26 *d = vendor_device >> 16;
27
swissChili93214982021-05-28 21:32:26 -070028 return pci_vendor_by_id(vendor);
swissChilica268482021-05-28 18:31:46 -070029 }
swissChili93214982021-05-28 21:32:26 -070030 return NULL;
swissChilica268482021-05-28 18:31:46 -070031}
32
33struct 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 }
swissChili0d248832021-04-08 18:16:02 -070055}
swissChili402a3832021-05-29 21:41:31 -070056
57void 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 {
swissChilib35a5cf2021-05-30 12:22:18 -070066 ushort vendor, device;
swissChili402a3832021-05-29 21:41:31 -070067
swissChilib35a5cf2021-05-30 12:22:18 -070068 struct pci_vendor *v = pci_check_vendor(bus, slot, func, &vendor, &device);
swissChili402a3832021-05-29 21:41:31 -070069
swissChilib35a5cf2021-05-30 12:22:18 -070070 if (v)
swissChili402a3832021-05-29 21:41:31 -070071 {
swissChilib35a5cf2021-05-30 12:22:18 -070072 kprintf("%d %d %d --- v:0x%x d:0x%x --- %s\n", bus, slot, func, vendor, device, v->name);
swissChili402a3832021-05-29 21:41:31 -070073 }
74 }
75 }
76 }
77}