moel@1: /* moel@1: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@1: moel@407: Copyright (C) 2009-2013 Michael Möller moel@344: 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@268: using System.IO; 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@279: private const uint HWCR = 0xC0010015; 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@271: private const ushort FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1303; moel@301: private const ushort FAMILY_12H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1703; moel@301: private const ushort FAMILY_14H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1703; moel@407: private const ushort FAMILY_15H_MODEL_00_MISC_CONTROL_DEVICE_ID = 0x1603; moel@407: private const ushort FAMILY_15H_MODEL_10_MISC_CONTROL_DEVICE_ID = 0x1403; moel@407: private const ushort FAMILY_16H_MODEL_00_MISC_CONTROL_DEVICE_ID = 0x1533; moel@407: moel@197: private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4; moel@273: private const uint CLOCK_POWER_TIMING_CONTROL_0_REGISTER = 0xD4; moel@201: moel@196: private readonly uint miscellaneousControlAddress; moel@251: private readonly ushort miscellaneousControlDeviceId; moel@1: moel@268: private readonly FileStream temperatureStream; moel@266: moel@279: private readonly double timeStampCounterMultiplier; moel@279: private readonly bool corePerformanceBoostSupport; moel@201: moel@191: public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings) moel@191: : base(processorIndex, cpuid, settings) moel@196: { moel@301: // AMD family 1Xh 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@301: case 0x12: miscellaneousControlDeviceId = moel@301: FAMILY_12H_MISCELLANEOUS_CONTROL_DEVICE_ID; break; moel@271: case 0x14: miscellaneousControlDeviceId = moel@273: FAMILY_14H_MISCELLANEOUS_CONTROL_DEVICE_ID; break; moel@407: case 0x15: moel@407: switch (model & 0xF0) { moel@407: case 0x00: miscellaneousControlDeviceId = moel@407: FAMILY_15H_MODEL_00_MISC_CONTROL_DEVICE_ID; break; moel@407: case 0x10: miscellaneousControlDeviceId = moel@407: FAMILY_15H_MODEL_10_MISC_CONTROL_DEVICE_ID; break; moel@407: default: miscellaneousControlDeviceId = 0; break; moel@407: } break; moel@407: case 0x16: moel@407: switch (model & 0xF0) { moel@407: case 0x00: miscellaneousControlDeviceId = moel@407: FAMILY_16H_MODEL_00_MISC_CONTROL_DEVICE_ID; break; moel@407: default: miscellaneousControlDeviceId = 0; break; moel@407: } 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@279: corePerformanceBoostSupport = (cpuid[0][0].ExtData[7, 3] & (1 << 9)) > 0; moel@279: 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@279: // disable core performance boost moel@279: uint hwcrEax, hwcrEdx; moel@279: Ring0.Rdmsr(HWCR, out hwcrEax, out hwcrEdx); moel@279: if (corePerformanceBoostSupport) moel@279: Ring0.Wrmsr(HWCR, hwcrEax | (1 << 25), hwcrEdx); moel@279: 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@279: // restore core performance boost moel@279: if (corePerformanceBoostSupport) moel@279: Ring0.Wrmsr(HWCR, hwcrEax, hwcrEdx); moel@279: moel@201: // restore the thread affinity. moel@238: ThreadAffinity.Set(mask); moel@201: moel@266: // the file reader for lm-sensors support on Linux moel@268: temperatureStream = null; moel@266: int p = (int)Environment.OSVersion.Platform; moel@266: if ((p == 4) || (p == 128)) { moel@266: string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/"); moel@266: foreach (string path in devicePaths) { moel@266: string name = null; moel@266: try { moel@266: using (StreamReader reader = new StreamReader(path + "/device/name")) moel@266: name = reader.ReadLine(); moel@266: } catch (IOException) { } moel@266: switch (name) { moel@266: case "k10temp": moel@268: temperatureStream = new FileStream(path + "/device/temp1_input", moel@268: FileMode.Open, FileAccess.Read, FileShare.ReadWrite); moel@266: break; moel@266: } moel@266: } moel@266: } moel@266: 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@273: 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@279: moel@201: while (Stopwatch.GetTimestamp() < timeEnd) { } moel@236: Ring0.Rdmsr(PERF_CTR_0, out lsbEnd, out msbEnd); moel@279: Ring0.Rdmsr(COFVID_STATUS, out eax, out edx); moel@301: double coreMultiplier = GetCoreMultiplier(eax); 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@279: moel@273: return 0.25 * Math.Round(4 * TimeStampCounterFrequency / busFrequency); moel@201: } moel@201: moel@191: protected override uint[] GetMSRs() { moel@279: return new uint[] { PERF_CTL_0, PERF_CTR_0, HWCR, P_STATE_0, moel@279: 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@273: if (family == 0x14) { moel@273: uint value = 0; moel@273: Ring0.ReadPciConfig(miscellaneousControlAddress, moel@273: CLOCK_POWER_TIMING_CONTROL_0_REGISTER, out value); moel@273: r.Append("PCI Register D18F3xD4: "); moel@273: r.AppendLine(value.ToString("X8", CultureInfo.InvariantCulture)); moel@273: } moel@201: r.AppendLine(); moel@201: moel@196: return r.ToString(); moel@196: } moel@196: moel@301: private double GetCoreMultiplier(uint cofvidEax) { moel@301: switch (family) { moel@301: case 0x10: moel@329: case 0x11: moel@407: case 0x15: moel@407: case 0x16: { moel@329: // 8:6 CpuDid: current core divisor ID moel@329: // 5:0 CpuFid: current core frequency ID moel@329: uint cpuDid = (cofvidEax >> 6) & 7; moel@329: uint cpuFid = cofvidEax & 0x1F; moel@329: return 0.5 * (cpuFid + 0x10) / (1 << (int)cpuDid); moel@301: } moel@329: case 0x12: { moel@329: // 8:4 CpuFid: current CPU core frequency ID moel@329: // 3:0 CpuDid: current CPU core divisor ID moel@329: uint cpuFid = (cofvidEax >> 4) & 0x1F; moel@329: uint cpuDid = cofvidEax & 0xF; moel@329: double divisor; moel@329: switch (cpuDid) { moel@329: case 0: divisor = 1; break; moel@329: case 1: divisor = 1.5; break; moel@329: case 2: divisor = 2; break; moel@329: case 3: divisor = 3; break; moel@329: case 4: divisor = 4; break; moel@329: case 5: divisor = 6; break; moel@329: case 6: divisor = 8; break; moel@329: case 7: divisor = 12; break; moel@329: case 8: divisor = 16; break; moel@329: default: divisor = 1; break; moel@329: } moel@329: return (cpuFid + 0x10) / divisor; moel@329: } moel@329: case 0x14: { moel@329: // 8:4: current CPU core divisor ID most significant digit moel@329: // 3:0: current CPU core divisor ID least significant digit moel@329: uint divisorIdMSD = (cofvidEax >> 4) & 0x1F; moel@329: uint divisorIdLSD = cofvidEax & 0xF; moel@329: uint value = 0; moel@329: Ring0.ReadPciConfig(miscellaneousControlAddress, moel@329: CLOCK_POWER_TIMING_CONTROL_0_REGISTER, out value); moel@329: uint frequencyId = value & 0x1F; moel@329: return (frequencyId + 0x10) / moel@329: (divisorIdMSD + (divisorIdLSD * 0.25) + 1); moel@329: } moel@301: default: moel@301: return 1; moel@301: } moel@273: } moel@273: moel@268: private string ReadFirstLine(Stream stream) { moel@268: StringBuilder sb = new StringBuilder(); moel@268: try { moel@268: stream.Seek(0, SeekOrigin.Begin); moel@268: int b = stream.ReadByte(); moel@268: while (b != -1 && b != 10) { moel@268: sb.Append((char)b); moel@268: b = stream.ReadByte(); moel@268: } moel@268: } catch { } moel@268: return sb.ToString(); moel@268: } moel@268: moel@110: public override void Update() { moel@191: base.Update(); moel@191: moel@268: if (temperatureStream == null) { moel@266: if (miscellaneousControlAddress != Ring0.InvalidPciAddress) { moel@266: uint value; moel@266: if (Ring0.ReadPciConfig(miscellaneousControlAddress, moel@266: REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) { moel@329: if (family == 0x15 && (value & 0x30000) == 0x30000) { moel@407: if ((model & 0xF0) == 0x00) { moel@407: coreTemperature.Value = ((value >> 21) & 0x7FC) / 8.0f + moel@407: coreTemperature.Parameters[0].Value - 49; moel@407: } else { moel@407: coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f + moel@407: coreTemperature.Parameters[0].Value - 49; moel@407: } moel@407: } else if (family == 0x16 && moel@407: ((value & 0x30000) == 0x30000 || (value & 0x80000) == 0x80000)) { moel@407: coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f + moel@407: coreTemperature.Parameters[0].Value - 49; moel@329: } else { moel@329: coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f + moel@329: coreTemperature.Parameters[0].Value; moel@329: } moel@266: ActivateSensor(coreTemperature); moel@266: } else { moel@266: DeactivateSensor(coreTemperature); moel@266: } moel@266: } moel@266: } else { moel@268: string s = ReadFirstLine(temperatureStream); moel@266: try { moel@266: coreTemperature.Value = 0.001f * moel@266: long.Parse(s, CultureInfo.InvariantCulture); moel@42: ActivateSensor(coreTemperature); moel@266: } catch { moel@42: DeactivateSensor(coreTemperature); moel@266: } 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@273: double multiplier; moel@301: multiplier = GetCoreMultiplier(curEax); 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@266: moel@298: public override void Close() { moel@268: if (temperatureStream != null) { moel@268: temperatureStream.Close(); moel@266: } moel@298: base.Close(); moel@266: } moel@1: } moel@1: }