forked from mh0rst/turionpowercontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCIRegObject.cpp
241 lines (188 loc) · 4.65 KB
/
PCIRegObject.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* PCIRegObject.cpp
*
* Created on: 29/mar/2011
* Author: paolo
*/
#include "PCIRegObject.h"
#include "sysdep.h"
DWORD PCIRegObject::getPath()
{
return getPath(this->device, this->function);
}
DWORD PCIRegObject::getPath(DWORD device, DWORD function)
{
return (device << 3) + (function);
}
//Constructor
PCIRegObject::PCIRegObject()
{
this->reg_ptr=NULL;
this->absIndex=NULL;
this->nodeMask=0x0;
this->reg=0x0;
this->function=0x0;
this->device=0x0;
this->nodeCount=0x0;
}
/*
* newPCIReg initializes a new object with default values (zeros).
* Use this function if you are going to override register values
*/
void PCIRegObject::newPCIReg(DWORD device, DWORD function, DWORD reg, DWORD nodeMask)
{
DWORD mask;
unsigned int count;
this->nodeMask = nodeMask;
this->reg = reg;
this->function = function;
this->device = device;
//count as many nodes are accounted in nodeMask
mask = this->nodeMask;
count = 0;
while (mask) {
if (mask & 1) {
count++;
}
mask >>= 1;
}
this->nodeCount = count;
if (this->reg_ptr) free(this->reg_ptr);
if (this->absIndex) free(this->absIndex);
this->reg_ptr = (DWORD *) calloc(this->nodeCount, sizeof(DWORD));
this->absIndex = (unsigned int *) calloc (this->nodeCount, sizeof(unsigned int));
return;
}
/*
* readPCIReg reads a PCI Configuration Register
* Parameters are:
* - device
* - function
* - register
* - nodeMask
*
* device and function parameters are constructed togheter using the private function
* getPath. In multiprocessor machines, each function is increased for each processor/node
* in the system.
* nodeMask is a bitmask of nodes. Bit 0 will let the function read the PCI register from
* node 0, bit 1 for node 1 and so on.
*/
bool PCIRegObject::readPCIReg(DWORD device, DWORD function, DWORD reg, DWORD nodeMask)
{
DWORD mask;
unsigned int count;
DWORD nid;
this->nodeMask = nodeMask;
this->reg = reg;
this->function = function;
this->device = device;
//count as many nodes are accounted in nodeMask
mask = this->nodeMask;
count = 0;
while (mask) {
if (mask & 1) {
count++;
}
mask >>= 1;
}
this->nodeCount = count;
if (this->reg_ptr) free(this->reg_ptr);
if (this->absIndex) free (this->absIndex);
this->reg_ptr = (DWORD *) calloc(this->nodeCount, sizeof(DWORD));
this->absIndex = (unsigned int *) calloc (this->nodeCount, sizeof(unsigned int));
mask = this->nodeMask;
count = 0;
nid = 0;
while (mask)
{
if (mask & 1)
{
if (!SysReadPciConfigDwordEx(getPath(this->device+nid, this->function), this->reg, &this->reg_ptr[count]))
{
/*This is not needed since memory will be freed by destructor
free(this->reg_ptr);
free(this->absIndex);*/
this->nodeCount = 0;
return false;
}
absIndex[count] = nid;
count++;
}
nid++;
mask >>= 1;
}
return true;
}
/*
* writePCIReg writes the PCI registers to the processors using nodeMask and parameters
* set when using readPCIReg.
*/
bool PCIRegObject::writePCIReg ()
{
DWORD mask;
unsigned int count;
DWORD nid;
if (this->nodeCount==0) return true;
mask = this->nodeMask;
count=0;
nid=0;
while (mask)
{
if (mask & 1)
{
if (!SysWritePciConfigDwordEx (getPath(this->device+nid, this->function),this->reg,this->reg_ptr[count])) return false;
count++;
}
nid++;
mask >>= 1;
}
return true;
}
unsigned int PCIRegObject::indexToAbsolute (unsigned int index)
{
return this->absIndex[index];
}
/* Returns the number of nodes currently in memory of an object */
DWORD PCIRegObject::getCount ()
{
return this->nodeCount;
}
/*
* getBits returns an integer for a specific node. Base and length parameters are used to isolate
* the sector of the whole register that is interesting.
*/
DWORD PCIRegObject::getBits (unsigned int nodeNumber, unsigned int base, unsigned int length)
{
DWORD xReg;
if (this->nodeCount==0) return 0;
if (nodeNumber>=this->nodeCount) return 0;
xReg=this->reg_ptr[nodeNumber];
xReg=xReg<<(32-base-length);
xReg=xReg>>(32-length);
return xReg;
}
/*
* setBits set the bits for all the processors specified in nodeMask.
* Base and length specify the offset and the width of the sector of the register
* we're interested in.
*/
bool PCIRegObject::setBits (unsigned int base, unsigned int length, DWORD value)
{
DWORD mask;
DWORD count;
if (this->nodeCount==0) return false;
mask = -1;
mask >>= (32 - length);
mask <<= base;
value=value<<base;
value=value & mask;
mask=~mask;
for (count=0;count<this->nodeCount;count++)
this->reg_ptr[count]=(this->reg_ptr[count] & mask) | value;
return true;
}
PCIRegObject::~PCIRegObject()
{
if (this->reg_ptr) free (this->reg_ptr);
if (this->absIndex) free (this->absIndex);
}