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