-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathcpuid.cpp
More file actions
648 lines (626 loc) · 19.6 KB
/
cpuid.cpp
File metadata and controls
648 lines (626 loc) · 19.6 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* This file is part of the Vc library. {{{
Copyright © 2011-2015 Matthias Kretz <kretz@kde.org>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the names of contributing organizations nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}}}*/
#include <Vc/cpuid.h>
#include <Vc/global.h>
namespace Vc_VERSIONED_NAMESPACE
{
CpuId::uint CpuId::s_ecx0 = 0;
CpuId::uint CpuId::s_logicalProcessors = 0;
CpuId::uint CpuId::s_processorFeaturesC = 0;
CpuId::uint CpuId::s_processorFeaturesD = 0;
CpuId::uint CpuId::s_processorFeatures7B = 0;
CpuId::uint CpuId::s_processorFeatures7C = 0;
CpuId::uint CpuId::s_processorFeatures8C = 0;
CpuId::uint CpuId::s_processorFeatures8D = 0;
CpuId::uint CpuId::s_L1Instruction = 0;
CpuId::uint CpuId::s_L1Data = 0;
CpuId::uint CpuId::s_L2Data = 0;
CpuId::uint CpuId::s_L3Data = 0;
CpuId::ushort CpuId::s_L1InstructionLineSize = 0;
CpuId::ushort CpuId::s_L1DataLineSize = 0;
CpuId::ushort CpuId::s_L2DataLineSize = 0;
CpuId::ushort CpuId::s_L3DataLineSize = 0;
CpuId::uint CpuId::s_L1Associativity = 0;
CpuId::uint CpuId::s_L2Associativity = 0;
CpuId::uint CpuId::s_L3Associativity = 0;
CpuId::ushort CpuId::s_prefetch = 32; // The Intel ORM says that if Vc_CPUID(2) doesn't set the prefetch size it is 32
CpuId::uchar CpuId::s_brandIndex = 0;
CpuId::uchar CpuId::s_cacheLineSize = 0;
CpuId::uchar CpuId::s_processorModel = 0;
CpuId::uchar CpuId::s_processorFamily = 0;
CpuId::ProcessorType CpuId::s_processorType = CpuId::IntelReserved;
bool CpuId::s_noL2orL3 = false;
#ifdef _WIN32
}
// better not include intrin.h inside the Vc namespace :)
#include <intrin.h>
namespace Vc_VERSIONED_NAMESPACE
{
#define Vc_CPUID(leaf) \
do { \
int out[4]; \
__cpuid(out, leaf); \
eax = out[0]; \
ebx = out[1]; \
ecx = out[2]; \
edx = out[3]; \
} while (false)
#define Vc_CPUID_C(leaf, ecx_) \
do { \
int out[4]; \
__cpuidex(out, leaf, ecx_); \
eax = out[0]; \
ebx = out[1]; \
ecx = out[2]; \
edx = out[3]; \
} while (false)
#elif defined(__i386__) && defined(__PIC__)
// %ebx may be the PIC register.
static inline void _Vc_cpuid(int leaf, unsigned int &eax, unsigned int &ebx, unsigned int &ecx, unsigned int &edx)
{
int tmpb;
asm("mov %%ebx, %[tmpb]\n\t"
"cpuid\n\t"
"mov %%ebx, %[ebx]\n\t"
"mov %[tmpb], %%ebx\n\t"
: [tmpb]"=m"(tmpb), "=a"(eax), [ebx] "=m"(ebx), "+c"(ecx), "=d"(edx)
: [leaf] "a"(leaf)
);
}
#define Vc_CPUID(leaf) \
ecx = 0; \
_Vc_cpuid(leaf, eax, ebx, ecx, edx)
#define Vc_CPUID_C(leaf, ecx_) \
ecx = ecx_; \
_Vc_cpuid(leaf, eax, ebx, ecx, edx)
#else
#define Vc_CPUID(leaf) \
__asm__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(leaf))
#define Vc_CPUID_C(leaf, ecx_) \
__asm__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(leaf), "c"(ecx_))
#endif
static unsigned int CpuIdAmdAssociativityTable(int bits)
{
switch (bits) {
case 0x0: return 0;
case 0x1: return 1;
case 0x2: return 2;
case 0x4: return 4;
case 0x6: return 8;
case 0x8: return 16;
case 0xA: return 32;
case 0xB: return 48;
case 0xC: return 64;
case 0xD: return 96;
case 0xE: return 128;
case 0xF: return 0xff;
}
return 0xffffffffu;
}
void CpuId::init()
{
{
static bool done = false;
if (done) return;
done = true;
}
uint eax, ebx, ecx, edx;
Vc_CPUID(0);
s_ecx0 = ecx;
Vc_CPUID(1);
s_processorFeaturesC = ecx;
s_processorFeaturesD = edx;
s_processorModel = (eax & 0x000000f0) >> 4;
s_processorFamily = (eax & 0x00000f00) >> 8;
if (isAmd()) {
if (s_processorFamily >= 0xf) {
const uchar processorFamilyExt = (eax & 0x0ff00000) >> 16;
s_processorFamily += processorFamilyExt;
const uchar processorModelExt = (eax & 0x000f0000) >> 12;
s_processorModel += processorModelExt;
}
} else if (s_processorFamily == 0xf) {
const uchar processorFamilyExt = (eax & 0x0ff00000) >> 16;
s_processorFamily += processorFamilyExt;
const uchar processorModelExt = (eax & 0x000f0000) >> 12;
s_processorModel += processorModelExt;
} else if (s_processorFamily == 0x6) {
const uchar processorModelExt = (eax & 0x000f0000) >> 12;
s_processorModel += processorModelExt;
} else if (s_processorFamily == 0xB) {
const uchar processorFamilyExt = (eax & 0x0ff00000) >> 16;
s_processorFamily += processorFamilyExt;
const uchar processorModelExt = (eax & 0x000f0000) >> 12;
s_processorModel += processorModelExt;
}
s_processorType = static_cast<ProcessorType>((eax & 0x00003000) >> 12);
s_brandIndex = ebx & 0xff;
ebx >>= 8;
s_cacheLineSize = ebx & 0xff;
ebx >>= 8;
s_logicalProcessors = ebx & 0xff;
Vc_CPUID_C(7, 0);
s_processorFeatures7B = ebx;
s_processorFeatures7C = ecx;
Vc_CPUID(0x80000001);
s_processorFeatures8C = ecx;
s_processorFeatures8D = edx;
if (isAmd()) {
s_prefetch = cacheLineSize();
Vc_CPUID(0x80000005);
s_L1DataLineSize = ecx & 0xff;
s_L1Data = (ecx >> 24) * 1024;
s_L1Associativity = (ecx >> 16) & 0xff;
s_L1InstructionLineSize = edx & 0xff;
s_L1Instruction = (edx >> 24) * 1024;
Vc_CPUID(0x80000006);
s_L2DataLineSize = ecx & 0xff;
s_L2Data = (ecx >> 16) * 1024;
s_L2Associativity = CpuIdAmdAssociativityTable((ecx >> 12) & 0xf);
s_L3DataLineSize = edx & 0xff;
s_L3Data = (edx >> 18) * 512 * 1024;
s_L3Associativity = CpuIdAmdAssociativityTable((ecx >> 12) & 0xf);
return;
}
// Intel only
int repeat = 0;
bool checkLeaf4 = false;
do {
Vc_CPUID(2);
if (repeat == 0) {
if (eax == 0 && ebx == 0 && ecx == 0 && edx == 0) {
// this is the case on MIC
checkLeaf4 = true;
break;
}
repeat = eax & 0xff;
}
if (0 == (0x80000000u & eax)) {
for (int i = 0; i < 3; ++i) {
eax >>= 8;
interpret(eax & 0xff, &checkLeaf4);
}
}
if (0 == (0x80000000u & ebx)) {
for (int i = 0; i < 4; ++i) {
interpret(ebx & 0xff, &checkLeaf4);
ebx >>= 8;
}
}
if (0 == (0x80000000u & ecx)) {
for (int i = 0; i < 4; ++i) {
interpret(ecx & 0xff, &checkLeaf4);
ecx >>= 8;
}
}
if (0 == (0x80000000u & edx)) {
for (int i = 0; i < 4; ++i) {
interpret(edx & 0xff, &checkLeaf4);
edx >>= 8;
}
}
} while (--repeat > 0);
if (checkLeaf4) {
s_prefetch = cacheLineSize();
if (s_prefetch == 0) {
s_prefetch = 64;
}
eax = 1;
for (int i = 0; eax & 0x1f; ++i) {
Vc_CPUID_C(4, i);
const int cacheLevel = (eax >> 5) & 7;
//const int sharedBy = 1 + ((eax >> 14) & 0xfff);
const int linesize = 1 + (ebx & 0xfff); ebx >>= 12;
const int partitions = 1 + (ebx & 0x3ff); ebx >>= 10;
const int ways = 1 + (ebx & 0x3ff);
const int sets = 1 + ecx;
const int size = ways * partitions * linesize * sets;
switch (eax & 0x1f) {
case 1: // data cache
switch (cacheLevel) {
case 1:
s_L1Data = size;
s_L1DataLineSize = linesize;
s_L1Associativity = ways;
break;
case 2:
s_L2Data = size;
s_L2DataLineSize = linesize;
s_L2Associativity = ways;
break;
case 3:
s_L3Data = size;
s_L3DataLineSize = linesize;
s_L3Associativity = ways;
break;
}
break;
case 2: // instruction cache
switch (cacheLevel) {
case 1:
s_L1Instruction = size;
s_L1InstructionLineSize = linesize;
break;
}
break;
case 3: // unified cache
switch (cacheLevel) {
case 1:
s_L1Data = size;// / sharedBy;
s_L1DataLineSize = linesize;
s_L1Associativity = ways;
break;
case 2:
s_L2Data = size;// / sharedBy;
s_L2DataLineSize = linesize;
s_L2Associativity = ways;
break;
case 3:
s_L3Data = size;// / sharedBy;
s_L3DataLineSize = linesize;
s_L3Associativity = ways;
break;
}
break;
case 0: // no more caches
break;
default: // reserved
break;
}
}
}
}
void CpuId::interpret(uchar byte, bool *checkLeaf4)
{
switch (byte) {
case 0x06:
s_L1Instruction = 8 * 1024;
s_L1InstructionLineSize = 32;
s_L1Associativity = 4;
break;
case 0x08:
s_L1Instruction = 16 * 1024;
s_L1InstructionLineSize = 32;
s_L1Associativity = 4;
break;
case 0x09:
s_L1Instruction = 32 * 1024;
s_L1InstructionLineSize = 64;
s_L1Associativity = 4;
break;
case 0x0A:
s_L1Data = 8 * 1024;
s_L1DataLineSize = 32;
s_L1Associativity = 2;
break;
case 0x0C:
s_L1Data = 16 * 1024;
s_L1DataLineSize = 32;
s_L1Associativity = 4;
break;
case 0x0D:
s_L1Data = 16 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 4;
break;
case 0x0E:
s_L1Data = 24 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 6;
break;
case 0x21:
s_L2Data = 256 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x22:
s_L3Data = 512 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 4;
break;
case 0x23:
s_L3Data = 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0x25:
s_L3Data = 2 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0x29:
s_L3Data = 4 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0x2C:
s_L1Data = 32 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 8;
break;
case 0x30:
s_L1Data = 32 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 8;
break;
case 0x40:
s_noL2orL3 = true;
break;
case 0x41:
s_L2Data = 128 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 4;
break;
case 0x42:
s_L2Data = 256 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 4;
break;
case 0x43:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 4;
break;
case 0x44:
s_L2Data = 1024 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 4;
break;
case 0x45:
s_L2Data = 2 * 1024 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 4;
break;
case 0x46:
s_L3Data = 4 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 4;
break;
case 0x47:
s_L3Data = 8 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0x48:
s_L2Data = 3 * 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 12;
break;
case 0x49:
if (s_processorFamily == 0xf && s_processorModel == 0x6) {
s_L3Data = 4 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
} else {
s_L2Data = 4 * 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 16;
}
break;
case 0x4A:
s_L3Data = 6 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 12;
break;
case 0x4B:
s_L3Data = 8 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
break;
case 0x4C:
s_L3Data = 12 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 12;
break;
case 0x4D:
s_L3Data = 16 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
break;
case 0x4E:
s_L2Data = 6 * 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 24;
break;
case 0x60:
s_L1Data = 16 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 8;
break;
case 0x66:
s_L1Data = 8 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 4;
break;
case 0x67:
s_L1Data = 16 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 4;
break;
case 0x68:
s_L1Data = 32 * 1024;
s_L1DataLineSize = 64;
s_L1Associativity = 4;
break;
case 0x78:
s_L2Data = 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 4;
break;
case 0x79:
s_L2Data = 128 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x7A:
s_L2Data = 256 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x7B:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x7C:
s_L2Data = 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x7D:
s_L2Data = 2 * 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x7F:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 2;
break;
case 0x80:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0x82:
s_L2Data = 256 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 8;
break;
case 0x83:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 8;
break;
case 0x84:
s_L2Data = 1024 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 8;
break;
case 0x85:
s_L2Data = 2 * 1024 * 1024;
s_L2DataLineSize = 32;
s_L2Associativity = 8;
break;
case 0x86:
s_L2Data = 512 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 4;
break;
case 0x87:
s_L2Data = 1024 * 1024;
s_L2DataLineSize = 64;
s_L2Associativity = 8;
break;
case 0xD0:
s_L3Data = 512 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 4;
break;
case 0xD1:
s_L3Data = 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 4;
break;
case 0xD2:
s_L3Data = 2 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 4;
break;
case 0xD6:
s_L3Data = 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0xD7:
s_L3Data = 2 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0xD8:
s_L3Data = 4 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 8;
break;
case 0xDC:
s_L3Data = 3 * 512 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 12;
break;
case 0xDD:
s_L3Data = 3 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 12;
break;
case 0xDE:
s_L3Data = 6 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 12;
break;
case 0xE2:
s_L3Data = 2 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
break;
case 0xE3:
s_L3Data = 4 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
break;
case 0xE4:
s_L3Data = 8 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 16;
break;
case 0xEA:
s_L3Data = 12 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 24;
break;
case 0xEB:
s_L3Data = 18 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 24;
break;
case 0xEC:
s_L3Data = 24 * 1024 * 1024;
s_L3DataLineSize = 64;
s_L3Associativity = 24;
break;
case 0xF0:
s_prefetch = 64;
break;
case 0xF1:
s_prefetch = 128;
break;
case 0xFF:
// we have to use Vc_CPUID(4) to find out
*checkLeaf4 = true;
break;
default:
break;
}
}
}
// vim: sw=4 sts=4 et tw=100