Hardware/CPU/IntelCPU.cs
author paulwerelds
Tue, 21 Sep 2010 10:33:28 +0000
changeset 194 fbf22ccd9d6f
parent 182 4801e9eaf979
child 195 0ee888c485d5
permissions -rw-r--r--
Fixed some silly mistakes
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 
    41 namespace OpenHardwareMonitor.Hardware.CPU {
    42   internal sealed class IntelCPU : GenericCPU {
    43 
    44     private Sensor[] coreTemperatures;
    45     private Sensor[] coreClocks;
    46     private Sensor busClock;
    47 
    48     private uint maxNehalemMultiplier = 0;
    49 
    50     private const uint IA32_THERM_STATUS_MSR = 0x019C;
    51     private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    52     private const uint IA32_PERF_STATUS = 0x0198;
    53     private const uint MSR_PLATFORM_INFO = 0xCE;
    54 
    55     private float[] Floats(float f) {
    56       float[] result = new float[coreCount];
    57       for (int i = 0; i < coreCount; i++)
    58         result[i] = f;
    59       return result;
    60     }
    61 
    62     public IntelCPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
    63       : base(processorIndex, cpuid, settings) 
    64     {
    65       float[] tjMax;
    66       switch (family) {
    67         case 0x06: {
    68             switch (model) {
    69               case 0x0F: // Intel Core (65nm)
    70                 switch (stepping) {
    71                   case 0x06: // B2
    72                     switch (coreCount) {
    73                       case 2:
    74                         tjMax = Floats(80 + 10); break;
    75                       case 4:
    76                         tjMax = Floats(90 + 10); break;
    77                       default:
    78                         tjMax = Floats(85 + 10); break;
    79                     }
    80                     tjMax = Floats(80 + 10); break;
    81                   case 0x0B: // G0
    82                     tjMax = Floats(90 + 10); break;
    83                   case 0x0D: // M0
    84                     tjMax = Floats(85 + 10); break;
    85                   default:
    86                     tjMax = Floats(85 + 10); break;
    87                 } break;
    88               case 0x17: // Intel Core (45nm)
    89                 tjMax = Floats(100); break;
    90               case 0x1C: // Intel Atom (45nm)
    91                 switch (stepping) {
    92                   case 0x02: // C0
    93                     tjMax = Floats(90); break;
    94                   case 0x0A: // A0, B0
    95                     tjMax = Floats(100); break;
    96                   default:
    97                     tjMax = Floats(90); break;
    98                 } break;
    99               case 0x1A: // Intel Core i7 LGA1366 (45nm)
   100               case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
   101               case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
   102               case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
   103                 uint eax, edx;
   104                 tjMax = new float[coreCount];
   105                 for (int i = 0; i < coreCount; i++) {
   106                   if (WinRing0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
   107                     out edx, (UIntPtr)(1L << cpuid[i][0].Thread))) {
   108                     tjMax[i] = (eax >> 16) & 0xFF;
   109                   } else {
   110                     tjMax[i] = 100;
   111                   }
   112                 }
   113                 if (WinRing0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
   114                   maxNehalemMultiplier = (eax >> 8) & 0xff;
   115                 }
   116                 break;
   117               default:
   118                 tjMax = Floats(100); break;
   119             }
   120           } break;
   121         default: tjMax = Floats(100); break;
   122       }
   123 
   124       // check if processor supports a digital thermal sensor
   125       if (cpuid[0][0].Data.GetLength(0) > 6 &&
   126         (cpuid[0][0].Data[6, 0] & 1) != 0) {
   127         coreTemperatures = new Sensor[coreCount];
   128         for (int i = 0; i < coreTemperatures.Length; i++) {
   129           coreTemperatures[i] = new Sensor(CoreString(i), i,
   130             SensorType.Temperature, this, new ParameterDescription[] { 
   131               new ParameterDescription(
   132                 "TjMax [°C]", "TjMax temperature of the core.\n" + 
   133                 "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
   134               new ParameterDescription("TSlope [°C]", 
   135                 "Temperature slope of the digital thermal sensor.\n" + 
   136                 "Temperature = TjMax - TSlope * Value.", 1)}, settings);
   137           ActivateSensor(coreTemperatures[i]);
   138         }
   139       } else {
   140         coreTemperatures = new Sensor[0];
   141       }
   142 
   143       busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
   144       coreClocks = new Sensor[coreCount];
   145       for (int i = 0; i < coreClocks.Length; i++) {
   146         coreClocks[i] =
   147           new Sensor(CoreString(i), i + 1, SensorType.Clock, this, settings);
   148         if (hasTSC)
   149           ActivateSensor(coreClocks[i]);
   150       }
   151 
   152       Update();
   153     }
   154 
   155     protected override uint[] GetMSRs() {
   156       return new uint[] {
   157         MSR_PLATFORM_INFO,
   158         IA32_PERF_STATUS ,
   159         IA32_THERM_STATUS_MSR,
   160         IA32_TEMPERATURE_TARGET
   161       };
   162     }
   163 
   164     public override void Update() {
   165       base.Update();
   166 
   167       for (int i = 0; i < coreTemperatures.Length; i++) {
   168         uint eax, edx;
   169         if (WinRing0.RdmsrTx(
   170           IA32_THERM_STATUS_MSR, out eax, out edx,
   171             (UIntPtr)(1L << cpuid[i][0].Thread))) {
   172           // if reading is valid
   173           if ((eax & 0x80000000) != 0) {
   174             // get the dist from tjMax from bits 22:16
   175             float deltaT = ((eax & 0x007F0000) >> 16);
   176             float tjMax = coreTemperatures[i].Parameters[0].Value;
   177             float tSlope = coreTemperatures[i].Parameters[1].Value;
   178             coreTemperatures[i].Value = tjMax - tSlope * deltaT;
   179           } else {
   180             coreTemperatures[i].Value = null;
   181           }
   182         }
   183       }
   184 
   185       if (hasTSC) {
   186         double newBusClock = 0;
   187         uint eax, edx;
   188         for (int i = 0; i < coreClocks.Length; i++) {
   189           System.Threading.Thread.Sleep(1);
   190           if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
   191             (UIntPtr)(1L << cpuid[i][0].Thread))) {
   192             if (maxNehalemMultiplier > 0) { // Core i3, i5, i7
   193               uint nehalemMultiplier = eax & 0xff;
   194               coreClocks[i].Value =
   195                 (float)(nehalemMultiplier * MaxClock / maxNehalemMultiplier);
   196               newBusClock = (float)(MaxClock / maxNehalemMultiplier);
   197             } else { // Core 2
   198               uint multiplier = (eax >> 8) & 0x1f;
   199               uint maxMultiplier = (edx >> 8) & 0x1f;
   200               // factor = multiplier * 2 to handle non integer multipliers 
   201               uint factor = (multiplier << 1) | ((eax >> 14) & 1);
   202               uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1);
   203               if (maxFactor > 0) {
   204                 coreClocks[i].Value = (float)(factor * MaxClock / maxFactor);
   205                 newBusClock = (float)(2 * MaxClock / maxFactor);
   206               }
   207             }
   208           } else { // Intel Pentium 4
   209             // if IA32_PERF_STATUS is not available, assume maxClock
   210             coreClocks[i].Value = (float)MaxClock;
   211           }
   212         }
   213         if (newBusClock > 0) {
   214           this.busClock.Value = (float)newBusClock;
   215           ActivateSensor(this.busClock);
   216         }
   217       }
   218     }
   219   }
   220 }