Hardware/CPU/AMD10CPU.cs
author moel.mich
Wed, 22 Sep 2010 20:44:34 +0000
changeset 197 fb66f749b7ff
parent 196 5e9a8595296c
child 201 958e9fe8afdf
permissions -rw-r--r--
A first (experimental) implementation for the AMD 10h family CPU clock speeds.
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@196
    38
using System;
moel@196
    39
using System.Globalization;
moel@196
    40
using System.Text;
moel@197
    41
using System.Threading;
moel@196
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.CPU {
moel@165
    44
moel@196
    45
  internal sealed class AMD10CPU : AMDCPU {
moel@1
    46
moel@195
    47
    private readonly Sensor coreTemperature;
moel@197
    48
    private readonly Sensor[] coreClocks;
moel@197
    49
    private readonly Sensor busClock;
moel@197
    50
moel@197
    51
    private const uint COFVID_STATUS = 0xC0010071;
moel@197
    52
    private const uint P_STATE_0 = 0xC0010064;
moel@1
    53
moel@196
    54
    private const byte MISCELLANEOUS_CONTROL_FUNCTION = 3;
moel@196
    55
    private const ushort MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1203;
moel@197
    56
    private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4;
moel@196
    57
    
moel@196
    58
    private readonly uint miscellaneousControlAddress;
moel@1
    59
moel@191
    60
    public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
moel@191
    61
      : base(processorIndex, cpuid, settings) 
moel@196
    62
    {            
moel@1
    63
      // AMD family 10h processors support only one temperature sensor
moel@22
    64
      coreTemperature = new Sensor(
moel@134
    65
        "Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0,
moel@195
    66
        SensorType.Temperature, this, new [] {
moel@122
    67
            new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
moel@165
    68
          }, settings);
moel@1
    69
moel@196
    70
      // get the pci address for the Miscellaneous Control registers 
moel@196
    71
      miscellaneousControlAddress = GetPciAddress(
moel@196
    72
        MISCELLANEOUS_CONTROL_FUNCTION, MISCELLANEOUS_CONTROL_DEVICE_ID);
moel@42
    73
moel@197
    74
      busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
moel@197
    75
      coreClocks = new Sensor[coreCount];
moel@197
    76
      for (int i = 0; i < coreClocks.Length; i++) {
moel@197
    77
        coreClocks[i] = new Sensor(CoreString(i), i + 1, SensorType.Clock,
moel@197
    78
          this, settings);
moel@197
    79
        if (hasTSC)
moel@197
    80
          ActivateSensor(coreClocks[i]);
moel@197
    81
      }
moel@197
    82
moel@1
    83
      Update();                   
moel@1
    84
    }
moel@1
    85
moel@191
    86
    protected override uint[] GetMSRs() {
moel@197
    87
      return new uint[] { P_STATE_0, COFVID_STATUS };
moel@1
    88
    }
moel@1
    89
moel@196
    90
    public override string GetReport() {
moel@196
    91
      StringBuilder r = new StringBuilder();
moel@196
    92
      r.Append(base.GetReport());
moel@196
    93
moel@197
    94
      r.Append("Miscellaneous Control Address: 0x");
moel@196
    95
      r.AppendLine((miscellaneousControlAddress).ToString("X",
moel@196
    96
        CultureInfo.InvariantCulture));
moel@196
    97
      r.AppendLine();
moel@196
    98
moel@196
    99
      return r.ToString();
moel@196
   100
    }
moel@196
   101
moel@197
   102
    private double MultiplierFromIDs(uint divisorID, uint frequencyID) {
moel@197
   103
      return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID);
moel@197
   104
    }
moel@197
   105
moel@110
   106
    public override void Update() {
moel@191
   107
      base.Update();
moel@191
   108
moel@196
   109
      if (miscellaneousControlAddress != WinRing0.InvalidPciAddress) {
moel@42
   110
        uint value;
moel@196
   111
        if (WinRing0.ReadPciConfigDwordEx(miscellaneousControlAddress,
moel@42
   112
          REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
moel@63
   113
          coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f +
moel@63
   114
            coreTemperature.Parameters[0].Value;
moel@42
   115
          ActivateSensor(coreTemperature);
moel@42
   116
        } else {
moel@42
   117
          DeactivateSensor(coreTemperature);
moel@42
   118
        }
moel@24
   119
      }
moel@197
   120
moel@197
   121
      if (hasTSC) {
moel@197
   122
        double newBusClock = 0;
moel@197
   123
moel@197
   124
        for (int i = 0; i < coreClocks.Length; i++) {
moel@197
   125
          Thread.Sleep(1);
moel@197
   126
moel@197
   127
          uint curEax, curEdx;
moel@197
   128
          uint maxEax, maxEdx;
moel@197
   129
          if (WinRing0.RdmsrTx(COFVID_STATUS, out curEax, out curEdx,
moel@197
   130
            (UIntPtr)(1L << cpuid[i][0].Thread)) &&
moel@197
   131
            WinRing0.RdmsrTx(P_STATE_0, out maxEax, out maxEdx,
moel@197
   132
            (UIntPtr)(1L << cpuid[i][0].Thread))) 
moel@197
   133
          {
moel@197
   134
            // 8:6 CpuDid: current core divisor ID
moel@197
   135
            // 5:0 CpuFid: current core frequency ID
moel@197
   136
            uint curCpuDid = (curEax >> 6) & 7;
moel@197
   137
            uint curCpuFid = curEax & 0x1F;
moel@197
   138
            double multiplier = MultiplierFromIDs(curCpuDid, curCpuFid);
moel@197
   139
moel@197
   140
            // we assume that the max multiplier (used for the TSC) 
moel@197
   141
            // can be found in the P_STATE_0 MSR
moel@197
   142
            uint maxCpuDid = (maxEax >> 6) & 7;
moel@197
   143
            uint maxCpuFid = maxEax & 0x1F;
moel@197
   144
            double maxMultiplier = MultiplierFromIDs(maxCpuDid, maxCpuFid);
moel@197
   145
moel@197
   146
            coreClocks[i].Value = (float)(multiplier * MaxClock / maxMultiplier);
moel@197
   147
            newBusClock = (float)(MaxClock / maxMultiplier);
moel@197
   148
          } else {
moel@197
   149
            coreClocks[i].Value = (float)MaxClock;
moel@197
   150
          }
moel@197
   151
        }
moel@197
   152
moel@197
   153
        if (newBusClock > 0) {
moel@197
   154
          this.busClock.Value = (float)newBusClock;
moel@197
   155
          ActivateSensor(this.busClock);
moel@197
   156
        }
moel@197
   157
      }
moel@197
   158
    } 
moel@1
   159
  }
moel@1
   160
}