|
| 1 | +#include "lspci.h" |
| 2 | + |
| 3 | +namespace pcm { |
| 4 | + |
| 5 | +const ccr_config ccr::skx_ccrs(44, 0xFFULL); |
| 6 | +const ccr_config ccr::icx_ccrs(48, 0xFFFULL); |
| 7 | + |
| 8 | +bool operator<(const iio_stack& lh, const iio_stack& rh) |
| 9 | +{ |
| 10 | + return lh.iio_unit_id < rh.iio_unit_id; |
| 11 | +} |
| 12 | + |
| 13 | +bool operator<(const bdf &l, const bdf &r) { |
| 14 | + if (l.domainno < r.domainno) |
| 15 | + return true; |
| 16 | + if (l.domainno > r.domainno) |
| 17 | + return false; |
| 18 | + if (l.busno < r.busno) |
| 19 | + return true; |
| 20 | + if (l.busno > r.busno) |
| 21 | + return false; |
| 22 | + if (l.devno < r.devno) |
| 23 | + return true; |
| 24 | + if (l.devno > r.devno) |
| 25 | + return false; |
| 26 | + if (l.funcno < r.funcno) |
| 27 | + return true; |
| 28 | + if (l.funcno > r.funcno) |
| 29 | + return false; |
| 30 | + |
| 31 | + return false; // bdf == bdf |
| 32 | +} |
| 33 | + |
| 34 | +void probe_capability_pci_express(struct pci *p, uint32_t cap_ptr) |
| 35 | +{ |
| 36 | + struct cap { |
| 37 | + union { |
| 38 | + struct { |
| 39 | + uint8_t id; |
| 40 | + union { |
| 41 | + uint8_t next; |
| 42 | + uint8_t cap_ptr; |
| 43 | + }; |
| 44 | + uint16_t junk; |
| 45 | + }; |
| 46 | + uint32 dw0; |
| 47 | + }; |
| 48 | + } cap; |
| 49 | + uint32 value; |
| 50 | + PciHandleType h(0, p->bdf.busno, p->bdf.devno, p->bdf.funcno); |
| 51 | + h.read32(cap_ptr, &value); //Capability pointer |
| 52 | + cap.dw0 = value; |
| 53 | + if (cap.id != 0x10 && cap.next != 0x00) { |
| 54 | + probe_capability_pci_express(p, cap.cap_ptr); |
| 55 | + } else { |
| 56 | + if (cap.id == 0x10) { // We're in PCI express capability structure |
| 57 | + h.read32(cap_ptr+0x10, &value); |
| 58 | + p->link_info = value; |
| 59 | + } else { /*Finish recursive searching but cannot find PCI express capability structure*/ } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +bool probe_pci(struct pci *p) |
| 64 | +{ |
| 65 | + uint32 value; |
| 66 | + p->exist = false; |
| 67 | + struct bdf *bdf = &p->bdf; |
| 68 | + if (PciHandleType::exists(bdf->domainno, bdf->busno, bdf->devno, bdf->funcno)) { |
| 69 | + PciHandleType h(bdf->domainno, bdf->busno, bdf->devno, bdf->funcno); |
| 70 | + // VID:DID |
| 71 | + h.read32(0x0, &value); |
| 72 | + // Invalid VID::DID |
| 73 | + if (value != (std::numeric_limits<unsigned int>::max)()) { |
| 74 | + p->offset_0 = value; |
| 75 | + h.read32(0xc, &value); |
| 76 | + p->header_type = (value >> 16) & 0x7f; |
| 77 | + if (p->header_type == 0) { |
| 78 | + // Status register |
| 79 | + h.read32(0x4, &value); |
| 80 | + // Capability list == true |
| 81 | + if (value & 0x100000) { |
| 82 | + // Capability pointer |
| 83 | + h.read32(0x34, &value); |
| 84 | + probe_capability_pci_express(p, value); |
| 85 | + } |
| 86 | + } else if (p->header_type == 1) { |
| 87 | + h.read32(0x18, &value); |
| 88 | + p->offset_18 = value; |
| 89 | + } |
| 90 | + p->exist = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return p->exist; |
| 95 | +} |
| 96 | + |
| 97 | +void print_pci(struct pci p, const PCIDB & pciDB) |
| 98 | +{ |
| 99 | + printf("Parent bridge info:"); |
| 100 | + printf("%x:%x.%d [%04x:%04x] %s %s %d P:%x S:%x S:%x ", |
| 101 | + p.bdf.busno, p.bdf.devno, p.bdf.funcno, |
| 102 | + p.vendor_id, p.device_id, |
| 103 | + (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():"unknown vendor", |
| 104 | + (pciDB.second.count(p.vendor_id) > 0 && pciDB.second.at(p.vendor_id).count(p.device_id) > 0)?pciDB.second.at(p.vendor_id).at(p.device_id).c_str():"unknown device", |
| 105 | + p.header_type, |
| 106 | + p.primary_bus_number, p.secondary_bus_number, p.subordinate_bus_number); |
| 107 | + printf("Device info:"); |
| 108 | + printf("%x:%x.%d [%04x:%04x] %s %s %d Gen%d x%d\n", |
| 109 | + p.bdf.busno, p.bdf.devno, p.bdf.funcno, |
| 110 | + p.vendor_id, p.device_id, |
| 111 | + (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():"unknown vendor", |
| 112 | + (pciDB.second.count(p.vendor_id) > 0 && pciDB.second.at(p.vendor_id).count(p.device_id) > 0)?pciDB.second.at(p.vendor_id).at(p.device_id).c_str():"unknown device", |
| 113 | + p.header_type, |
| 114 | + p.link_speed, p.link_width); |
| 115 | +} |
| 116 | + |
| 117 | +void load_PCIDB(PCIDB & pciDB) |
| 118 | +{ |
| 119 | + std::ifstream in(PCI_IDS_PATH); |
| 120 | + std::string line, item; |
| 121 | + |
| 122 | + if (!in.is_open()) |
| 123 | + { |
| 124 | +#ifndef _MSC_VER |
| 125 | + // On Unix, try PCI_IDS_PATH2 |
| 126 | + in.open(PCI_IDS_PATH2); |
| 127 | + } |
| 128 | + |
| 129 | + if (!in.is_open()) |
| 130 | + { |
| 131 | + // On Unix, try the current directory if the default path failed |
| 132 | + in.open("pci.ids"); |
| 133 | + } |
| 134 | + |
| 135 | + if (!in.is_open()) |
| 136 | + { |
| 137 | +#endif |
| 138 | + std::cerr << PCI_IDS_NOT_FOUND << "\n"; |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + int vendorID = -1; |
| 143 | + |
| 144 | + while (std::getline(in, line)) { |
| 145 | + // Ignore any line starting with # |
| 146 | + if (line.size() == 0 || line[0] == '#') |
| 147 | + continue; |
| 148 | + |
| 149 | + if (line[0] == '\t' && line[1] == '\t') |
| 150 | + { |
| 151 | + // subvendor subdevice subsystem_name |
| 152 | + continue; |
| 153 | + } |
| 154 | + if (line[0] == '\t') |
| 155 | + { |
| 156 | + int deviceID = stoi(line.substr(1,4),0,16); |
| 157 | + //std::cout << vendorID << ";" << vendorName << ";" << deviceID << ";" << line.substr(7) << "\n"; |
| 158 | + pciDB.second[vendorID][deviceID] = line.substr(7); |
| 159 | + continue; |
| 160 | + } |
| 161 | + // vendor |
| 162 | + vendorID = stoi(line.substr(0,4),0,16); |
| 163 | + pciDB.first[vendorID] = line.substr(6); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +} // Namespace pcm |
0 commit comments