moel@1: /* moel@1: moel@1: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@1: moel@1: The contents of this file are subject to the Mozilla Public License Version moel@1: 1.1 (the "License"); you may not use this file except in compliance with moel@1: the License. You may obtain a copy of the License at moel@1: moel@1: http://www.mozilla.org/MPL/ moel@1: moel@1: Software distributed under the License is distributed on an "AS IS" basis, moel@1: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@1: for the specific language governing rights and limitations under the License. moel@1: moel@1: The Original Code is the Open Hardware Monitor code. moel@1: moel@1: The Initial Developer of the Original Code is moel@1: Michael Möller . moel@1: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@1: the Initial Developer. All Rights Reserved. moel@1: moel@1: Contributor(s): moel@1: moel@1: Alternatively, the contents of this file may be used under the terms of moel@1: either the GNU General Public License Version 2 or later (the "GPL"), or moel@1: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@1: in which case the provisions of the GPL or the LGPL are applicable instead moel@1: of those above. If you wish to allow use of your version of this file only moel@1: under the terms of either the GPL or the LGPL, and not to allow others to moel@1: use your version of this file under the terms of the MPL, indicate your moel@1: decision by deleting the provisions above and replace them with the notice moel@1: and other provisions required by the GPL or the LGPL. If you do not delete moel@1: the provisions above, a recipient may use your version of this file under moel@1: the terms of any one of the MPL, the GPL or the LGPL. moel@1: moel@1: */ moel@1: moel@196: using System; moel@201: using System.Collections.Generic; moel@201: using System.Diagnostics; moel@196: using System.Globalization; moel@201: using System.Runtime.InteropServices; moel@196: using System.Text; moel@197: using System.Threading; moel@196: moel@1: namespace OpenHardwareMonitor.Hardware.CPU { moel@165: moel@196: internal sealed class AMD10CPU : AMDCPU { moel@1: moel@195: private readonly Sensor coreTemperature; moel@197: private readonly Sensor[] coreClocks; moel@197: private readonly Sensor busClock; moel@201: moel@201: private const uint PERF_CTL_0 = 0xC0010000; moel@201: private const uint PERF_CTR_0 = 0xC0010004; moel@201: private const uint P_STATE_0 = 0xC0010064; moel@197: private const uint COFVID_STATUS = 0xC0010071; moel@1: moel@196: private const byte MISCELLANEOUS_CONTROL_FUNCTION = 3; moel@251: private const ushort FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1203; moel@251: private const ushort FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1303; moel@197: private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4; moel@201: moel@196: private readonly uint miscellaneousControlAddress; moel@251: private readonly ushort miscellaneousControlDeviceId; moel@1: moel@201: private double timeStampCounterMultiplier; moel@201: moel@191: public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings) moel@191: : base(processorIndex, cpuid, settings) moel@196: { moel@251: // AMD family 10h/11h processors support only one temperature sensor moel@22: coreTemperature = new Sensor( moel@134: "Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0, moel@195: SensorType.Temperature, this, new [] { moel@122: new ParameterDescription("Offset [°C]", "Temperature offset.", 0) moel@165: }, settings); moel@1: moel@251: switch (family) { moel@251: case 0x10: miscellaneousControlDeviceId = moel@251: FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID; break; moel@251: case 0x11: miscellaneousControlDeviceId = moel@251: FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID; break; moel@251: default: miscellaneousControlDeviceId = 0; break; moel@251: } moel@251: moel@196: // get the pci address for the Miscellaneous Control registers moel@196: miscellaneousControlAddress = GetPciAddress( moel@251: MISCELLANEOUS_CONTROL_FUNCTION, miscellaneousControlDeviceId); moel@42: moel@197: busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings); moel@197: coreClocks = new Sensor[coreCount]; moel@197: for (int i = 0; i < coreClocks.Length; i++) { moel@197: coreClocks[i] = new Sensor(CoreString(i), i + 1, SensorType.Clock, moel@197: this, settings); moel@201: if (HasTimeStampCounter) moel@197: ActivateSensor(coreClocks[i]); moel@197: } moel@197: moel@238: // set affinity to the first thread for all frequency estimations moel@238: ulong mask = ThreadAffinity.Set(1UL << cpuid[0][0].Thread); moel@201: moel@201: uint ctlEax, ctlEdx; moel@236: Ring0.Rdmsr(PERF_CTL_0, out ctlEax, out ctlEdx); moel@201: uint ctrEax, ctrEdx; moel@236: Ring0.Rdmsr(PERF_CTR_0, out ctrEax, out ctrEdx); moel@201: moel@201: timeStampCounterMultiplier = estimateTimeStampCounterMultiplier(); moel@201: moel@201: // restore the performance counter registers moel@236: Ring0.Wrmsr(PERF_CTL_0, ctlEax, ctlEdx); moel@236: Ring0.Wrmsr(PERF_CTR_0, ctrEax, ctrEdx); moel@201: moel@201: // restore the thread affinity. moel@238: ThreadAffinity.Set(mask); moel@201: moel@1: Update(); moel@1: } moel@1: moel@201: private double estimateTimeStampCounterMultiplier() { moel@201: // preload the function moel@201: estimateTimeStampCounterMultiplier(0); moel@201: estimateTimeStampCounterMultiplier(0); moel@201: moel@201: // estimate the multiplier moel@201: List estimate = new List(3); moel@201: for (int i = 0; i < 3; i++) moel@201: estimate.Add(estimateTimeStampCounterMultiplier(0.025)); moel@201: estimate.Sort(); moel@201: return estimate[1]; moel@201: } moel@201: moel@201: private double estimateTimeStampCounterMultiplier(double timeWindow) { moel@201: uint eax, edx; moel@201: moel@201: // select event "076h CPU Clocks not Halted" and enable the counter moel@236: Ring0.Wrmsr(PERF_CTL_0, moel@201: (1 << 22) | // enable performance counter moel@201: (1 << 17) | // count events in user mode moel@201: (1 << 16) | // count events in operating-system mode moel@201: 0x76, 0x00000000); moel@201: moel@201: // set the counter to 0 moel@236: Ring0.Wrmsr(PERF_CTR_0, 0, 0); moel@201: moel@201: long ticks = (long)(timeWindow * Stopwatch.Frequency); moel@201: uint lsbBegin, msbBegin, lsbEnd, msbEnd; moel@201: moel@201: long timeBegin = Stopwatch.GetTimestamp() + moel@201: (long)Math.Ceiling(0.001 * ticks); moel@201: long timeEnd = timeBegin + ticks; moel@201: while (Stopwatch.GetTimestamp() < timeBegin) { } moel@236: Ring0.Rdmsr(PERF_CTR_0, out lsbBegin, out msbBegin); moel@201: while (Stopwatch.GetTimestamp() < timeEnd) { } moel@236: Ring0.Rdmsr(PERF_CTR_0, out lsbEnd, out msbEnd); moel@201: moel@236: Ring0.Rdmsr(COFVID_STATUS, out eax, out edx); moel@201: uint cpuDid = (eax >> 6) & 7; moel@201: uint cpuFid = eax & 0x1F; moel@201: double coreMultiplier = MultiplierFromIDs(cpuDid, cpuFid); moel@201: moel@201: ulong countBegin = ((ulong)msbBegin << 32) | lsbBegin; moel@201: ulong countEnd = ((ulong)msbEnd << 32) | lsbEnd; moel@201: moel@201: double coreFrequency = 1e-6 * moel@201: (((double)(countEnd - countBegin)) * Stopwatch.Frequency) / moel@201: (timeEnd - timeBegin); moel@201: moel@201: double busFrequency = coreFrequency / coreMultiplier; moel@201: return 0.5 * Math.Round(2 * TimeStampCounterFrequency / busFrequency); moel@201: } moel@201: moel@191: protected override uint[] GetMSRs() { moel@201: return new uint[] { PERF_CTL_0, PERF_CTR_0, P_STATE_0, COFVID_STATUS }; moel@1: } moel@1: moel@196: public override string GetReport() { moel@196: StringBuilder r = new StringBuilder(); moel@196: r.Append(base.GetReport()); moel@196: moel@197: r.Append("Miscellaneous Control Address: 0x"); moel@196: r.AppendLine((miscellaneousControlAddress).ToString("X", moel@196: CultureInfo.InvariantCulture)); moel@201: r.Append("Time Stamp Counter Multiplier: "); moel@201: r.AppendLine(timeStampCounterMultiplier.ToString( moel@201: CultureInfo.InvariantCulture)); moel@201: r.AppendLine(); moel@201: moel@196: return r.ToString(); moel@196: } moel@196: moel@201: private static double MultiplierFromIDs(uint divisorID, uint frequencyID) { moel@197: return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID); moel@197: } moel@197: moel@110: public override void Update() { moel@191: base.Update(); moel@191: moel@236: if (miscellaneousControlAddress != Ring0.InvalidPciAddress) { moel@42: uint value; moel@236: if (Ring0.ReadPciConfig(miscellaneousControlAddress, moel@42: REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) { moel@63: coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f + moel@63: coreTemperature.Parameters[0].Value; moel@42: ActivateSensor(coreTemperature); moel@42: } else { moel@42: DeactivateSensor(coreTemperature); moel@42: } moel@24: } moel@197: moel@201: if (HasTimeStampCounter) { moel@197: double newBusClock = 0; moel@197: moel@197: for (int i = 0; i < coreClocks.Length; i++) { moel@197: Thread.Sleep(1); moel@197: moel@197: uint curEax, curEdx; moel@236: if (Ring0.RdmsrTx(COFVID_STATUS, out curEax, out curEdx, moel@238: 1UL << cpuid[i][0].Thread)) moel@197: { moel@197: // 8:6 CpuDid: current core divisor ID moel@197: // 5:0 CpuFid: current core frequency ID moel@201: uint cpuDid = (curEax >> 6) & 7; moel@201: uint cpuFid = curEax & 0x1F; moel@201: double multiplier = MultiplierFromIDs(cpuDid, cpuFid); moel@197: moel@201: coreClocks[i].Value = moel@201: (float)(multiplier * TimeStampCounterFrequency / moel@201: timeStampCounterMultiplier); moel@201: newBusClock = moel@201: (float)(TimeStampCounterFrequency / timeStampCounterMultiplier); moel@197: } else { moel@201: coreClocks[i].Value = (float)TimeStampCounterFrequency; moel@197: } moel@197: } moel@197: moel@197: if (newBusClock > 0) { moel@197: this.busClock.Value = (float)newBusClock; moel@197: ActivateSensor(this.busClock); moel@197: } moel@197: } moel@201: } moel@1: } moel@1: }