2010-01-26 22:37:48 +00:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the License.
|
|
|
|
|
|
|
|
|
|
The Original Code is the Open Hardware Monitor code.
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Michael Möller <m.moeller@gmx.ch>.
|
2011-04-09 20:16:42 +00:00
|
|
|
|
Portions created by the Initial Developer are Copyright (C) 2009-2011
|
2010-01-26 22:37:48 +00:00
|
|
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
|
|
|
|
|
Alternatively, the contents of this file may be used under the terms of
|
|
|
|
|
either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
|
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
|
in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
|
of those above. If you wish to allow use of your version of this file only
|
|
|
|
|
under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
|
use your version of this file under the terms of the MPL, indicate your
|
|
|
|
|
decision by deleting the provisions above and replace them with the notice
|
|
|
|
|
and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
|
the provisions above, a recipient may use your version of this file under
|
|
|
|
|
the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
using System;
|
2010-09-30 16:51:09 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2010-09-22 19:12:12 +00:00
|
|
|
|
using System.Globalization;
|
2011-04-10 23:36:07 +00:00
|
|
|
|
using System.IO;
|
2010-09-30 16:51:09 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2010-09-22 19:12:12 +00:00
|
|
|
|
using System.Text;
|
2010-09-22 20:44:34 +00:00
|
|
|
|
using System.Threading;
|
2010-08-08 13:57:26 +00:00
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
namespace OpenHardwareMonitor.Hardware.CPU {
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
internal sealed class AMD10CPU : AMDCPU {
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly Sensor coreTemperature;
|
2010-09-22 20:44:34 +00:00
|
|
|
|
private readonly Sensor[] coreClocks;
|
|
|
|
|
private readonly Sensor busClock;
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
|
|
|
|
private const uint PERF_CTL_0 = 0xC0010000;
|
|
|
|
|
private const uint PERF_CTR_0 = 0xC0010004;
|
2010-09-22 20:44:34 +00:00
|
|
|
|
private const uint P_STATE_0 = 0xC0010064;
|
2010-09-30 16:51:09 +00:00
|
|
|
|
private const uint COFVID_STATUS = 0xC0010071;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
private const byte MISCELLANEOUS_CONTROL_FUNCTION = 3;
|
2011-01-27 22:29:43 +00:00
|
|
|
|
private const ushort FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1203;
|
|
|
|
|
private const ushort FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1303;
|
2010-09-22 20:44:34 +00:00
|
|
|
|
private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4;
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
private readonly uint miscellaneousControlAddress;
|
2011-01-27 22:29:43 +00:00
|
|
|
|
private readonly ushort miscellaneousControlDeviceId;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2011-04-10 23:36:07 +00:00
|
|
|
|
private readonly FileStream temperatureStream;
|
2011-04-09 20:16:42 +00:00
|
|
|
|
|
2010-09-30 16:51:09 +00:00
|
|
|
|
private double timeStampCounterMultiplier;
|
|
|
|
|
|
2010-09-20 19:28:25 +00:00
|
|
|
|
public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
|
|
|
|
|
: base(processorIndex, cpuid, settings)
|
2010-09-22 19:12:12 +00:00
|
|
|
|
{
|
2011-01-27 22:29:43 +00:00
|
|
|
|
// AMD family 10h/11h processors support only one temperature sensor
|
2010-02-02 21:58:54 +00:00
|
|
|
|
coreTemperature = new Sensor(
|
2010-06-06 11:07:57 +00:00
|
|
|
|
"Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0,
|
2010-09-21 20:32:36 +00:00
|
|
|
|
SensorType.Temperature, this, new [] {
|
2010-05-20 21:23:54 +00:00
|
|
|
|
new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
|
2010-08-08 13:57:26 +00:00
|
|
|
|
}, settings);
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2011-01-27 22:29:43 +00:00
|
|
|
|
switch (family) {
|
|
|
|
|
case 0x10: miscellaneousControlDeviceId =
|
|
|
|
|
FAMILY_10H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
|
|
|
|
|
case 0x11: miscellaneousControlDeviceId =
|
|
|
|
|
FAMILY_11H_MISCELLANEOUS_CONTROL_DEVICE_ID; break;
|
|
|
|
|
default: miscellaneousControlDeviceId = 0; break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
// get the pci address for the Miscellaneous Control registers
|
|
|
|
|
miscellaneousControlAddress = GetPciAddress(
|
2011-01-27 22:29:43 +00:00
|
|
|
|
MISCELLANEOUS_CONTROL_FUNCTION, miscellaneousControlDeviceId);
|
2010-02-12 22:46:31 +00:00
|
|
|
|
|
2010-09-22 20:44:34 +00:00
|
|
|
|
busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
|
|
|
|
|
coreClocks = new Sensor[coreCount];
|
|
|
|
|
for (int i = 0; i < coreClocks.Length; i++) {
|
|
|
|
|
coreClocks[i] = new Sensor(CoreString(i), i + 1, SensorType.Clock,
|
|
|
|
|
this, settings);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
if (HasTimeStampCounter)
|
2010-09-22 20:44:34 +00:00
|
|
|
|
ActivateSensor(coreClocks[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-03 22:07:46 +00:00
|
|
|
|
// set affinity to the first thread for all frequency estimations
|
|
|
|
|
ulong mask = ThreadAffinity.Set(1UL << cpuid[0][0].Thread);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
|
|
|
|
uint ctlEax, ctlEdx;
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Rdmsr(PERF_CTL_0, out ctlEax, out ctlEdx);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
uint ctrEax, ctrEdx;
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Rdmsr(PERF_CTR_0, out ctrEax, out ctrEdx);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
|
|
|
|
timeStampCounterMultiplier = estimateTimeStampCounterMultiplier();
|
|
|
|
|
|
|
|
|
|
// restore the performance counter registers
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Wrmsr(PERF_CTL_0, ctlEax, ctlEdx);
|
|
|
|
|
Ring0.Wrmsr(PERF_CTR_0, ctrEax, ctrEdx);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
|
|
|
|
// restore the thread affinity.
|
2010-11-03 22:07:46 +00:00
|
|
|
|
ThreadAffinity.Set(mask);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
2011-04-09 20:16:42 +00:00
|
|
|
|
// the file reader for lm-sensors support on Linux
|
2011-04-10 23:36:07 +00:00
|
|
|
|
temperatureStream = null;
|
2011-04-09 20:16:42 +00:00
|
|
|
|
int p = (int)Environment.OSVersion.Platform;
|
|
|
|
|
if ((p == 4) || (p == 128)) {
|
|
|
|
|
string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
|
|
|
|
|
foreach (string path in devicePaths) {
|
|
|
|
|
string name = null;
|
|
|
|
|
try {
|
|
|
|
|
using (StreamReader reader = new StreamReader(path + "/device/name"))
|
|
|
|
|
name = reader.ReadLine();
|
|
|
|
|
} catch (IOException) { }
|
|
|
|
|
switch (name) {
|
|
|
|
|
case "k10temp":
|
2011-04-10 23:36:07 +00:00
|
|
|
|
temperatureStream = new FileStream(path + "/device/temp1_input",
|
|
|
|
|
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
2011-04-09 20:16:42 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
Update();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 16:51:09 +00:00
|
|
|
|
private double estimateTimeStampCounterMultiplier() {
|
|
|
|
|
// preload the function
|
|
|
|
|
estimateTimeStampCounterMultiplier(0);
|
|
|
|
|
estimateTimeStampCounterMultiplier(0);
|
|
|
|
|
|
|
|
|
|
// estimate the multiplier
|
|
|
|
|
List<double> estimate = new List<double>(3);
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
estimate.Add(estimateTimeStampCounterMultiplier(0.025));
|
|
|
|
|
estimate.Sort();
|
|
|
|
|
return estimate[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double estimateTimeStampCounterMultiplier(double timeWindow) {
|
|
|
|
|
uint eax, edx;
|
|
|
|
|
|
|
|
|
|
// select event "076h CPU Clocks not Halted" and enable the counter
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Wrmsr(PERF_CTL_0,
|
2010-09-30 16:51:09 +00:00
|
|
|
|
(1 << 22) | // enable performance counter
|
|
|
|
|
(1 << 17) | // count events in user mode
|
|
|
|
|
(1 << 16) | // count events in operating-system mode
|
|
|
|
|
0x76, 0x00000000);
|
|
|
|
|
|
|
|
|
|
// set the counter to 0
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Wrmsr(PERF_CTR_0, 0, 0);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
|
|
|
|
long ticks = (long)(timeWindow * Stopwatch.Frequency);
|
|
|
|
|
uint lsbBegin, msbBegin, lsbEnd, msbEnd;
|
|
|
|
|
|
|
|
|
|
long timeBegin = Stopwatch.GetTimestamp() +
|
|
|
|
|
(long)Math.Ceiling(0.001 * ticks);
|
|
|
|
|
long timeEnd = timeBegin + ticks;
|
|
|
|
|
while (Stopwatch.GetTimestamp() < timeBegin) { }
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Rdmsr(PERF_CTR_0, out lsbBegin, out msbBegin);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
while (Stopwatch.GetTimestamp() < timeEnd) { }
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Rdmsr(PERF_CTR_0, out lsbEnd, out msbEnd);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
|
2010-10-31 22:08:47 +00:00
|
|
|
|
Ring0.Rdmsr(COFVID_STATUS, out eax, out edx);
|
2010-09-30 16:51:09 +00:00
|
|
|
|
uint cpuDid = (eax >> 6) & 7;
|
|
|
|
|
uint cpuFid = eax & 0x1F;
|
|
|
|
|
double coreMultiplier = MultiplierFromIDs(cpuDid, cpuFid);
|
|
|
|
|
|
|
|
|
|
ulong countBegin = ((ulong)msbBegin << 32) | lsbBegin;
|
|
|
|
|
ulong countEnd = ((ulong)msbEnd << 32) | lsbEnd;
|
|
|
|
|
|
|
|
|
|
double coreFrequency = 1e-6 *
|
|
|
|
|
(((double)(countEnd - countBegin)) * Stopwatch.Frequency) /
|
|
|
|
|
(timeEnd - timeBegin);
|
|
|
|
|
|
|
|
|
|
double busFrequency = coreFrequency / coreMultiplier;
|
|
|
|
|
return 0.5 * Math.Round(2 * TimeStampCounterFrequency / busFrequency);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-20 19:28:25 +00:00
|
|
|
|
protected override uint[] GetMSRs() {
|
2010-09-30 16:51:09 +00:00
|
|
|
|
return new uint[] { PERF_CTL_0, PERF_CTR_0, P_STATE_0, COFVID_STATUS };
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
public override string GetReport() {
|
|
|
|
|
StringBuilder r = new StringBuilder();
|
|
|
|
|
r.Append(base.GetReport());
|
|
|
|
|
|
2010-09-22 20:44:34 +00:00
|
|
|
|
r.Append("Miscellaneous Control Address: 0x");
|
2010-09-22 19:12:12 +00:00
|
|
|
|
r.AppendLine((miscellaneousControlAddress).ToString("X",
|
|
|
|
|
CultureInfo.InvariantCulture));
|
2010-09-30 16:51:09 +00:00
|
|
|
|
r.Append("Time Stamp Counter Multiplier: ");
|
|
|
|
|
r.AppendLine(timeStampCounterMultiplier.ToString(
|
|
|
|
|
CultureInfo.InvariantCulture));
|
|
|
|
|
r.AppendLine();
|
|
|
|
|
|
2010-09-22 19:12:12 +00:00
|
|
|
|
return r.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 16:51:09 +00:00
|
|
|
|
private static double MultiplierFromIDs(uint divisorID, uint frequencyID) {
|
2010-09-22 20:44:34 +00:00
|
|
|
|
return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-10 23:36:07 +00:00
|
|
|
|
private string ReadFirstLine(Stream stream) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
try {
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
int b = stream.ReadByte();
|
|
|
|
|
while (b != -1 && b != 10) {
|
|
|
|
|
sb.Append((char)b);
|
|
|
|
|
b = stream.ReadByte();
|
|
|
|
|
}
|
|
|
|
|
} catch { }
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-09 16:22:13 +00:00
|
|
|
|
public override void Update() {
|
2010-09-20 19:28:25 +00:00
|
|
|
|
base.Update();
|
|
|
|
|
|
2011-04-10 23:36:07 +00:00
|
|
|
|
if (temperatureStream == null) {
|
2011-04-09 20:16:42 +00:00
|
|
|
|
if (miscellaneousControlAddress != Ring0.InvalidPciAddress) {
|
|
|
|
|
uint value;
|
|
|
|
|
if (Ring0.ReadPciConfig(miscellaneousControlAddress,
|
|
|
|
|
REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
|
|
|
|
|
coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f +
|
|
|
|
|
coreTemperature.Parameters[0].Value;
|
|
|
|
|
ActivateSensor(coreTemperature);
|
|
|
|
|
} else {
|
|
|
|
|
DeactivateSensor(coreTemperature);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-04-10 23:36:07 +00:00
|
|
|
|
string s = ReadFirstLine(temperatureStream);
|
2011-04-09 20:16:42 +00:00
|
|
|
|
try {
|
|
|
|
|
coreTemperature.Value = 0.001f *
|
|
|
|
|
long.Parse(s, CultureInfo.InvariantCulture);
|
2010-02-12 22:46:31 +00:00
|
|
|
|
ActivateSensor(coreTemperature);
|
2011-04-09 20:16:42 +00:00
|
|
|
|
} catch {
|
2010-02-12 22:46:31 +00:00
|
|
|
|
DeactivateSensor(coreTemperature);
|
2011-04-09 20:16:42 +00:00
|
|
|
|
}
|
2010-02-03 20:35:10 +00:00
|
|
|
|
}
|
2010-09-22 20:44:34 +00:00
|
|
|
|
|
2010-09-30 16:51:09 +00:00
|
|
|
|
if (HasTimeStampCounter) {
|
2010-09-22 20:44:34 +00:00
|
|
|
|
double newBusClock = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coreClocks.Length; i++) {
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
|
|
|
|
|
uint curEax, curEdx;
|
2010-10-31 22:08:47 +00:00
|
|
|
|
if (Ring0.RdmsrTx(COFVID_STATUS, out curEax, out curEdx,
|
2010-11-03 22:07:46 +00:00
|
|
|
|
1UL << cpuid[i][0].Thread))
|
2010-09-22 20:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
// 8:6 CpuDid: current core divisor ID
|
|
|
|
|
// 5:0 CpuFid: current core frequency ID
|
2010-09-30 16:51:09 +00:00
|
|
|
|
uint cpuDid = (curEax >> 6) & 7;
|
|
|
|
|
uint cpuFid = curEax & 0x1F;
|
|
|
|
|
double multiplier = MultiplierFromIDs(cpuDid, cpuFid);
|
|
|
|
|
|
|
|
|
|
coreClocks[i].Value =
|
|
|
|
|
(float)(multiplier * TimeStampCounterFrequency /
|
|
|
|
|
timeStampCounterMultiplier);
|
|
|
|
|
newBusClock =
|
|
|
|
|
(float)(TimeStampCounterFrequency / timeStampCounterMultiplier);
|
2010-09-22 20:44:34 +00:00
|
|
|
|
} else {
|
2010-09-30 16:51:09 +00:00
|
|
|
|
coreClocks[i].Value = (float)TimeStampCounterFrequency;
|
2010-09-22 20:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newBusClock > 0) {
|
|
|
|
|
this.busClock.Value = (float)newBusClock;
|
|
|
|
|
ActivateSensor(this.busClock);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-30 16:51:09 +00:00
|
|
|
|
}
|
2011-04-09 20:16:42 +00:00
|
|
|
|
|
|
|
|
|
public override void Close() {
|
2011-04-10 23:36:07 +00:00
|
|
|
|
if (temperatureStream != null) {
|
|
|
|
|
temperatureStream.Close();
|
2011-04-09 20:16:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|