Added support for reading the TAMG ACPI table on Gigabyte mainboards. Fixed a small problem with HDD/SSD names (0 chars are trimmed now as well).
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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-2011
20 the Initial Developer. All Rights Reserved.
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.
39 using System.Collections.Generic;
40 using System.Diagnostics;
41 using System.Globalization;
43 using System.Runtime.InteropServices;
45 using System.Threading;
47 namespace OpenHardwareMonitor.Hardware.CPU {
49 internal sealed class AMD10CPU : AMDCPU {
51 private readonly Sensor coreTemperature;
52 private readonly Sensor[] coreClocks;
53 private readonly Sensor busClock;
55 private const uint PERF_CTL_0 = 0xC0010000;
56 private const uint PERF_CTR_0 = 0xC0010004;
57 private const uint HWCR = 0xC0010015;
58 private const uint P_STATE_0 = 0xC0010064;
59 private const uint COFVID_STATUS = 0xC0010071;
61 private const byte MISCELLANEOUS_CONTROL_FUNCTION = 3;
62 private const ushort FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1203;
63 private const ushort FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1303;
64 private const ushort FAMILY_12H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1703;
65 private const ushort FAMILY_14H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1703;
66 private const ushort FAMILY_15H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1603;
67 private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4;
68 private const uint CLOCK_POWER_TIMING_CONTROL_0_REGISTER = 0xD4;
70 private readonly uint miscellaneousControlAddress;
71 private readonly ushort miscellaneousControlDeviceId;
73 private readonly FileStream temperatureStream;
75 private readonly double timeStampCounterMultiplier;
76 private readonly bool corePerformanceBoostSupport;
78 public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
79 : base(processorIndex, cpuid, settings)
81 // AMD family 1Xh processors support only one temperature sensor
82 coreTemperature = new Sensor(
83 "Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0,
84 SensorType.Temperature, this, new [] {
85 new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
89 case 0x10: miscellaneousControlDeviceId =
90 FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
91 case 0x11: miscellaneousControlDeviceId =
92 FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
93 case 0x12: miscellaneousControlDeviceId =
94 FAMILY_12H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
95 case 0x14: miscellaneousControlDeviceId =
96 FAMILY_14H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
97 case 0x15: miscellaneousControlDeviceId =
98 FAMILY_15H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
99 default: miscellaneousControlDeviceId = 0; break;
102 // get the pci address for the Miscellaneous Control registers
103 miscellaneousControlAddress = GetPciAddress(
104 MISCELLANEOUS_CONTROL_FUNCTION, miscellaneousControlDeviceId);
106 busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
107 coreClocks = new Sensor[coreCount];
108 for (int i = 0; i < coreClocks.Length; i++) {
109 coreClocks[i] = new Sensor(CoreString(i), i + 1, SensorType.Clock,
111 if (HasTimeStampCounter)
112 ActivateSensor(coreClocks[i]);
115 corePerformanceBoostSupport = (cpuid[0][0].ExtData[7, 3] & (1 << 9)) > 0;
117 // set affinity to the first thread for all frequency estimations
118 ulong mask = ThreadAffinity.Set(1UL << cpuid[0][0].Thread);
120 // disable core performance boost
121 uint hwcrEax, hwcrEdx;
122 Ring0.Rdmsr(HWCR, out hwcrEax, out hwcrEdx);
123 if (corePerformanceBoostSupport)
124 Ring0.Wrmsr(HWCR, hwcrEax | (1 << 25), hwcrEdx);
127 Ring0.Rdmsr(PERF_CTL_0, out ctlEax, out ctlEdx);
129 Ring0.Rdmsr(PERF_CTR_0, out ctrEax, out ctrEdx);
131 timeStampCounterMultiplier = estimateTimeStampCounterMultiplier();
133 // restore the performance counter registers
134 Ring0.Wrmsr(PERF_CTL_0, ctlEax, ctlEdx);
135 Ring0.Wrmsr(PERF_CTR_0, ctrEax, ctrEdx);
137 // restore core performance boost
138 if (corePerformanceBoostSupport)
139 Ring0.Wrmsr(HWCR, hwcrEax, hwcrEdx);
141 // restore the thread affinity.
142 ThreadAffinity.Set(mask);
144 // the file reader for lm-sensors support on Linux
145 temperatureStream = null;
146 int p = (int)Environment.OSVersion.Platform;
147 if ((p == 4) || (p == 128)) {
148 string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
149 foreach (string path in devicePaths) {
152 using (StreamReader reader = new StreamReader(path + "/device/name"))
153 name = reader.ReadLine();
154 } catch (IOException) { }
157 temperatureStream = new FileStream(path + "/device/temp1_input",
158 FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
167 private double estimateTimeStampCounterMultiplier() {
168 // preload the function
169 estimateTimeStampCounterMultiplier(0);
170 estimateTimeStampCounterMultiplier(0);
172 // estimate the multiplier
173 List<double> estimate = new List<double>(3);
174 for (int i = 0; i < 3; i++)
175 estimate.Add(estimateTimeStampCounterMultiplier(0.025));
180 private double estimateTimeStampCounterMultiplier(double timeWindow) {
183 // select event "076h CPU Clocks not Halted" and enable the counter
184 Ring0.Wrmsr(PERF_CTL_0,
185 (1 << 22) | // enable performance counter
186 (1 << 17) | // count events in user mode
187 (1 << 16) | // count events in operating-system mode
190 // set the counter to 0
191 Ring0.Wrmsr(PERF_CTR_0, 0, 0);
193 long ticks = (long)(timeWindow * Stopwatch.Frequency);
194 uint lsbBegin, msbBegin, lsbEnd, msbEnd;
196 long timeBegin = Stopwatch.GetTimestamp() +
197 (long)Math.Ceiling(0.001 * ticks);
198 long timeEnd = timeBegin + ticks;
199 while (Stopwatch.GetTimestamp() < timeBegin) { }
200 Ring0.Rdmsr(PERF_CTR_0, out lsbBegin, out msbBegin);
202 while (Stopwatch.GetTimestamp() < timeEnd) { }
203 Ring0.Rdmsr(PERF_CTR_0, out lsbEnd, out msbEnd);
204 Ring0.Rdmsr(COFVID_STATUS, out eax, out edx);
205 double coreMultiplier = GetCoreMultiplier(eax);
207 ulong countBegin = ((ulong)msbBegin << 32) | lsbBegin;
208 ulong countEnd = ((ulong)msbEnd << 32) | lsbEnd;
210 double coreFrequency = 1e-6 *
211 (((double)(countEnd - countBegin)) * Stopwatch.Frequency) /
212 (timeEnd - timeBegin);
214 double busFrequency = coreFrequency / coreMultiplier;
216 return 0.25 * Math.Round(4 * TimeStampCounterFrequency / busFrequency);
219 protected override uint[] GetMSRs() {
220 return new uint[] { PERF_CTL_0, PERF_CTR_0, HWCR, P_STATE_0,
224 public override string GetReport() {
225 StringBuilder r = new StringBuilder();
226 r.Append(base.GetReport());
228 r.Append("Miscellaneous Control Address: 0x");
229 r.AppendLine((miscellaneousControlAddress).ToString("X",
230 CultureInfo.InvariantCulture));
231 r.Append("Time Stamp Counter Multiplier: ");
232 r.AppendLine(timeStampCounterMultiplier.ToString(
233 CultureInfo.InvariantCulture));
234 if (family == 0x14) {
236 Ring0.ReadPciConfig(miscellaneousControlAddress,
237 CLOCK_POWER_TIMING_CONTROL_0_REGISTER, out value);
238 r.Append("PCI Register D18F3xD4: ");
239 r.AppendLine(value.ToString("X8", CultureInfo.InvariantCulture));
246 private double GetCoreMultiplier(uint cofvidEax) {
250 // 8:6 CpuDid: current core divisor ID
251 // 5:0 CpuFid: current core frequency ID
252 uint cpuDid = (cofvidEax >> 6) & 7;
253 uint cpuFid = cofvidEax & 0x1F;
254 return 0.5 * (cpuFid + 0x10) / (1 << (int)cpuDid);
256 // 8:4 CpuFid: current CPU core frequency ID
257 // 3:0 CpuDid: current CPU core divisor ID
258 uint CpuFid = (cofvidEax >> 4) & 0x1F;
259 uint CpuDid = cofvidEax & 0xF;
262 case 0: divisor = 1; break;
263 case 1: divisor = 1.5; break;
264 case 2: divisor = 2; break;
265 case 3: divisor = 3; break;
266 case 4: divisor = 4; break;
267 case 5: divisor = 6; break;
268 case 6: divisor = 8; break;
269 case 7: divisor = 12; break;
270 case 8: divisor = 16; break;
271 default: divisor = 1; break;
273 return (CpuFid + 0x10) / divisor;
275 // 8:4: current CPU core divisor ID most significant digit
276 // 3:0: current CPU core divisor ID least significant digit
277 uint divisorIdMSD = (cofvidEax >> 4) & 0x1F;
278 uint divisorIdLSD = cofvidEax & 0xF;
280 Ring0.ReadPciConfig(miscellaneousControlAddress,
281 CLOCK_POWER_TIMING_CONTROL_0_REGISTER, out value);
282 uint frequencyId = value & 0x1F;
283 return (frequencyId + 0x10) /
284 (divisorIdMSD + (divisorIdLSD * 0.25) + 1);
290 private string ReadFirstLine(Stream stream) {
291 StringBuilder sb = new StringBuilder();
293 stream.Seek(0, SeekOrigin.Begin);
294 int b = stream.ReadByte();
295 while (b != -1 && b != 10) {
297 b = stream.ReadByte();
300 return sb.ToString();
303 public override void Update() {
306 if (temperatureStream == null) {
307 if (miscellaneousControlAddress != Ring0.InvalidPciAddress) {
309 if (Ring0.ReadPciConfig(miscellaneousControlAddress,
310 REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
311 coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f +
312 coreTemperature.Parameters[0].Value;
313 ActivateSensor(coreTemperature);
315 DeactivateSensor(coreTemperature);
319 string s = ReadFirstLine(temperatureStream);
321 coreTemperature.Value = 0.001f *
322 long.Parse(s, CultureInfo.InvariantCulture);
323 ActivateSensor(coreTemperature);
325 DeactivateSensor(coreTemperature);
329 if (HasTimeStampCounter) {
330 double newBusClock = 0;
332 for (int i = 0; i < coreClocks.Length; i++) {
336 if (Ring0.RdmsrTx(COFVID_STATUS, out curEax, out curEdx,
337 1UL << cpuid[i][0].Thread))
340 multiplier = GetCoreMultiplier(curEax);
342 coreClocks[i].Value =
343 (float)(multiplier * TimeStampCounterFrequency /
344 timeStampCounterMultiplier);
346 (float)(TimeStampCounterFrequency / timeStampCounterMultiplier);
348 coreClocks[i].Value = (float)TimeStampCounterFrequency;
352 if (newBusClock > 0) {
353 this.busClock.Value = (float)newBusClock;
354 ActivateSensor(this.busClock);
359 public override void Close() {
360 if (temperatureStream != null) {
361 temperatureStream.Close();