Hardware/CPU/IntelCPU.cs
author moel.mich
Sun, 28 Dec 2014 15:34:37 +0000
changeset 425 520bca5f2a7d
parent 424 427fc32e589f
child 426 56d59f52fa2b
permissions -rw-r--r--
Added support for Intel Core M-5xxx (14nm) Broadwell CPUs (Fixed Issue 646).
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@424
     7
  Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@219
    12
using System.Globalization;
moel@219
    13
using System.Text;
moel@1
    14
moel@1
    15
namespace OpenHardwareMonitor.Hardware.CPU {
moel@191
    16
  internal sealed class IntelCPU : GenericCPU {
moel@46
    17
moel@219
    18
    private enum Microarchitecture {
moel@219
    19
      Unknown,
moel@264
    20
      NetBurst,
moel@219
    21
      Core,
moel@219
    22
      Atom,
moel@249
    23
      Nehalem,
moel@350
    24
      SandyBridge,
moel@396
    25
      IvyBridge,
moel@425
    26
      Haswell,
moel@425
    27
      Broadwell
moel@219
    28
    }
moel@219
    29
moel@195
    30
    private readonly Sensor[] coreTemperatures;
moel@306
    31
    private readonly Sensor packageTemperature;
moel@195
    32
    private readonly Sensor[] coreClocks;
moel@195
    33
    private readonly Sensor busClock;
moel@321
    34
    private readonly Sensor[] powerSensors;
moel@63
    35
moel@219
    36
    private readonly Microarchitecture microarchitecture;
moel@219
    37
    private readonly double timeStampCounterMultiplier;
moel@79
    38
moel@1
    39
    private const uint IA32_THERM_STATUS_MSR = 0x019C;
moel@4
    40
    private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
moel@44
    41
    private const uint IA32_PERF_STATUS = 0x0198;
moel@46
    42
    private const uint MSR_PLATFORM_INFO = 0xCE;
moel@306
    43
    private const uint IA32_PACKAGE_THERM_STATUS = 0x1B1;
moel@317
    44
    private const uint MSR_RAPL_POWER_UNIT = 0x606;
moel@317
    45
    private const uint MSR_PKG_ENERY_STATUS = 0x611;
moel@321
    46
    private const uint MSR_DRAM_ENERGY_STATUS = 0x619;
moel@317
    47
    private const uint MSR_PP0_ENERY_STATUS = 0x639;
moel@320
    48
    private const uint MSR_PP1_ENERY_STATUS = 0x641;
moel@317
    49
moel@321
    50
    private readonly uint[] energyStatusMSRs = { MSR_PKG_ENERY_STATUS, 
moel@321
    51
      MSR_PP0_ENERY_STATUS, MSR_PP1_ENERY_STATUS, MSR_DRAM_ENERGY_STATUS };
moel@321
    52
    private readonly string[] powerSensorLabels = 
moel@321
    53
      { "CPU Package", "CPU Cores", "CPU Graphics", "CPU DRAM" };
moel@317
    54
    private float energyUnitMultiplier = 0;
moel@321
    55
    private DateTime[] lastEnergyTime;
moel@321
    56
    private uint[] lastEnergyConsumed;
moel@317
    57
moel@1
    58
moel@69
    59
    private float[] Floats(float f) {
moel@69
    60
      float[] result = new float[coreCount];
moel@69
    61
      for (int i = 0; i < coreCount; i++)
moel@69
    62
        result[i] = f;
moel@69
    63
      return result;
moel@69
    64
    }
moel@69
    65
moel@249
    66
    private float[] GetTjMaxFromMSR() {
moel@249
    67
      uint eax, edx;
moel@249
    68
      float[] result = new float[coreCount];
moel@249
    69
      for (int i = 0; i < coreCount; i++) {
moel@249
    70
        if (Ring0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
moel@249
    71
          out edx, 1UL << cpuid[i][0].Thread)) {
moel@249
    72
          result[i] = (eax >> 16) & 0xFF;
moel@249
    73
        } else {
moel@249
    74
          result[i] = 100;
moel@249
    75
        }
moel@249
    76
      }
moel@249
    77
      return result;
moel@249
    78
    }
moel@249
    79
moel@191
    80
    public IntelCPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
moel@321
    81
      : base(processorIndex, cpuid, settings) {
moel@219
    82
      // set tjMax
moel@69
    83
      float[] tjMax;
moel@49
    84
      switch (family) {
moel@49
    85
        case 0x06: {
moel@49
    86
            switch (model) {
moel@219
    87
              case 0x0F: // Intel Core 2 (65nm)
moel@219
    88
                microarchitecture = Microarchitecture.Core;
moel@49
    89
                switch (stepping) {
moel@49
    90
                  case 0x06: // B2
moel@49
    91
                    switch (coreCount) {
moel@49
    92
                      case 2:
moel@69
    93
                        tjMax = Floats(80 + 10); break;
moel@49
    94
                      case 4:
moel@69
    95
                        tjMax = Floats(90 + 10); break;
moel@49
    96
                      default:
moel@69
    97
                        tjMax = Floats(85 + 10); break;
moel@49
    98
                    }
moel@69
    99
                    tjMax = Floats(80 + 10); break;
moel@49
   100
                  case 0x0B: // G0
moel@69
   101
                    tjMax = Floats(90 + 10); break;
moel@49
   102
                  case 0x0D: // M0
moel@69
   103
                    tjMax = Floats(85 + 10); break;
moel@49
   104
                  default:
moel@69
   105
                    tjMax = Floats(85 + 10); break;
moel@49
   106
                } break;
moel@219
   107
              case 0x17: // Intel Core 2 (45nm)
moel@219
   108
                microarchitecture = Microarchitecture.Core;
moel@69
   109
                tjMax = Floats(100); break;
moel@114
   110
              case 0x1C: // Intel Atom (45nm)
moel@219
   111
                microarchitecture = Microarchitecture.Atom;
moel@114
   112
                switch (stepping) {
moel@114
   113
                  case 0x02: // C0
moel@114
   114
                    tjMax = Floats(90); break;
moel@114
   115
                  case 0x0A: // A0, B0
moel@114
   116
                    tjMax = Floats(100); break;
moel@114
   117
                  default:
moel@114
   118
                    tjMax = Floats(90); break;
moel@191
   119
                } break;
moel@49
   120
              case 0x1A: // Intel Core i7 LGA1366 (45nm)
moel@49
   121
              case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
moel@249
   122
              case 0x1F: // Intel Core i5, i7 
moel@49
   123
              case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
moel@91
   124
              case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
moel@350
   125
              case 0x2E: // Intel Xeon Processor 7500 series (45nm)
moel@350
   126
              case 0x2F: // Intel Xeon Processor (32nm)
moel@219
   127
                microarchitecture = Microarchitecture.Nehalem;
moel@249
   128
                tjMax = GetTjMaxFromMSR();
moel@249
   129
                break;
moel@249
   130
              case 0x2A: // Intel Core i5, i7 2xxx LGA1155 (32nm)
moel@350
   131
              case 0x2D: // Next Generation Intel Xeon, i7 3xxx LGA2011 (32nm)
moel@249
   132
                microarchitecture = Microarchitecture.SandyBridge;
moel@249
   133
                tjMax = GetTjMaxFromMSR();
moel@49
   134
                break;
moel@350
   135
              case 0x3A: // Intel Core i5, i7 3xxx LGA1155 (22nm)
moel@417
   136
              case 0x3E: // Intel Core i7 4xxx LGA2011 (22nm)
moel@350
   137
                microarchitecture = Microarchitecture.IvyBridge;
moel@350
   138
                tjMax = GetTjMaxFromMSR();
moel@350
   139
                break;
moel@425
   140
              case 0x3C: // Intel Core i5, i7 4xxx LGA1150 (22nm)              
moel@424
   141
              case 0x3F: // Intel Xeon E5-2600/1600 v3, Core i7-59xx
moel@424
   142
                         // LGA2011-v3, Haswell-E (22nm)
moel@425
   143
              case 0x45: // Intel Core i5, i7 4xxxU (22nm)
moel@424
   144
              case 0x46: 
moel@396
   145
                microarchitecture = Microarchitecture.Haswell;
moel@396
   146
                tjMax = GetTjMaxFromMSR();
moel@396
   147
                break;
moel@425
   148
              case 0x3D: // Intel Core M-5xxx (14nm)
moel@425
   149
                microarchitecture = Microarchitecture.Broadwell;
moel@425
   150
                tjMax = GetTjMaxFromMSR();
moel@425
   151
                break;
moel@49
   152
              default:
moel@219
   153
                microarchitecture = Microarchitecture.Unknown;
moel@321
   154
                tjMax = Floats(100);
moel@219
   155
                break;
moel@49
   156
            }
moel@49
   157
          } break;
moel@264
   158
        case 0x0F: {
moel@264
   159
            switch (model) {
moel@264
   160
              case 0x00: // Pentium 4 (180nm)
moel@264
   161
              case 0x01: // Pentium 4 (130nm)
moel@264
   162
              case 0x02: // Pentium 4 (130nm)
moel@264
   163
              case 0x03: // Pentium 4, Celeron D (90nm)
moel@264
   164
              case 0x04: // Pentium 4, Pentium D, Celeron D (90nm)
moel@264
   165
              case 0x06: // Pentium 4, Pentium D, Celeron D (65nm)
moel@264
   166
                microarchitecture = Microarchitecture.NetBurst;
moel@321
   167
                tjMax = Floats(100);
moel@264
   168
                break;
moel@264
   169
              default:
moel@264
   170
                microarchitecture = Microarchitecture.Unknown;
moel@264
   171
                tjMax = Floats(100);
moel@264
   172
                break;
moel@264
   173
            }
moel@264
   174
          } break;
moel@219
   175
        default:
moel@219
   176
          microarchitecture = Microarchitecture.Unknown;
moel@321
   177
          tjMax = Floats(100);
moel@219
   178
          break;
moel@219
   179
      }
moel@219
   180
moel@219
   181
      // set timeStampCounterMultiplier
moel@219
   182
      switch (microarchitecture) {
moel@264
   183
        case Microarchitecture.NetBurst:
moel@219
   184
        case Microarchitecture.Atom:
moel@219
   185
        case Microarchitecture.Core: {
moel@219
   186
            uint eax, edx;
moel@236
   187
            if (Ring0.Rdmsr(IA32_PERF_STATUS, out eax, out edx)) {
moel@321
   188
              timeStampCounterMultiplier =
moel@219
   189
                ((edx >> 8) & 0x1f) + 0.5 * ((edx >> 14) & 1);
moel@219
   190
            }
moel@219
   191
          } break;
moel@321
   192
        case Microarchitecture.Nehalem:
moel@350
   193
        case Microarchitecture.SandyBridge:
moel@396
   194
        case Microarchitecture.IvyBridge:
moel@425
   195
        case Microarchitecture.Haswell: 
moel@425
   196
        case Microarchitecture.Broadwell: {
moel@219
   197
            uint eax, edx;
moel@236
   198
            if (Ring0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
moel@219
   199
              timeStampCounterMultiplier = (eax >> 8) & 0xff;
moel@219
   200
            }
moel@219
   201
          } break;
moel@350
   202
        default: 
moel@350
   203
          timeStampCounterMultiplier = 0;
moel@350
   204
          break;
moel@49
   205
      }
moel@1
   206
moel@306
   207
      // check if processor supports a digital thermal sensor at core level
moel@191
   208
      if (cpuid[0][0].Data.GetLength(0) > 6 &&
moel@350
   209
        (cpuid[0][0].Data[6, 0] & 1) != 0 && 
moel@350
   210
        microarchitecture != Microarchitecture.Unknown) 
moel@350
   211
      {
moel@44
   212
        coreTemperatures = new Sensor[coreCount];
moel@44
   213
        for (int i = 0; i < coreTemperatures.Length; i++) {
moel@134
   214
          coreTemperatures[i] = new Sensor(CoreString(i), i,
moel@321
   215
            SensorType.Temperature, this, new[] { 
moel@63
   216
              new ParameterDescription(
moel@306
   217
                "TjMax [°C]", "TjMax temperature of the core sensor.\n" + 
moel@69
   218
                "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
moel@122
   219
              new ParameterDescription("TSlope [°C]", 
moel@122
   220
                "Temperature slope of the digital thermal sensor.\n" + 
moel@165
   221
                "Temperature = TjMax - TSlope * Value.", 1)}, settings);
moel@155
   222
          ActivateSensor(coreTemperatures[i]);
moel@44
   223
        }
moel@44
   224
      } else {
moel@44
   225
        coreTemperatures = new Sensor[0];
moel@1
   226
      }
moel@49
   227
moel@306
   228
      // check if processor supports a digital thermal sensor at package level
moel@306
   229
      if (cpuid[0][0].Data.GetLength(0) > 6 &&
moel@350
   230
        (cpuid[0][0].Data[6, 0] & 0x40) != 0 && 
moel@350
   231
        microarchitecture != Microarchitecture.Unknown) 
moel@350
   232
      {
moel@321
   233
        packageTemperature = new Sensor("CPU Package",
moel@321
   234
          coreTemperatures.Length, SensorType.Temperature, this, new[] { 
moel@306
   235
              new ParameterDescription(
moel@306
   236
                "TjMax [°C]", "TjMax temperature of the package sensor.\n" + 
moel@306
   237
                "Temperature = TjMax - TSlope * Value.", tjMax[0]), 
moel@306
   238
              new ParameterDescription("TSlope [°C]", 
moel@306
   239
                "Temperature slope of the digital thermal sensor.\n" + 
moel@306
   240
                "Temperature = TjMax - TSlope * Value.", 1)}, settings);
moel@306
   241
        ActivateSensor(packageTemperature);
moel@321
   242
      }
moel@306
   243
moel@191
   244
      busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
moel@44
   245
      coreClocks = new Sensor[coreCount];
moel@44
   246
      for (int i = 0; i < coreClocks.Length; i++) {
moel@49
   247
        coreClocks[i] =
moel@165
   248
          new Sensor(CoreString(i), i + 1, SensorType.Clock, this, settings);
moel@350
   249
        if (HasTimeStampCounter && microarchitecture != Microarchitecture.Unknown)
moel@79
   250
          ActivateSensor(coreClocks[i]);
moel@44
   251
      }
moel@191
   252
moel@350
   253
      if (microarchitecture == Microarchitecture.SandyBridge ||
moel@396
   254
          microarchitecture == Microarchitecture.IvyBridge ||
moel@425
   255
          microarchitecture == Microarchitecture.Haswell ||
moel@425
   256
          microarchitecture == Microarchitecture.Broadwell) 
moel@350
   257
      {
moel@321
   258
        powerSensors = new Sensor[energyStatusMSRs.Length];
moel@321
   259
        lastEnergyTime = new DateTime[energyStatusMSRs.Length];
moel@321
   260
        lastEnergyConsumed = new uint[energyStatusMSRs.Length];
moel@321
   261
moel@317
   262
        uint eax, edx;
moel@317
   263
        if (Ring0.Rdmsr(MSR_RAPL_POWER_UNIT, out eax, out edx))
moel@424
   264
          energyUnitMultiplier = 1.0f / (1 << (int)((eax >> 8) & 0x1F));
moel@317
   265
moel@321
   266
        if (energyUnitMultiplier != 0) {
moel@321
   267
          for (int i = 0; i < energyStatusMSRs.Length; i++) {
moel@321
   268
            if (!Ring0.Rdmsr(energyStatusMSRs[i], out eax, out edx))
moel@321
   269
              continue;
moel@317
   270
moel@321
   271
            lastEnergyTime[i] = DateTime.UtcNow;
moel@321
   272
            lastEnergyConsumed[i] = eax;
moel@321
   273
            powerSensors[i] = new Sensor(powerSensorLabels[i], i,
moel@321
   274
              SensorType.Power, this, settings);
moel@321
   275
            ActivateSensor(powerSensors[i]);
moel@321
   276
          }
moel@317
   277
        }
moel@317
   278
      }
moel@317
   279
moel@191
   280
      Update();
moel@1
   281
    }
moel@1
   282
moel@191
   283
    protected override uint[] GetMSRs() {
moel@321
   284
      return new[] {
moel@191
   285
        MSR_PLATFORM_INFO,
moel@191
   286
        IA32_PERF_STATUS ,
moel@191
   287
        IA32_THERM_STATUS_MSR,
moel@306
   288
        IA32_TEMPERATURE_TARGET,
moel@317
   289
        IA32_PACKAGE_THERM_STATUS,
moel@317
   290
        MSR_RAPL_POWER_UNIT,
moel@317
   291
        MSR_PKG_ENERY_STATUS,
moel@321
   292
        MSR_DRAM_ENERGY_STATUS,
moel@320
   293
        MSR_PP0_ENERY_STATUS,
moel@320
   294
        MSR_PP1_ENERY_STATUS
moel@191
   295
      };
moel@1
   296
    }
moel@1
   297
moel@219
   298
    public override string GetReport() {
moel@219
   299
      StringBuilder r = new StringBuilder();
moel@219
   300
      r.Append(base.GetReport());
moel@219
   301
moel@264
   302
      r.Append("Microarchitecture: ");
moel@264
   303
      r.AppendLine(microarchitecture.ToString());
moel@219
   304
      r.Append("Time Stamp Counter Multiplier: ");
moel@219
   305
      r.AppendLine(timeStampCounterMultiplier.ToString(
moel@219
   306
        CultureInfo.InvariantCulture));
moel@219
   307
      r.AppendLine();
moel@219
   308
moel@219
   309
      return r.ToString();
moel@219
   310
    }
moel@219
   311
moel@191
   312
    public override void Update() {
moel@191
   313
      base.Update();
moel@1
   314
moel@1
   315
      for (int i = 0; i < coreTemperatures.Length; i++) {
moel@46
   316
        uint eax, edx;
moel@418
   317
        // if reading is valid
moel@418
   318
        if (Ring0.RdmsrTx(IA32_THERM_STATUS_MSR, out eax, out edx,
moel@418
   319
            1UL << cpuid[i][0].Thread) && (eax & 0x80000000) != 0) 
moel@418
   320
        {
moel@418
   321
          // get the dist from tjMax from bits 22:16
moel@418
   322
          float deltaT = ((eax & 0x007F0000) >> 16);
moel@418
   323
          float tjMax = coreTemperatures[i].Parameters[0].Value;
moel@418
   324
          float tSlope = coreTemperatures[i].Parameters[1].Value;
moel@418
   325
          coreTemperatures[i].Value = tjMax - tSlope * deltaT;
moel@418
   326
        } else {
moel@418
   327
          coreTemperatures[i].Value = null;
moel@79
   328
        }
moel@24
   329
      }
moel@24
   330
moel@306
   331
      if (packageTemperature != null) {
moel@306
   332
        uint eax, edx;
moel@418
   333
        // if reading is valid
moel@418
   334
        if (Ring0.RdmsrTx(IA32_PACKAGE_THERM_STATUS, out eax, out edx,
moel@418
   335
            1UL << cpuid[0][0].Thread) && (eax & 0x80000000) != 0) 
moel@418
   336
        {
moel@306
   337
          // get the dist from tjMax from bits 22:16
moel@306
   338
          float deltaT = ((eax & 0x007F0000) >> 16);
moel@306
   339
          float tjMax = packageTemperature.Parameters[0].Value;
moel@306
   340
          float tSlope = packageTemperature.Parameters[1].Value;
moel@306
   341
          packageTemperature.Value = tjMax - tSlope * deltaT;
moel@306
   342
        } else {
moel@306
   343
          packageTemperature.Value = null;
moel@306
   344
        }
moel@306
   345
      }
moel@306
   346
moel@350
   347
      if (HasTimeStampCounter && timeStampCounterMultiplier > 0) {
moel@191
   348
        double newBusClock = 0;
moel@191
   349
        uint eax, edx;
moel@191
   350
        for (int i = 0; i < coreClocks.Length; i++) {
moel@191
   351
          System.Threading.Thread.Sleep(1);
moel@236
   352
          if (Ring0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
moel@321
   353
            1UL << cpuid[i][0].Thread)) {
moel@321
   354
            newBusClock =
moel@219
   355
              TimeStampCounterFrequency / timeStampCounterMultiplier;
moel@250
   356
            switch (microarchitecture) {
moel@250
   357
              case Microarchitecture.Nehalem: {
moel@250
   358
                  uint multiplier = eax & 0xff;
moel@250
   359
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   360
                } break;
moel@350
   361
              case Microarchitecture.SandyBridge:
moel@396
   362
              case Microarchitecture.IvyBridge:
moel@425
   363
              case Microarchitecture.Haswell: 
moel@425
   364
              case Microarchitecture.Broadwell: {
moel@250
   365
                  uint multiplier = (eax >> 8) & 0xff;
moel@250
   366
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   367
                } break;
moel@250
   368
              default: {
moel@321
   369
                  double multiplier =
moel@250
   370
                    ((eax >> 8) & 0x1f) + 0.5 * ((eax >> 14) & 1);
moel@250
   371
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   372
                } break;
moel@321
   373
            }
moel@321
   374
          } else {
moel@201
   375
            // if IA32_PERF_STATUS is not available, assume TSC frequency
moel@201
   376
            coreClocks[i].Value = (float)TimeStampCounterFrequency;
moel@46
   377
          }
moel@44
   378
        }
moel@191
   379
        if (newBusClock > 0) {
moel@191
   380
          this.busClock.Value = (float)newBusClock;
moel@191
   381
          ActivateSensor(this.busClock);
moel@191
   382
        }
moel@44
   383
      }
moel@317
   384
moel@321
   385
      if (powerSensors != null) {
moel@321
   386
        foreach (Sensor sensor in powerSensors) {
moel@321
   387
          if (sensor == null)
moel@321
   388
            continue;
moel@317
   389
moel@321
   390
          uint eax, edx;
moel@321
   391
          if (!Ring0.Rdmsr(energyStatusMSRs[sensor.Index], out eax, out edx))
moel@321
   392
            continue;
moel@317
   393
moel@317
   394
          DateTime time = DateTime.UtcNow;
moel@317
   395
          uint energyConsumed = eax;
moel@321
   396
          float deltaTime =
moel@321
   397
            (float)(time - lastEnergyTime[sensor.Index]).TotalSeconds;
moel@321
   398
          if (deltaTime < 0.01)
moel@321
   399
            continue;
moel@321
   400
moel@321
   401
          sensor.Value = energyUnitMultiplier * unchecked(
moel@321
   402
            energyConsumed - lastEnergyConsumed[sensor.Index]) / deltaTime;
moel@321
   403
          lastEnergyTime[sensor.Index] = time;
moel@321
   404
          lastEnergyConsumed[sensor.Index] = energyConsumed;
moel@317
   405
        }
moel@317
   406
      }
moel@46
   407
    }
moel@191
   408
  }
moel@1
   409
}