Hardware/CPU/IntelCPU.cs
author moel.mich
Sat, 22 Jan 2011 17:58:32 +0000
changeset 250 c19d56a0bcad
parent 249 3b5be5dce071
child 264 718555482989
permissions -rw-r--r--
Fixed the Intel Sandy Bridge CPU core clock calculation.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@219
    39
using System.Globalization;
moel@219
    40
using System.Text;
moel@1
    41
moel@1
    42
namespace OpenHardwareMonitor.Hardware.CPU {
moel@191
    43
  internal sealed class IntelCPU : GenericCPU {
moel@46
    44
moel@219
    45
    private enum Microarchitecture {
moel@219
    46
      Unknown,
moel@219
    47
      Core,
moel@219
    48
      Atom,
moel@249
    49
      Nehalem,
moel@249
    50
      SandyBridge
moel@219
    51
    }
moel@219
    52
moel@195
    53
    private readonly Sensor[] coreTemperatures;
moel@195
    54
    private readonly Sensor[] coreClocks;
moel@195
    55
    private readonly Sensor busClock;
moel@63
    56
moel@219
    57
    private readonly Microarchitecture microarchitecture;
moel@219
    58
    private readonly double timeStampCounterMultiplier;
moel@79
    59
moel@1
    60
    private const uint IA32_THERM_STATUS_MSR = 0x019C;
moel@4
    61
    private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
moel@44
    62
    private const uint IA32_PERF_STATUS = 0x0198;
moel@46
    63
    private const uint MSR_PLATFORM_INFO = 0xCE;
moel@1
    64
moel@69
    65
    private float[] Floats(float f) {
moel@69
    66
      float[] result = new float[coreCount];
moel@69
    67
      for (int i = 0; i < coreCount; i++)
moel@69
    68
        result[i] = f;
moel@69
    69
      return result;
moel@69
    70
    }
moel@69
    71
moel@249
    72
    private float[] GetTjMaxFromMSR() {
moel@249
    73
      uint eax, edx;
moel@249
    74
      float[] result = new float[coreCount];
moel@249
    75
      for (int i = 0; i < coreCount; i++) {
moel@249
    76
        if (Ring0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
moel@249
    77
          out edx, 1UL << cpuid[i][0].Thread)) {
moel@249
    78
          result[i] = (eax >> 16) & 0xFF;
moel@249
    79
        } else {
moel@249
    80
          result[i] = 100;
moel@249
    81
        }
moel@249
    82
      }
moel@249
    83
      return result;
moel@249
    84
    }
moel@249
    85
moel@191
    86
    public IntelCPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
moel@191
    87
      : base(processorIndex, cpuid, settings) 
moel@191
    88
    {
moel@219
    89
      // set tjMax
moel@69
    90
      float[] tjMax;
moel@49
    91
      switch (family) {
moel@49
    92
        case 0x06: {
moel@49
    93
            switch (model) {
moel@219
    94
              case 0x0F: // Intel Core 2 (65nm)
moel@219
    95
                microarchitecture = Microarchitecture.Core;
moel@49
    96
                switch (stepping) {
moel@49
    97
                  case 0x06: // B2
moel@49
    98
                    switch (coreCount) {
moel@49
    99
                      case 2:
moel@69
   100
                        tjMax = Floats(80 + 10); break;
moel@49
   101
                      case 4:
moel@69
   102
                        tjMax = Floats(90 + 10); break;
moel@49
   103
                      default:
moel@69
   104
                        tjMax = Floats(85 + 10); break;
moel@49
   105
                    }
moel@69
   106
                    tjMax = Floats(80 + 10); break;
moel@49
   107
                  case 0x0B: // G0
moel@69
   108
                    tjMax = Floats(90 + 10); break;
moel@49
   109
                  case 0x0D: // M0
moel@69
   110
                    tjMax = Floats(85 + 10); break;
moel@49
   111
                  default:
moel@69
   112
                    tjMax = Floats(85 + 10); break;
moel@49
   113
                } break;
moel@219
   114
              case 0x17: // Intel Core 2 (45nm)
moel@219
   115
                microarchitecture = Microarchitecture.Core;
moel@69
   116
                tjMax = Floats(100); break;
moel@114
   117
              case 0x1C: // Intel Atom (45nm)
moel@219
   118
                microarchitecture = Microarchitecture.Atom;
moel@114
   119
                switch (stepping) {
moel@114
   120
                  case 0x02: // C0
moel@114
   121
                    tjMax = Floats(90); break;
moel@114
   122
                  case 0x0A: // A0, B0
moel@114
   123
                    tjMax = Floats(100); break;
moel@114
   124
                  default:
moel@114
   125
                    tjMax = Floats(90); break;
moel@191
   126
                } break;
moel@49
   127
              case 0x1A: // Intel Core i7 LGA1366 (45nm)
moel@49
   128
              case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
moel@249
   129
              case 0x1F: // Intel Core i5, i7 
moel@49
   130
              case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
moel@91
   131
              case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
moel@249
   132
              case 0x2E: // Intel Xeon Processor 7500 series
moel@219
   133
                microarchitecture = Microarchitecture.Nehalem;
moel@249
   134
                tjMax = GetTjMaxFromMSR();
moel@249
   135
                break;
moel@249
   136
              case 0x2A: // Intel Core i5, i7 2xxx LGA1155 (32nm)
moel@249
   137
              case 0x2D: // Next Generation Intel Xeon Processor
moel@249
   138
                microarchitecture = Microarchitecture.SandyBridge;
moel@249
   139
                tjMax = GetTjMaxFromMSR();
moel@49
   140
                break;
moel@49
   141
              default:
moel@219
   142
                microarchitecture = Microarchitecture.Unknown;
moel@219
   143
                tjMax = Floats(100); 
moel@219
   144
                break;
moel@49
   145
            }
moel@49
   146
          } break;
moel@219
   147
        default:
moel@219
   148
          microarchitecture = Microarchitecture.Unknown;
moel@219
   149
          tjMax = Floats(100); 
moel@219
   150
          break;
moel@219
   151
      }
moel@219
   152
moel@219
   153
      // set timeStampCounterMultiplier
moel@219
   154
      switch (microarchitecture) {
moel@219
   155
        case Microarchitecture.Atom:
moel@219
   156
        case Microarchitecture.Core: {
moel@219
   157
            uint eax, edx;
moel@236
   158
            if (Ring0.Rdmsr(IA32_PERF_STATUS, out eax, out edx)) {
moel@219
   159
              timeStampCounterMultiplier = 
moel@219
   160
                ((edx >> 8) & 0x1f) + 0.5 * ((edx >> 14) & 1);
moel@219
   161
            }
moel@219
   162
          } break;
moel@249
   163
        case Microarchitecture.Nehalem: 
moel@249
   164
        case Microarchitecture.SandyBridge: {
moel@219
   165
            uint eax, edx;
moel@236
   166
            if (Ring0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
moel@219
   167
              timeStampCounterMultiplier = (eax >> 8) & 0xff;
moel@219
   168
            }
moel@219
   169
          } break;
moel@219
   170
        default:
moel@219
   171
          timeStampCounterMultiplier = 1;
moel@219
   172
          break;
moel@49
   173
      }
moel@1
   174
moel@44
   175
      // check if processor supports a digital thermal sensor
moel@191
   176
      if (cpuid[0][0].Data.GetLength(0) > 6 &&
moel@191
   177
        (cpuid[0][0].Data[6, 0] & 1) != 0) {
moel@44
   178
        coreTemperatures = new Sensor[coreCount];
moel@44
   179
        for (int i = 0; i < coreTemperatures.Length; i++) {
moel@134
   180
          coreTemperatures[i] = new Sensor(CoreString(i), i,
moel@195
   181
            SensorType.Temperature, this, new [] { 
moel@63
   182
              new ParameterDescription(
moel@122
   183
                "TjMax [°C]", "TjMax temperature of the core.\n" + 
moel@69
   184
                "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
moel@122
   185
              new ParameterDescription("TSlope [°C]", 
moel@122
   186
                "Temperature slope of the digital thermal sensor.\n" + 
moel@165
   187
                "Temperature = TjMax - TSlope * Value.", 1)}, settings);
moel@155
   188
          ActivateSensor(coreTemperatures[i]);
moel@44
   189
        }
moel@44
   190
      } else {
moel@44
   191
        coreTemperatures = new Sensor[0];
moel@1
   192
      }
moel@49
   193
moel@191
   194
      busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
moel@44
   195
      coreClocks = new Sensor[coreCount];
moel@44
   196
      for (int i = 0; i < coreClocks.Length; i++) {
moel@49
   197
        coreClocks[i] =
moel@165
   198
          new Sensor(CoreString(i), i + 1, SensorType.Clock, this, settings);
moel@201
   199
        if (HasTimeStampCounter)
moel@79
   200
          ActivateSensor(coreClocks[i]);
moel@44
   201
      }
moel@191
   202
moel@191
   203
      Update();
moel@1
   204
    }
moel@1
   205
moel@191
   206
    protected override uint[] GetMSRs() {
moel@195
   207
      return new [] {
moel@191
   208
        MSR_PLATFORM_INFO,
moel@191
   209
        IA32_PERF_STATUS ,
moel@191
   210
        IA32_THERM_STATUS_MSR,
moel@191
   211
        IA32_TEMPERATURE_TARGET
moel@191
   212
      };
moel@1
   213
    }
moel@1
   214
moel@219
   215
    public override string GetReport() {
moel@219
   216
      StringBuilder r = new StringBuilder();
moel@219
   217
      r.Append(base.GetReport());
moel@219
   218
moel@219
   219
      r.Append("Time Stamp Counter Multiplier: ");
moel@219
   220
      r.AppendLine(timeStampCounterMultiplier.ToString(
moel@219
   221
        CultureInfo.InvariantCulture));
moel@219
   222
      r.AppendLine();
moel@219
   223
moel@219
   224
      return r.ToString();
moel@219
   225
    }
moel@219
   226
moel@191
   227
    public override void Update() {
moel@191
   228
      base.Update();
moel@1
   229
moel@1
   230
      for (int i = 0; i < coreTemperatures.Length; i++) {
moel@46
   231
        uint eax, edx;
moel@236
   232
        if (Ring0.RdmsrTx(
moel@191
   233
          IA32_THERM_STATUS_MSR, out eax, out edx,
moel@238
   234
            1UL << cpuid[i][0].Thread)) {
moel@1
   235
          // if reading is valid
moel@1
   236
          if ((eax & 0x80000000) != 0) {
moel@1
   237
            // get the dist from tjMax from bits 22:16
moel@63
   238
            float deltaT = ((eax & 0x007F0000) >> 16);
moel@63
   239
            float tjMax = coreTemperatures[i].Parameters[0].Value;
moel@63
   240
            float tSlope = coreTemperatures[i].Parameters[1].Value;
moel@63
   241
            coreTemperatures[i].Value = tjMax - tSlope * deltaT;
moel@24
   242
          } else {
moel@155
   243
            coreTemperatures[i].Value = null;
moel@1
   244
          }
moel@79
   245
        }
moel@24
   246
      }
moel@24
   247
moel@201
   248
      if (HasTimeStampCounter) {
moel@191
   249
        double newBusClock = 0;
moel@191
   250
        uint eax, edx;
moel@191
   251
        for (int i = 0; i < coreClocks.Length; i++) {
moel@191
   252
          System.Threading.Thread.Sleep(1);
moel@236
   253
          if (Ring0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
moel@238
   254
            1UL << cpuid[i][0].Thread)) 
moel@219
   255
          {
moel@219
   256
            newBusClock = 
moel@219
   257
              TimeStampCounterFrequency / timeStampCounterMultiplier;
moel@250
   258
            switch (microarchitecture) {
moel@250
   259
              case Microarchitecture.Nehalem: {
moel@250
   260
                  uint multiplier = eax & 0xff;
moel@250
   261
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   262
                } break;
moel@250
   263
              case Microarchitecture.SandyBridge: {
moel@250
   264
                  uint multiplier = (eax >> 8) & 0xff;
moel@250
   265
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   266
                } break;
moel@250
   267
              default: {
moel@250
   268
                  double multiplier = 
moel@250
   269
                    ((eax >> 8) & 0x1f) + 0.5 * ((eax >> 14) & 1);
moel@250
   270
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   271
                } break;
moel@250
   272
            }         
moel@219
   273
          } else { 
moel@201
   274
            // if IA32_PERF_STATUS is not available, assume TSC frequency
moel@201
   275
            coreClocks[i].Value = (float)TimeStampCounterFrequency;
moel@46
   276
          }
moel@44
   277
        }
moel@191
   278
        if (newBusClock > 0) {
moel@191
   279
          this.busClock.Value = (float)newBusClock;
moel@191
   280
          ActivateSensor(this.busClock);
moel@191
   281
        }
moel@44
   282
      }
moel@46
   283
    }
moel@191
   284
  }
moel@1
   285
}