forked from intel/pcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lspci.h
216 lines (199 loc) · 6.33 KB
/
lspci.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef CPUCounters_LSPCI_H
#define CPUCounters_LSPCI_H
#include <vector>
#include <fstream>
#include "cpucounters.h"
using namespace std;
typedef uint32_t h_id;
typedef uint32_t v_id;
typedef map<std::pair<h_id,v_id>,uint64_t> ctr_data;
typedef vector<ctr_data> stack_content;
typedef vector<stack_content> result_content;
struct bdf {
uint8_t busno;
uint8_t devno;
uint8_t funcno;
};
struct pci {
bool exist = false;
struct bdf bdf;
union {
struct {
uint16_t vendor_id;
uint16_t device_id;
};
uint32_t offset_0;
};
int8_t header_type;
union {
struct {
uint8_t primary_bus_number;
uint8_t secondary_bus_number;
uint8_t subordinate_bus_number;
uint8_t junk;
};
uint32_t offset_18;
};
union {
struct {
uint16_t link_ctrl;
union {
struct {
uint16_t link_speed : 4;
uint16_t link_width : 6;
uint16_t undefined : 1;
uint16_t link_trained : 1;
};
uint16_t link_sta;
};
};
uint32_t link_info;
};
};
struct counter {
string h_event_name;
string v_event_name;
IIOPMUCNTCTLRegister Opcodes;
int idx; /* Some counters need to be placed in specific index */
int multiplier;
int divider;
uint32_t h_id;
uint32_t v_id;
vector<result_content> data;
};
struct iio_skx {
struct {
struct {
struct pci root_pci_dev; /* single device represent root port */
vector<struct pci> child_pci_devs; /* Contain child switch and end-point devices */
} parts[4]; /* part 0, 1, 2, 3 */
uint8_t busno; /* holding busno for each IIO stack */
string stack_name;
vector<uint64_t> values;
} stacks[6]; /* iio stack 0, 1, 2, 3, 4, 5 */
uint32_t socket_id;
};
bool operator < (const bdf &l, const bdf &r) {
if (l.busno < r.busno)
return true;
if (l.busno > r.busno)
return false;
if (l.devno < r.devno)
return true;
if (l.devno > r.devno)
return false;
if (l.funcno < r.funcno)
return true;
if (l.funcno > r.funcno)
return false;
return false; // bdf == bdf
};
void probe_capability_pci_express(struct pci *p, uint32_t cap_ptr)
{
struct cap {
union {
struct {
uint8_t id;
union {
uint8_t next;
uint8_t cap_ptr;
};
uint16_t junk;
};
uint32 dw0;
};
} cap;
uint32 value;
PciHandleType h(0, p->bdf.busno, p->bdf.devno, p->bdf.funcno);
h.read32(cap_ptr, &value); //Capability pointer
cap.dw0 = value;
if (cap.id != 0x10 && cap.next != 0x00) {
probe_capability_pci_express(p, cap.cap_ptr);
} else {
if (cap.id == 0x10) { // We're in PCI express capability structure
h.read32(cap_ptr+0x10, &value);
p->link_info = value;
} else { /*Finish recursive searching but cannot find PCI express capability structure*/ }
}
}
void probe_pci(struct pci *p)
{
uint32 value;
struct bdf *bdf = &p->bdf;
if (PciHandleType::exists(bdf->busno, bdf->devno, bdf->funcno)) {
p->exist = true;
PciHandleType h(0, bdf->busno, bdf->devno, bdf->funcno);
h.read32(0x0, &value); //VID:DID
p->offset_0 = value;
h.read32(0xc, &value);
p->header_type = (value >> 16) & 0x7f;
if (p->header_type == 0) {
h.read32(0x4, &value); //Status register
if (value & 0x100000) {//Capability list == true
h.read32(0x34, &value); //Capability pointer
probe_capability_pci_express(p, value);
}
} else if (p->header_type == 1) {
h.read32(0x18, &value);
p->offset_18 = value;
}
}
else
p->exist = false;
}
/*
first : [vendorID] -> vencor name
second : [vendorID][deviceID] -> device name
*/
typedef std::pair< std::map<int, std::string> ,std::map< int, std::map<int, std::string> > > PCIDB;
void print_pci(struct pci p, const PCIDB & pciDB)
{
printf("Parent bridge info:");
printf("%x:%x.%d [%04x:%04x] %s %s %d P:%x S:%x S:%x ",
p.bdf.busno, p.bdf.devno, p.bdf.funcno,
p.vendor_id, p.device_id,
(pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():"unknown vendor",
(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",
p.header_type,
p.primary_bus_number, p.secondary_bus_number, p.subordinate_bus_number);
printf("Device info:");
printf("%x:%x.%d [%04x:%04x] %s %s %d Gen%d x%d\n",
p.bdf.busno, p.bdf.devno, p.bdf.funcno,
p.vendor_id, p.device_id,
(pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():"unknown vendor",
(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",
p.header_type,
p.link_speed, p.link_width);
}
void load_PCIDB(PCIDB & pciDB)
{
std::ifstream in("pci.ids");
std::string line, item;
if (!in.is_open())
{
std::cerr << "pci.ids file is not available. Download it from https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids " << endl;
return;
}
int vendorID = -1;
while (std::getline(in, line)) {
// Ignore any line starting with #
if (line.size() == 0 || line[0] == '#')
continue;
if (line[0] == '\t' && line[1] == '\t')
{
// subvendor subdevice subsystem_name
continue;
}
if (line[0] == '\t')
{
int deviceID = stoi(line.substr(1,4),0,16);
//std::cout << vendorID << ";" << vendorName << ";" << deviceID << ";"<< line.substr(7) << endl;
pciDB.second[vendorID][deviceID] = line.substr(7);
continue;
}
// vendor
vendorID = stoi(line.substr(0,4),0,16);
pciDB.first[vendorID] = line.substr(6);
}
}
#endif